#12 Building with Ant
Oct 06, 2012 |
10 minutes |
Build
In this episode I show how to build a standard Xtext project with an Ant.
- Download:
- source code Project Files in Zip (20.9 MB)
- mp4 Full Size H.264 Video (26.7 MB)
- m4v Smaller H.264 Video (15.3 MB)
- webm Full Size VP8 Video (16.4 MB)
- ogv Full Size Theora Video (39.9 MB)
WARNING: Do NOT name the ant build file: build.xml.
The build.xml
file will be removed by the eclipse plugin export wizard.
The ant-build.xml file
ant-build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="mydsl" default="all">
<property name="workflow" value="src/org/xtext/example/mydsl/GenerateMyDsl.mwe2"/>
<path id="classpath">
<fileset dir="lib" includes="*.jar"/>
<fileset dir="." includes=".antlr-generator-3.2.0.jar"/>
<pathelement location="src"/>
</path>
<target name="clean">
<delete dir="ant"/>
</target>
<target name="generate-xtext-artifacts">
<java classpathref="classpath"
classname="org.eclipse.emf.mwe2.launch.runtime.Mwe2Launcher"
fork="true">
<arg value="${workflow}"/>
</java>
</target>
<target name="compile-xtend">
<pathconvert pathsep="${path.separator}"
property="classpath-string"
refid="classpath">
</pathconvert>
<java classpathref="classpath"
classname="org.eclipse.xtend.core.compiler.batch.Main"
fork="true"
failonerror="yes">
<arg value="-cp" />
<arg value="${classpath-string}" />
<arg value="-d" />
<arg value="${basedir}/ant/xtend-gen" />
<arg value="${basedir}/src-gen${path.separator}${basedir}/src" />
</java>
</target>
<target name="compile-java">
<mkdir dir="ant/bin"/>
<javac classpathref="classpath"
destdir="ant/bin"
source="1.6"
target="1.6"
includeantruntime="true">
<src path="src" />
<src path="src-gen" />
<src path="ant/xtend-gen" />
</javac>
<copy todir="ant/bin">
<fileset dir="src" excludes="**/*.java,**/*.xtend" />
<fileset dir="src-gen" excludes="**/*.java,**/*.xtend" />
<fileset dir="ant/xtend-gen" excludes="**/*.java,**/*.xtend" />
</copy>
</target>
<target name="all" depends="clean,generate-xtext-artifacts,compile-xtend,compile-java"
description="build all">
</target>
</project>
<?xml version="1.0" encoding="UTF-8"?> <project name="mydsl" default="all"> <property name="workflow" value="src/org/xtext/example/mydsl/GenerateMyDsl.mwe2"/> <path id="classpath"> <fileset dir="lib" includes="*.jar"/> <fileset dir="." includes=".antlr-generator-3.2.0.jar"/> <pathelement location="src"/> </path> <target name="clean"> <delete dir="ant"/> </target> <target name="generate-xtext-artifacts"> <java classpathref="classpath" classname="org.eclipse.emf.mwe2.launch.runtime.Mwe2Launcher" fork="true"> <arg value="${workflow}"/> </java> </target> <target name="compile-xtend"> <pathconvert pathsep="${path.separator}" property="classpath-string" refid="classpath"> </pathconvert> <java classpathref="classpath" classname="org.eclipse.xtend.core.compiler.batch.Main" fork="true" failonerror="yes"> <arg value="-cp" /> <arg value="${classpath-string}" /> <arg value="-d" /> <arg value="${basedir}/ant/xtend-gen" /> <arg value="${basedir}/src-gen${path.separator}${basedir}/src" /> </java> </target> <target name="compile-java"> <mkdir dir="ant/bin"/> <javac classpathref="classpath" destdir="ant/bin" source="1.6" target="1.6" includeantruntime="true"> <src path="src" /> <src path="src-gen" /> <src path="ant/xtend-gen" /> </javac> <copy todir="ant/bin"> <fileset dir="src" excludes="**/*.java,**/*.xtend" /> <fileset dir="src-gen" excludes="**/*.java,**/*.xtend" /> <fileset dir="ant/xtend-gen" excludes="**/*.java,**/*.xtend" /> </copy> </target> <target name="all" depends="clean,generate-xtext-artifacts,compile-xtend,compile-java" description="build all"> </target> </project>
Copy dependencies from to the lib directory:
- Select all dependencies in Eclipse and press
Ctrl-C
cd
into the lib directory and run
$ while read i ; do copy $i . ; done
$ while read i ; do copy $i . ; done
- Paste the clipboard content into the console (
Ctrl-Shift-C
on my Ubuntu) - Press
Ctrl+D
Run the Mydsl generator from the command line
$ java -classpath $(find lib/ -name "*.jar" -printf "%p:")ant/bin \
org.xtext.example.mydsl.generator.Main greetings.mydsl
$ java -classpath $(find lib/ -name "*.jar" -printf "%p:")ant/bin \ org.xtext.example.mydsl.generator.Main greetings.mydsl
Ref "classpath" defined in ant-build.xml
- ${proj-dir}/src
- ${proj-dir}/.antlr-generator-3.2.0.jar
- ${proj-dir}/lib/com.google.guava_10.0.1.v201203051515.jar
- ${proj-dir}/lib/com.google.inject.assistedinject_3.0.0.201207101151.jar
- ${proj-dir}/lib/com.google.inject.multibindings_3.0.0.201207101151.jar
- ${proj-dir}/lib/com.google.inject_3.0.0.v201203062045.jar
- ${proj-dir}/lib/com.ibm.icu_4.4.2.v20110823.jar
- ${proj-dir}/lib/javax.inject_1.0.0.v20091030.jar
- ${proj-dir}/lib/org.antlr.runtime_3.2.0.v201101311130.jar
- ${proj-dir}/lib/org.apache.commons.cli_1.2.0.v201105210650.jar
- ${proj-dir}/lib/org.apache.commons.logging_1.0.4.v201101211617.jar
- ${proj-dir}/lib/org.apache.log4j_1.2.15.v201012070815.jar
- ${proj-dir}/lib/org.eclipse.emf.codegen.ecore_2.8.1.v20120917-0436.jar
- ${proj-dir}/lib/org.eclipse.emf.codegen_2.8.0.v20120917-0436.jar
- ${proj-dir}/lib/org.eclipse.emf.common_2.8.0.v20120911-0500.jar
- ${proj-dir}/lib/org.eclipse.emf.ecore.xmi_2.8.0.v20120911-0500.jar
- ${proj-dir}/lib/org.eclipse.emf.ecore_2.8.1.v20120911-0500.jar
- ${proj-dir}/lib/org.eclipse.emf.mwe.core_1.2.1.v201206110920.jar
- ${proj-dir}/lib/org.eclipse.emf.mwe.utils_1.2.1.v201206110920.jar
- ${proj-dir}/lib/org.eclipse.emf.mwe2.language_2.3.0.v201206120758.jar
- ${proj-dir}/lib/org.eclipse.emf.mwe2.launch_2.3.0.v201206120758.jar
- ${proj-dir}/lib/org.eclipse.emf.mwe2.lib_2.3.0.v201206110920.jar
- ${proj-dir}/lib/org.eclipse.emf.mwe2.runtime_2.3.0.v201206110920.jar
- ${proj-dir}/lib/org.eclipse.jdt.annotation_1.0.0.v20120728-095341.jar
- ${proj-dir}/lib/org.eclipse.jdt.core_3.8.2.v20120814-155456.jar
- ${proj-dir}/lib/org.eclipse.xpand_1.2.0.v201206110941.jar
- ${proj-dir}/lib/org.eclipse.xtend.core_2.3.1.v201208210947.jar
- ${proj-dir}/lib/org.eclipse.xtend.lib_2.3.1.v201208210947.jar
- ${proj-dir}/lib/org.eclipse.xtend.typesystem.emf_1.2.0.v201206110941.jar
- ${proj-dir}/lib/org.eclipse.xtend_1.2.0.v201206110941.jar
- ${proj-dir}/lib/org.eclipse.xtext.common.types_2.3.1.v201208210947.jar
- ${proj-dir}/lib/org.eclipse.xtext.generator_2.3.1.v201208210947.jar
- ${proj-dir}/lib/org.eclipse.xtext.util_2.3.1.v201208210947.jar
- ${proj-dir}/lib/org.eclipse.xtext.xbase.lib_2.3.1.v201208210947.jar
- ${proj-dir}/lib/org.eclipse.xtext.xbase_2.3.1.v201208210947.jar
- ${proj-dir}/lib/org.eclipse.xtext_2.3.1.v201208210947.jar
Running Ant build out of eclipse
Running Ant build out of eclipse can lead sometimes to errors within the main Xtext project. This could be caused by the auto-refresh workspace feature turned on or something else. If you experience the problem, you can add the following line to your build.xml right after the xtext workflow launch. This will force eclipse to rebuild the project.
xml
<eclipse.incrementalBuild project="<project>.xtext" kind="clean"/>
<eclipse.incrementalBuild project="<project>.xtext" kind="clean"/>
To make it work, you have to ensure, that the Ant-script runs within eclipse-JRE. This could be verified in the Ant launch configuration. You can also save the launch configuration and commit it into your SCM (e. g. git) spreading the configuration across other team members.