3

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>
Community
  • 1
  • 1
S__R
  • 31
  • 2
  • 5

3 Answers3

2

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.

DB5
  • 13,553
  • 7
  • 66
  • 71
sathishvisa
  • 315
  • 1
  • 5
  • 16
0

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.

Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66
0

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

Andrey Chaschev
  • 16,160
  • 5
  • 51
  • 68