简体   繁体   中英

Eclipse built-in jar export option vs Maven assembly plugin

I use IntelliJ IDEA to generate a jar file with maven-assembly-plugin .

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <archive>
            <manifest>
                <mainClass>test.LeanFTest</mainClass>
            </manifest>
        </archive>
        <finalName>${project.artifactId}-fatjar-${project.version}</finalName>
        <appendAssemblyId>false</appendAssemblyId>
    </configuration>
</plugin>

It didn't work, it asks continuously for dependencies during execution. I was unable to use jar file, so I imported the project to Eclipse and used the built-in jar export option. This jar is working fine. I couldn't find what the difference is between these jar files.

The reason is probably that Eclipse is generating a "Class-Path:" entry in the manifest file. Try to update the assembly conf in your .pom file by adding something like (by heart, not checked):

<addClasspath>true</addClasspath>

By default the maven-assembly-plugin will not bundle all the dependencies required for your own jar to run standalone.

So you will need to add this below under the plugin configuration:

<descriptorRefs>
    <descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>

As a result, the plugin tag becomes:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <archive>
            <manifest>
                <mainClass>test.LeanFTest</mainClass>
            </manifest>
        </archive>
        <finalName>${project.artifactId}-fatjar-${project.version}</finalName>
        <appendAssemblyId>false</appendAssemblyId>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef> <!-- the secret -->
        </descriptorRefs>
    </configuration>
</plugin>

You can run:

java -jar path/to/LeanFTest.jar

To directly run the program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM