Search code examples
javamavenjava-9

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


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


Solution

  • 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.