Namek Dev
a developer's log
NamekDev

Build Runnable JAR of Xtend + JavaFX project in Maven

April 12, 2016

After a long research I’ve found a site that helps with compiling Xtend** **using Maven - http://www.eclipse.org/xtend/download.html - pretty straightforward, isn’t it? However, it lacks a few more things.

My goal was to compile and package Java 8 project together with JavaFX, written mostly in Xtend (in place of Java) with unpacked libraries into one Runnable JAR. In the end I’d like it to be compatible with e(fx)clipse IDE. Here’s how.

Create pom.xml

Make sure you have Maven installed in your system. Then call this:

mvn archetype:generate -DarchetypeGroupId=org.eclipse.xtend -DarchetypeArtifactId=xtend-archetype

This will ask you about groupId, artifactId, version and package. Based on that information it creates a pom.xml file that is configured to compile Xtend project into Java 1.5.

Improve pom.xml

What’s left:

  1. I expect it to work with Java 8, not only Java 5
  2. add resources: *.fxml  and *.css  files
  3. add dependencies (other libs, like minimal-json)
  4. package jar by unpacking depedencies into that jar

And this jar needs to be runnable!

1. Java version

To correct Java 1.5 to newer version of source/target find similiar fragment and simply edit 1.5 to e.g. 1.8:

<plugin>
	<artifactId>maven-compiler-plugin</artifactId>
	<version>3.0</version>
	<configuration>
		<source>1.5</source>
		<target>1.5</target>
	</configuration>
</plugin>

2. Resources

Pattern is very simple. Just put this snippet into   part.

<resources>
	<resource>
		<directory>src/main/java</directory>
		<includes>
			<include>**/*.fxml</include>
			<include>**/*.css</include>
		</includes>
	</resource>
</resources>

3. Dependencies

  part is already in generated file. Add in there what you need. My choice at the time was to add minimal-json and JNativeHook.

<dependency>
	<groupId>com.1stleg</groupId>
	<artifactId>jnativehook</artifactId>
	<version>2.0.2</version>
</dependency>
<dependency>
	<groupId>com.eclipsesource.minimal-json</groupId>
	<artifactId>minimal-json</artifactId>
	<version>0.9.4</version>
</dependency>

4. Package

Very important step is to package my app into single .jar file. Amongs many options I choosed to use maven-assembly-plugin:

<plugin>
	<artifactId>maven-assembly-plugin</artifactId>
	<configuration>
		<archive>
			<manifest>
				<mainClass>net.namekdev.theconsole.Main</mainClass>
			</manifest>
		</archive>
		<descriptorRefs>
			<descriptorRef>jar-with-dependencies</descriptorRef>
		</descriptorRefs>
		<appendAssemblyId>false</appendAssemblyId>
	</configuration>
	<executions>
		<execution>
			<id>make-assembly</id>
			<phase>package</phase>
			<goals>
				<goal>single</goal>
			</goals>
		</execution>
	</executions>
</plugin>

One of interesting parts is: false  because filename of the output file results in theconsole-0.1-SNAPSHOT.jar  instead of  theconsole-0.1-SNAPSHOT-jar-with-dependencies.jar .

Another important part is   tag - it defines which class should be the entry point for the app. That’s how we configure the Runnable JAR.

Summary

Now I can simply run mvn package  to get my so awaited .jar inside of target  folder. To see whole configuration in one snippet take a look at the pom.xml in The Console project.

Daj Się Poznać, the-console
comments powered by Disqus