具有依赖性的Maven runnable jar

时间:2014-07-11 14:53:14

标签: java eclipse maven jar m2eclipse

我正在使用两个插件来生成一个带有来自eclipse的m2e插件的可运行jar文件。这是配置:

<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>

除了标准依赖项之外,我还有几个本地的依赖项,以下列方式添加:

<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>

在maven的构建之后,一切看起来都很好。所有库都复制到lib文件夹。但是当我将jar文件和lib文件夹复制到服务器并使用java -jar path-to-file.jar运行文件时,它失败了,因为无法从外部库(CRMObjects.jar)找到类。

有什么建议吗?提前谢谢

2 个答案:

答案 0 :(得分:0)

Java并不知道在哪里可以找到您的库jar。你需要一个&#34; Class-Path:lib / jar1.jar lib / jar2.jar&#34;在应用程序jar中的清单文件中键入条目,或者需要在命令行上设置类路径。请参阅此答案:https://stackoverflow.com/a/219801/1271971

答案 1 :(得分:0)

最后使用了两个插件:

<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>

最后,我有完整的可执行jar文件,其中包含所有依赖项和库。