【发布时间】:2016-11-03 16:20:36
【问题描述】:
我有一个项目,其中有多个 .jar 文件。要将其添加为依赖项,我需要将所有这些罐子变成一个大罐子。到目前为止,我已经到了:
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>mainProjects</groupId>
<artifactId>mainProjects.master</artifactId>
<relativePath>../pom.xml</relativePath>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>mainProjects</groupId>
<artifactId>sampleModule1</artifactId>
<name>sampleModule1</name>
<version>1.0.0.qualifier</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>Sample1.jar</include>
<include>Sample2.jar</include>
</includes>
</artifactSet>
<finalName>${project.artifactId}-${project.version}-final</finalName>
<outputDirectory>${project.artifactId}/build</outputDirectory>
<shadedArtifactAttached>false</shadedArtifactAttached>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
这将创建最终的 jar,但是,内部没有其他 jar(sample1.jar 和 sample2.jar)中应有的内容。我查看了插件的文档,但是他们所做的只是通过类文件而不是 jar 来完成。所以从现在开始我不知道该怎么做。
有什么想法吗?
更新:
所以为了说清楚,特此分享一下我拥有的项目结构:
+- mainProjects
| +- pom.xml
| +- mainProject1
| | +- pom.xml
| | +- src
| +- mainProject2
| | +- pom.xml
| | +- src
| +- group1
| | +- pom.xml
| | +- sampleModule1
| | | +- pom.xml
| | | +- build
| | | +- sample1.jar
| | | +- sample2.jar
| | | +- sample3.jar
| | | +- sample4.jar
| | | +- sample5.jar
| | | +- sample6.jar
| | +- sampleModule2
| | | +- pom.xml
| | | +- src
现在,我希望能够在 pom.xml 的 pom.xml 中将 sampleModule1 用作 jar 的依赖项,如下所示:
<dependency>
<groupId>group1</groupId>
<artifactId>sampleModule1</artifactId>
<version>1.0.0.qualifier</version>
<scope>system</scope>
<systemPath>sampleModule1/build/${project.artifactId}-${project.version}-final.jar</systemPath>
</dependency>
为此,我需要将所有的jar 编译成一个jar,这样我就可以使用一个systemPath 来添加它。我找到了this,它展示了如何将多个文件包含在一个文件中的示例。但是,在示例中它们不是罐子,而是类和其他。现在在这里,我试图实现相同的目标,但只使用 jars。
【问题讨论】:
-
你的项目结构是什么?这些 jar 文件在 pom.xml 文件之外吗?
-
谢谢提醒。它们与 pom.xml 位于同一目录中。
-
为什么不将它们作为依赖项添加到 pom.xml 中?这就是maven的意思!
-
同一个项目下的10个.jar,你确定吗?我的意思是,据我所知,在添加依赖项时,您可以为每个工件 ID 添加一个 .jar。我可以只使用相同的 artifact-id 并继续添加所有 jar 吗?
-
请张贴您的结构和实际 jar 文件的屏幕截图,以帮助我们更好地理解。谢谢!
标签: maven jar maven-plugin maven-shade-plugin