I've been using the ant task to generate a schema.sql file for years.
I recently upgraded from Hibernate 4 to 5 to find that now the task always appends to any existing file.
I traced the behavior to the org.hibernate.tool.schema.internal.exec.ScriptTargetOutputToFile class:
Code:
@SuppressWarnings("ResultOfMethodCallIgnored")
static Writer toFileWriter(File file) {
try {
if ( ! file.exists() ) {
// best effort, since this is very likely not allowed in EE environments
log.debug( "Attempting to create non-existent script target file : " + file.getAbsolutePath() );
if ( file.getParentFile() != null ) {
file.getParentFile().mkdirs();
}
file.createNewFile();
}
}
catch (Exception e) {
log.debug( "Exception calling File#createNewFile : " + e.toString() );
}
try {
return new FileWriter( file, true );
}
catch (IOException e) {
throw new SchemaManagementException( "Unable to open specified script target file for writing : " + file, e );
}
}
As you see, the FileWriter is created with a "true" second parameter, which means "append".
Shouldn't this be corrected or maybe offered as an option of the ant task?