Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » XText maven plugin output configuration obscurity(Output configuration settings can be defined but are never used?)
icon8.gif  XText maven plugin output configuration obscurity [message #1413732] Fri, 29 August 2014 09:46 Go to next message
Michel de Blok is currently offline Michel de BlokFriend
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 #1413764 is a reply to message #1413732] Fri, 29 August 2014 11:15 Go to previous messageGo to next message
Marc Schlegel is currently offline Marc SchlegelFriend
Messages: 69
Registered: July 2009
Member
I dont have a solution for the override but you should add an additional source folder to your maven-configuration instead, and let xtext generate files to that folder.

Then you also have the possibility to exclude that folder from your source-control. The files will be generated on every build so they are always up to date.

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-clean-plugin</artifactId>
      <version>2.5</version>
      <executions>
         <execution>
            <phase>clean</phase>
            <goals>
               <goal>clean</goal>
            </goals>
            <configuration>
		<filesets>
			 <fileset>
				<directory>src/main/generated/</directory>
				<includes>
					<include>**/*</include>
				 </includes>
			</fileset>
		</filesets>
            </configuration>
      </execution>
      </executions>
</plugin>

...

<outputDirectory>src/main/generated</outputDirectory>
Re: XText maven plugin output configuration obscurity [message #1413770 is a reply to message #1413764] Fri, 29 August 2014 11:26 Go to previous messageGo to next message
Michel de Blok is currently offline Michel de BlokFriend
Messages: 10
Registered: February 2010
Junior Member
Thanks, but that won't work because in my case because I want to generate skeletons of Java sources and resources in the regular places (src/main/java, src/test/java, etc..) to make it easier for developers to implement custom code..
Re: XText maven plugin output configuration obscurity [message #1413773 is a reply to message #1413770] Fri, 29 August 2014 11:35 Go to previous messageGo to next message
Michel de Blok is currently offline Michel de BlokFriend
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



Re: XText maven plugin output configuration obscurity [message #1413778 is a reply to message #1413773] Fri, 29 August 2014 11:51 Go to previous messageGo to next message
Marc Schlegel is currently offline Marc SchlegelFriend
Messages: 69
Registered: July 2009
Member
Glad you figured out a nice solution.

You could file a bug here and attach your solution.
Re: XText maven plugin output configuration obscurity [message #1413802 is a reply to message #1413778] Fri, 29 August 2014 13:05 Go to previous message
Michel de Blok is currently offline Michel de BlokFriend
Messages: 10
Registered: February 2010
Junior Member
Seems there already was an issue for it: https://bugs.eclipse.org/bugs/show_bug.cgi?id=398979

Added my comments..
Previous Topic:JVM Crash with Cross-References
Next Topic:[Solved] [Xpand/Xtend] : metamodel importation
Goto Forum:
  


Current Time: Sat Dec 07 14:04:19 GMT 2019

Powered by FUDForum. Page generated in 0.02236 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top