Question

I'm using maven for building my jar (Intellij IDEA IDE). It is app built using Apache Camel. An excerpt from my pom.xml:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <mainClass>MainDriver</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

I'm able to retrieve a jar file from maven but upon running all I got are the NoClassDeFFound errors.

Finally, I've tried using the one-jar plugin (http://one-jar.sourceforge.net/‎) . And I've got it working. My question is can I achieve the same without using one-jar? I'm not liking the packaging of one-jar, what it does is when you extract the jar from one-jar, you will find your app jar within it, so you'll need to do an extra extraction on your jar to get to your classes/resources. My configuration files are on resources and from time to time, I'll be needing to modify it.

Thanks!

Was it helpful?

Solution

The maven-shade-plugin creates a jar with your application code and all dependencies merged into a single jar file. The problem with this approach is that you need to decided what to do with files with conflicting names. This is the configuration we use to build executable JARs for our Camel apps, including Spring and all required dependencies.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>1.5</version>
    <configuration>
        <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <mainClass>org.apache.camel.spring.Main</mainClass>
            </transformer>
            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                <resource>META-INF/spring.handlers</resource>
            </transformer>
            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                <resource>META-INF/spring.schemas</resource>
            </transformer>
            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                <resource>META-INF/spring.tooling</resource>
            </transformer>
            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                <resource>META-INF/cxf/bus-extensions.txt</resource>
            </transformer>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
            <transformer implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
                <resource>META-INF/INDEX.LIST</resource>
            </transformer>
            <transformer implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
                <resource>META-INF/MSFTSIG.SF</resource>
            </transformer>
            <transformer implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
                <resource>META-INF/MSFTSIG.RSA</resource>
            </transformer>
        </transformers>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
        </execution>
    </executions>
</plugin>

OTHER TIPS

You can use maven-jar-plugin

<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
    <archive>
        <manifest>
            <addClasspath>true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
            <mainClass>com.poc.sms.MainApp</mainClass>
        </manifest>
    </archive>
</configuration>
</plugin>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top