If I build a JAR on Java 9 from a command line, I pass a parameter --main-class
to include MainClass attribute into the module-info.class
:
jar --create --file <filename> --main-class=<mainclass> --module-version 0.1 -C classes .
But what if I build a JAR from maven? Here is my maven-jar-plugin
configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
Can I pass custom arguments to maven-jar-plugin
(like compilerArgs
for maven-compiler-plugin
)?
A temporary workaround is to call jar --update
in the package
phase:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
...
</configuration>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>package</phase>
<configuration>
<target>
<exec executable="${java.home}/bin/jar" failonerror="true">
<arg value="--main-class"/>
<arg value="[main class here]"/>
<arg value="--module-version"/>
<arg value="0.1"/>
<arg value="--update"/>
<arg value="--file"/>
<arg value="${project.build.directory}/${project.artifactId}-${project.version}.jar"/>
</exec>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
I'm open for better ideas.
This is possible with maven-jar-plugin version 3.1.2+ (not released yet, but issue is closed):
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<archive>
<manifest>
<mainClass>[main class here]</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
The description says:
Starting with version 3.1.2, if the JAR file contains module-info.class, this plugin will update the modular descriptor (module-info.class) with additional attributes [...]. The most notable additional attribute added is the module main class.
Basically it takes the value which is added to the Manifest-file and also adds it to module-info.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With