Xtext Tip: How to make a DSL generate code from the code generated from another DSL

Sometimes it happens that you have an Xtext DSL that needs generate code to be consumed by another DSL. We can see an example of the need in the question in the Eclipse Forums. This is especially a problem when you use maven to build your projects.

There is a simple solution for that. You can make xtext-maven-plugin generate the code at different phases of the compilation.

In this example we have two DSLs: HelloGen that generates a hello file, and Hello that generates a txt file from a hello file.

To do this, you can do as follows in your pom.xml file:

			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>build-helper-maven-plugin</artifactId>
				<executions>
					<execution>
						<id>add-source-hello</id>
						<goals>
							<goal>add-source</goal>
						</goals>
						<phase>prepare-package</phase>
						<configuration>
							<sources>
								<source>${project.build.outputDirectory}/generated/xtext</source>
							</sources>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.eclipse.xtext</groupId>
				<artifactId>xtext-maven-plugin</artifactId>
				<executions>
					<execution>
						<id>generate-hello</id>
						<goals>
							<goal>generate</goal>
						</goals>
						<configuration>
							<sourceRoots>
								<sourceRoot>${project.basedir}/src/main/java</sourceRoot>
								<sourceRoot>${project.basedir}/src/test/java</sourceRoot>
							</sourceRoots>
							<languages>
								<language>
									<setup>com.idiomaticsoft.dsl.hellogenn.HelloGenStandaloneSetup</setup>
									<outputConfigurations>
										<outputConfiguration>
											<outputDirectory>${project.build.directory}/generated-sources/xtext/</outputDirectory>
										</outputConfiguration>
									</outputConfigurations>
								</language>
							</languages>
						</configuration>
					</execution>
					<execution>
						<id>generate-txt</id>
						<goals>
							<goal>generate</goal>
						</goals>
						<phase>prepare-package</phase>
						<configuration>
							<languages>
								<language>
									<setup>com.idiomaticsoft.dsl.hello.HelloStandaloneSetup</setup>
									<outputConfigurations>
										<outputConfiguration>
											<outputDirectory>${project.build.directory}/generated-sources/xtext</outputDirectory>
										</outputConfiguration>
									</outputConfigurations>
								</language>
							</languages>
						</configuration>
					</execution>
				</executions>
					<dependencies>
						<dependency>
							<groupId>com.idiomaticsoft.dsl.hello</groupId>
							<artifactId>com.idiomaticsoft.dsl.hello</artifactId>
							<version>1.0.0-SNAPSHOT</version>
						</dependency>
						<dependency>
							<groupId>com.idiomaticsoft.dsl.hellogenn</groupId>
							<artifactId>com.idiomaticsoft.dsl.hellogenn</artifactId>
							<version>1.0.0-SNAPSHOT</version>
						</dependency>
					</dependencies>
			</plugin>

The xtext-maven-plugin is configured to run at two phases, the first one (generate-hello) as usual, and the second one (generate-txt) at the prepare-package phase. The prepare-package runs after the compilation, so at that time the code of generate-hello will have already been run.

The trick to make this work is to have the build-helper-maven-plugin to add the generation directory to the source folders. The full example for this tip can be found here.

comments powered by Disqus