Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify --main-class and --module-version in maven-jar-plugin?

Tags:

java

maven

java-9

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)?

like image 208
ZhekaKozlov Avatar asked Apr 28 '17 03:04

ZhekaKozlov


2 Answers

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.

like image 127
ZhekaKozlov Avatar answered Oct 13 '22 10:10

ZhekaKozlov


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.

like image 35
Itchy Avatar answered Oct 13 '22 09:10

Itchy