XText maven plugin output configuration obscurity [message #1413732] |
Fri, 29 August 2014 09:46 |
Michel de Blok Messages: 10 Registered: February 2010 |
Junior Member |
|
|
Hi all,
I'm using the XText Maven plugin to generate code on our Jenkins build server and it seems behavior is different from running the generator in Eclipse.
I'm using separate output configurations for "manual" (custom) code that will be generated once and won't overwrite any existing Java sources (i.e. src/main/java)
Works fine in Eclipse, but on Jenkins the Maven build always overwrites my existing sources in src/main/java. Even if I set 'overrideExistingResources' to false in the language setup of the XText Maven plugin, using:
<outputConfigurations>
<outputConfiguration>
<name>custom-java</name>
<outputDirectory>./src/main/java</outputDirectory>
<overrideExistingResources>false</overrideExistingResources>
<cleanUpDerivedResources>false</cleanUpDerivedResources>
</outputConfiguration>
</outputConfigurations>
After digging in the XText source code, it seems that a different implementation of IFileSystemAccess is used when running a Maven build (JavaIoFileSystemAccess) that does not support all outputConfiguration settings (!) In this case, it never checks if a file already exists, it always (over)writes it.
What's the use of defining configuration elements in a Maven plugin that are never used in reality? This was very confusing to me and now causes quite a few bugs in code released with Maven, because it's based on invalid source code (all custom changes are gone).
Did anyone else run into this issue? Solution seems to write your own extension of JavaIoFileSystemAccess that does support all OutputConfiguration settings, so trying that now..
Regards,
Michel de Blok
|
|
|
|
|
Re: XText maven plugin output configuration obscurity [message #1413773 is a reply to message #1413770] |
Fri, 29 August 2014 11:35 |
Michel de Blok Messages: 10 Registered: February 2010 |
Junior Member |
|
|
The workaround I'm using now is to create a subclass of JavaIoFileSystemAccess called OutputConfigurationAwareFileSystemAccess in my DSL project and to implement the missing features there.
I have added the following binding to my DSL runtime module:
public Class<? extends JavaIoFileSystemAccess> bindJavaIoFileSystemAccess() {
return OutputConfigurationAwareFileSystemAccess.class;
}
And then use the following implementation of OutputConfigurationAwareFileSystemAccess:
package my.package.common.output;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import org.eclipse.xtext.generator.JavaIoFileSystemAccess;
import org.eclipse.xtext.generator.OutputConfiguration;
import org.eclipse.xtext.util.RuntimeIOException;
import com.google.common.io.ByteStreams;
public class OutputConfigurationAwareFileSystemAccess extends JavaIoFileSystemAccess {
@Override
public void generateFile(String fileName, String outputName, CharSequence contents) throws RuntimeIOException {
OutputConfiguration outputConfig = getOutputConfig(outputName);
File file = getFile(fileName, outputName);
if (!createFolder(file.getParentFile(), outputConfig)) {
return; // folder does not exist
}
if (!file.exists() || outputConfig.isOverrideExistingResources()) {
try {
String encoding = getEncoding(getURI(fileName, outputName));
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), encoding);
try {
writer.append(postProcess(fileName, outputName, contents, encoding));
if (isWriteTrace()) {
generateTrace(fileName, outputName, contents);
}
} finally {
writer.close();
}
} catch (IOException e) {
throw new RuntimeIOException(e);
}
}
}
@Override
public void generateFile(String fileName, String outputName, InputStream content) throws RuntimeIOException {
OutputConfiguration outputConfig = getOutputConfig(outputName);
File file = getFile(fileName, outputName);
if (!createFolder(file.getParentFile(), outputConfig)) {
return; // folder does not exist
}
if (!file.exists() || outputConfig.isOverrideExistingResources()) {
try {
OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
try {
ByteStreams.copy(content, out);
} finally {
try {
out.close();
} finally {
content.close();
}
}
} catch (IOException e) {
throw new RuntimeIOException(e);
}
}
}
protected boolean createFolder(File parent, OutputConfiguration outputConfig) {
if (parent != null && !parent.exists() && outputConfig.isCreateOutputDirectory() && !parent.mkdirs()) {
throw new RuntimeIOException("Could not create directory " + parent);
}
return parent != null && parent.exists();
}
}
But I really think this should be fixed in JavaIoFileSystemAccess in the next version of XText.
Regards,
Michel
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03378 seconds