Search code examples
javaeclipsemavenjarm2eclipse

Maven runnable jar with dependencies


I am using two plugins to generate a runnable jar file with m2e plugin from eclipse. Here is the config:

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
            <archive>
                <manifest>
                    <mainClass>com.unlockservice.App</mainClass>
                    <addClasspath>true</addClasspath>
                    <classpathPrefix>lib/</classpathPrefix>
                </manifest>
            </archive>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.1</version>
        <executions>
            <execution>
                <id>copy-dependencies</id>
                <phase>package</phase>
                <goals>
                    <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                    <outputDirectory>${project.build.directory}/lib</outputDirectory>
                    <overWriteReleases>false</overWriteReleases>
                    <overWriteSnapshots>false</overWriteSnapshots>
                    <overWriteIfNewer>true</overWriteIfNewer>
                </configuration>
            </execution>
        </executions>
      </plugin>

In additional to standard dependencies I have a couple of local ones wich are added in the following way:

<dependency>
        <groupId>com.ejl</groupId>
        <artifactId>CRMObjects</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${basedir}/lib/CRMObjects.jar</systemPath>
    </dependency>
    <dependency>
        <groupId>com.ejl</groupId>
        <artifactId>CRMPDFGenerators</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${basedir}/lib/CRMPDFGenerators.jar</systemPath>
    </dependency>

After maven's build everything looks fine. All the libraries copied to lib folder. But when I copy both jar file and lib folder to the server and run the file with java -jar path-to-file.jar it fails because can't find the classes from external libraries (CRMObjects.jar).

Any suggestions why it happens? Thank you in advance


Solution

  • Finally made it work with two plugins:

    <plugin>
         <artifactId>maven-compiler-plugin</artifactId>
         <version>3.1</version>
         <configuration>
              <source>1.6</source>
              <target>1.6</target>
         </configuration>
    </plugin>
    <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-shade-plugin</artifactId>
         <version>2.3</version>
         <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
            </execution>
         </executions>
         <configuration>
            <finalName>${project.artifactId}-${project.version}</finalName>
                <transformers>
                    <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.ManifestResourceTransformer">
                                <mainClass>com.main.App</mainClass>
                    </transformer>
                </transformers>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
        </plugin>
    

    At the end I have the completed executable jar file with all dependencies and libraries inside it.