【发布时间】:2012-01-26 11:31:59
【问题描述】:
我正在使用 maven 创建一个独立的 java 应用程序,并且我正在使用 maven-dependecy-plugin 将依赖项包含在 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}/classes/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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>create-my-bundle</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
这包括 lib 文件夹中生成的 jar 文件中的依赖项,该 jar 运行正常,但问题出在另一个生成的 jar 文件 appname-1.0-jar-with-dependencies.jar。
问题:我不确定这是否是问题,但我注意到在生成的appname-1.0-jar-with-dependencies.jar 的目标文件夹中,它包含重复的应用程序文件,例如:
- 所有 sql、property 文件、xml 文件都存在两次。
- 所有 java 类 .class 文件都存在两次。
- 有很多与依赖相关的overview.html和license.txt文件。
我不确定这是否正确,我还需要有人为我澄清这个生成的 jar 文件的重要性,因为我不熟悉这个插件。
请指教,谢谢。
【问题讨论】:
-
“存在两次”是什么意思?两个文件不能有相同的路径——你看到的完整路径是什么?该插件创建了一个包含所有项目依赖项的 jar 文件,因此它可以独立运行——不知道你为什么还要使用它;你为什么开始使用它?
-
当我用 win rar 查看这个 jar 时,我可以在我的应用程序中看到每个 .sql,.xml 文件的两个副本。
-
我使用它是因为我想制作一个包含所有依赖项的独立应用程序,这是一个不好的做法吗?还是我应该使用另一个插件?
-
这就是插件的用途。关于双文件——我问的是完整路径是什么,因为两个文件不能同名。
-
@DaveNewton -- MahmoudS 完全正确。 JAR 格式可以保存重复文件(完全相同的路径和名称),这是 maven-assembly-plugin 的一个已知问题。见:stackoverflow.com/questions/10308916/…
标签: java maven-2 jar packaging maven-dependency-plugin