[英]Maven: Generating an executable jar file with all the dependencies
I recently converted one of my Java project to a Maven project. 我最近将我的Java项目之一转换为Maven项目。 My pom.xml looks something like this:
我的pom.xml看起来像这样:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<groupId>com.myproject.groupid</groupId>
<artifactId>myproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MyProject</name>
<description>The first version of my maven project</description>
<dependencies>
<dependency>
<groupId>com.dependent.jar</groupId>
<artifactId>dependentjar</artifactId>
<version>0.0.1</version>
<scope>system</scope>
<type>jar</type>
<systemPath>${project.basedir}/jars/dependent.jar</systemPath>
</dependency>
<dependency>
<groupId>org.apache.ws.commons.schema</groupId>
<artifactId>XmlSchema</artifactId>
<version>1.4.3</version>
</dependency>
<dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.myproject.main.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</build>
</project>
When i execute the mvn compile and mvn install, the project works fine and it also generates the jar. 当我执行mvn compile和mvn install时,项目运行正常,并且还会生成jar。 But when i try to run the jar [using java -jar MyProject.jar], i get an error which says: Exception in thread "main" java.lang.NoClassDefFoundError and this is because maven is not able to add the dependent jar specified in the section.
但是,当我尝试运行jar(使用java -jar MyProject.jar)时,出现错误消息:线程“ main” java.lang.NoClassDefFoundError中的异常,这是因为maven无法添加指定的从属jar在本节中。 [it is not available during run time]
[在运行时不可用]
Could anybody let me know the best possible way for me to copy the systemPath jars to the jar that is being generated by maven? 有人可以让我知道将systemPath jar复制到maven生成的jar的最佳方法吗?
I looked at maven-shade-plugin and maven-assembly-plugin and could not find much luck with both of them. 我查看了maven-shade-plugin和maven-assembly-plugin,但两者都发现运气不佳。 Any help would be appreciated!
任何帮助,将不胜感激!
Try to add this to your maven-jar-plugin configuration 尝试将其添加到您的maven-jar-plugin配置中
<addClasspath>true</addClasspath>
<classpathPrefix>*path to dependencies*</classpathPrefix>
Also to manage your dependencies you can use Maven dependency plugin 另外,要管理您的依赖项,您可以使用Maven依赖项插件
In the build section of your pom, you can use: 在pom的build部分中,您可以使用:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>com:library:**</include>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Then, on the command line, you can run mvn package
, it will show in the output which jars it is excluding. 然后,在命令行上,您可以运行
mvn package
,它将在输出中显示其排除的jar。 Then, you can include selected jars accordingly. 然后,您可以相应地包括选定的罐子。 If you then get
NoClassDefFoundError
, you may need to point Java to your class, by for example using com.company.Main
as an argument to the java
command. 如果随后收到
NoClassDefFoundError
,则可能需要将Java指向类,例如,使用com.company.Main
作为java
命令的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.