我想将我的项目打包在一个可执行的JAR中进行分发。
如何使Maven项目将所有依赖JAR打包到输出JAR中?
我想将我的项目打包在一个可执行的JAR中进行分发。
如何使Maven项目将所有依赖JAR打包到输出JAR中?
当前回答
我仔细阅读了这些响应中的每一个,希望创建一个包含所有依赖项的可执行JAR文件,但没有一个能正常工作。答案是shade插件,它非常简单明了。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<!-- Run shade goal on package phase -->
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>path.to.MainClass</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
请注意,您的依赖项需要有编译或运行时的范围才能正常工作。
这个例子来自mkyong.com
其他回答
为了解决这个问题,我们将使用Maven Assembly插件,该插件将JAR文件及其依赖JAR文件一起创建为单个可执行JAR文件。只需在pom.xml文件中添加以下插件配置。
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.your.package.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-my-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
完成此操作后,不要忘记使用以下命令运行Maven工具:
mvn clean compile assembly:single
Maven-将JAR文件及其依赖JAR文件一起创建为单个可执行JAR文件
您可以使用依赖插件在包阶段之前在单独的目录中生成所有依赖项,然后将其包含在清单的类路径中:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>theMainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
或者,使用${project.build.directory}/classes/lib作为OutputDirectory将所有JAR文件集成到主JAR文件中,但随后需要添加自定义类加载代码来加载JAR文件。
好吧,这是我的解决方案。我知道它没有使用pom.xml文件。但我在NetBeans上编译和运行程序时遇到了问题,但当我尝试Java-jarMyJarFile.jar时失败了。现在,我不完全理解Maven,我想这就是为什么在NetBeans8.0.2中将我的jar文件包含在库中以将其放入jar文件时遇到了困难。我在想我以前是如何在Eclipse中使用没有Maven的JAR文件的。
Maven可以编译所有的依赖项和插件。不是NetBeans。(如果您可以获得NetBeans并能够使用java.jar来实现这一点,请告诉我如何(^.^)v)
已解决-针对Linux:通过打开终端。
Then
cd /MyRootDirectoryForMyProject
Next
mvn org.apache.maven.plugins:maven-compiler-plugin:compile
Next
mvn install
这将在目标目录中创建一个JAR文件。
MyJarFile-1.0-jar-with-dependencies.jar
Now
cd target
(您可能需要运行:chmod+x MyJarFile-1.0-jar-with-dependencies.jar)
最后
java -jar MyJarFile-1.0-jar-with-dependencies.jar
请参阅LifecyclePhaseNotFoundException。
maven汇编插件对我很有用。
我花了几个小时使用maven依赖插件,但无法使其工作。主要原因是我必须在配置部分中明确定义工件项,如文档中所述,应包含这些工件项。
这里有一个例子,当您想使用它时,比如:mvn dependency:copy,其中没有包含任何artifactItems,但它不起作用。
您可以将以下内容添加到pom.xml文件中:
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.mycompany.package.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.mycompany.package.MainClass</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-my-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
然后,您必须通过控制台切换到pom.xml文件所在的目录。然后,您必须执行mvn assembly:single,然后希望构建具有依赖关系的可执行JAR文件。您可以在切换到带有cd的输出(目标)目录时检查它/目标并使用类似于java-jarmavenproject1-1.0-SNAPSHOT-JAR-with-dependencies.JAR的命令启动JAR。
我用Apache Maven 3.0.3测试了这一点。