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

If I build a Java 9 JAR from the command line, I pass a parameter --main-classto include the MainClass attribute in 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 configuration maven-jar-plugin:

<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(e.g. compilerArgsfor maven-compiler-plugin)?

+10
source share
4 answers

The workaround is to call jar --updatein phase package:

<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 am open to the best ideas.

+7
source

java-9, , @Steve . maven-plugin Java-9

, ( Java-8), maven-jar-plugin

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.0.2</version>
        <configuration>
            <archive>
                <manifest>
                    <mainClass>com.maven.your.MainClass</mainClass>
                </manifest>
            </archive>
        </configuration>
    </plugin>
</plugins>

, com.maven.your.MainClass , , - jar.

MainClass, , maven-assembly-plugin .. JAR Maven?,

- Apache Maven http://maven.apache.org/shared/maven-archiver/index.html

+1

maven-jar-plugin 3.1. 2+ ( , ):

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

:

3.1.2, JAR module-info.class, (module-info.class) [...]. .

, , Manifest , module-info.

+1

, :

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

MANIFEST.MF

Main-Class: classname

:

          <configuration>  
                <archive>  
                    <manifest>  
                        <mainClass>com.Main</mainClass>
                        <addClasspath>true</addClasspath>  
                    </manifest>  
                </archive>  
            </configuration>  
-4

Source: https://habr.com/ru/post/1016847/


All Articles