Java 如何告诉Maven将所有依赖项重新打包到一个jar文件中
给定一个包含依赖项列表的pom.xml文件,是否有Maven命令将所有这些jar捆绑到一个大jar中我不想在构建中包含我的.class文件-我只想在构建中包含库.class/jar文件-有什么方法可以做到这一点吗 我看到了这个插件:Java 如何告诉Maven将所有依赖项重新打包到一个jar文件中,java,maven,jar,pom.xml,Java,Maven,Jar,Pom.xml,给定一个包含依赖项列表的pom.xml文件,是否有Maven命令将所有这些jar捆绑到一个大jar中我不想在构建中包含我的.class文件-我只想在构建中包含库.class/jar文件-有什么方法可以做到这一点吗 我看到了这个插件: <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifac
<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}/Crunchify/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
但是,当我使用pom.xml中的
maven汇编插件运行mvn clean package
时,我没有看到任何新的jar文件。Witjar与依赖项和single~package
,你陷入了汇编的一个“陷阱”(在包(未完成)之前没有任何东西需要汇编)
但结合这两个插件,这将起作用:
/pom.xml
…将为您提供:
/target/${build.finalName}-big-big-jar.jar
…文件,包含所有暂时依赖项,而不包含.jar/类。即jar和single~package
,您陷入了程序集的一个“陷阱”(在包(未完成)之前没有任何程序集)
但结合这两个插件,这将起作用:
/pom.xml
…将为您提供:
/target/${build.finalName}-big-big-jar.jar
..文件,包含所有没有.jar/类的临时依赖项。这是类似的:maven shade插件()是一个不错的选择。它可以处理组合JAR时出现的所有奇怪情况(例如,合并元数据)。@Rob Thank,你能举一个只打包依赖项而不打包用户/项目类的例子吗?为了帮助理解什么是好的解决方案,你能解释一下为什么不想在结果JAR中打包类吗?${project.build.directory}
通常是target
文件夹…这是类似的:maven shade plugin()是一种方法。它可以处理组合JAR时出现的所有奇怪情况(例如,合并元数据)@Rob谢谢,你能举一个只打包依赖项而不打包用户/项目类的例子吗?为了帮助理解什么是一个好的解决方案,你能解释一下为什么你不想把你的类放在结果JAR中吗?${project.build.directory}
通常是目标
文件夹。。。
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<project>
...
<!-- many many dependencies -->
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<descriptors>
<descriptor>src/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>copy-deps</id>
<phase>process-sources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>big-big-jar</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}/dependency</directory>
<outputDirectory />
<includes>
<!-- attention: maybe you need more ... zip, tz.gz.bz. -->
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>
mvn clean package
...
BUILD SUCCESS