0

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

nKognito
  • 6,297
  • 17
  • 77
  • 138

2 Answers2

0

Java doesn't know where to find your library jars. You either need a "Class-Path: lib/jar1.jar lib/jar2.jar" type entry in the manifest file in your application jar or you need to set the classpath on the command line. See this answer: https://stackoverflow.com/a/219801/1271971

Community
  • 1
  • 1
nicktalbot
  • 416
  • 2
  • 6
  • But it knows where to find all the other dependencies (for example spring or hibernate) that copied to the lib folder.. – nKognito Jul 11 '14 at 15:06
  • 1
    OK. I guess it's because you've used "system" scope - this implies the jars are available on the system so are not copied to the lib folder. – nicktalbot Jul 11 '14 at 15:08
  • Either that or they are copied to the lib folder but not added to the "Class-Path:" line in the manifest by the maven-jar-plugin? – nicktalbot Jul 11 '14 at 15:11
  • Yes, they are missing in the manifest file but copied to the lib folder.. How can I make them appear in the manifest file? – nKognito Jul 11 '14 at 17:51
  • Unfortunately it is impossible to use both `-jar` and `-cp` directive in a single command... – nKognito Jul 11 '14 at 20:29
  • I think it's the 'system' scope is causing your problem. Try following this answer http://stackoverflow.com/a/2230464/1271971 to install the jars into your local repo and treating the same as other dependencies. – nicktalbot Jul 16 '14 at 10:39
0

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.

nKognito
  • 6,297
  • 17
  • 77
  • 138