16

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

ZhekaKozlov
  • 36,558
  • 20
  • 126
  • 155
  • 1
    @SteveC Read the question carefully. `MANIFEST.MF` != `module-info.java` – ZhekaKozlov Apr 28 '17 at 03:56
  • 1
    Apologies. I have not seen anything that suggests that any of the maven plugins have been updated for Java 9 yet. More info at [Apache Maven Java 9 - Jigsaw](https://cwiki.apache.org/confluence/display/MAVEN/Java+9+-+Jigsaw) – Steve C Apr 28 '17 at 04:02
  • 1
    Does not yet work: https://issues.apache.org/jira/browse/MJAR-238 – eckes Mar 16 '18 at 03:19

3 Answers3

9

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.

ZhekaKozlov
  • 36,558
  • 20
  • 126
  • 155
7

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.

Itchy
  • 2,263
  • 28
  • 41
-4

Maybe you can use this:

<configuration>
   <archive>
      <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
   </archive>
</configuration>

then edit the MANIFEST.MF file

Main-Class: classname

edit: I found this configuration

          <configuration>  
                <archive>  
                    <manifest>  
                        <mainClass>com.Main</mainClass>
                        <addClasspath>true</addClasspath>  
                    </manifest>  
                </archive>  
            </configuration>  
frank
  • 52
  • 4