繁体   English   中英

具有依赖项的Maven可运行jar

[英]Maven runnable jar with dependencies

我正在使用两个插件通过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)中找到类。

有什么建议为什么会发生吗? 先感谢您

Java不知道在哪里可以找到您的库jar。 您可能需要在应用程序jar的清单文件中输入“ Class-Path:lib / jar1.jar lib / jar2.jar”类型条目,或者需要在命令行上设置类路径。 看到这个答案: https : //stackoverflow.com/a/219801/1271971

最终使它与两个插件一起工作:

<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文件,其中包含所有依赖项和库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM