简体   繁体   中英

Executable jar with dependency using maven without unpacking jars but include classpath

I probably need something like this question

Maven build assembly with dependencies

or maven assembly create jar with dependency and class path

How do I execute the executable jar ?

java -jar at commandline gives me Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger

I have executable.jar created in my target directory and lib directory with all dependency jar under /lib directoty.

This is the snippet of my pom.xml.What should I be changing?

</dependencies> 
 <dependency>
<groupId><gId></groupId>
<artifactId><aId></artifactId>
<version><v></version>
</dependency>
<dependency>
<groupId>gI2</groupId>
<artifactId>aI2</artifactId>
<version>v</version>
</dependency>
</dependencies>
<build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.proj.app</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>

Uses Maven Shade plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>1.6</version>
    <configuration>
        <createDependencyReducedPom>true</createDependencyReducedPom>
        <filters>
            <filter>
                <artifact>*:*</artifact>
                <excludes>
                    <exclude>META-INF/*.SF</exclude>
                    <exclude>META-INF/*.DSA</exclude>
                    <exclude>META-INF/*.RSA</exclude>
                </excludes>
            </filter>
        </filters>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass><Your main class here></mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

This will include all your dependencies in one jar and will automatically add the main class to your Manifest file.

Make sure you run the "package" goal.

Your distribution needs all JAR files that include the libraries you are using in your application. The assembly plugin is designed for exactly that requirement.

I usually use th assembly plugin to create a ZIP file containing the whole distribution. That is the JAR file of my own application, all necesary config files, and - of course - all library JARs.

Additionally, your executable JAR file needs a proper manifest file. That is, it needs a correct main classe entry and a class path.

You can do this with App Assembler plugin. It will copy the dependencies to a folder and create shortcuts to execute your main classes.

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