【发布时间】:2013-03-27 13:12:02
【问题描述】:
我对使用 Maven 构建我的 Java 项目还很陌生,并且遇到了我不知道如何处理的情况。
我有一个具有 3 个依赖项的 Java 应用程序,我们称它们为 a、b 和 c。但是,c 将是不同的工件,具体取决于我们构建的平台,所以我使用配置文件来实现这一点。这是我pom.xml的一个sn-p:
<profiles>
<profile>
<id>win32</id>
<activation>
<os>
<family>windows</family>
<arch>x86</arch>
</os>
</activation>
<dependencies>
<dependency>
<groupId>com.seanbright</groupId>
<artifactId>c-win32-x86</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>win64</id>
<activation>
<os>
<family>windows</family>
<arch>amd64</arch>
</os>
</activation>
<dependencies>
<dependency>
<groupId>com.seanbright</groupId>
<artifactId>c-win32-x86_64</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</profile>
</profiles>
a 和 b 工件被列为 POM 级别的依赖项,因为它们与平台无关并且不会与配置文件一起激活。为简洁起见,此处未显示它们。
现在我想为我的项目构建一个可执行 JAR,并将 a、b 和 c 与我的代码生成的 JAR 一起包含在 lib/ 目录中,所以我最终会得到像这样:
target/my-project-1.0.0.jar
target/lib/a-1.0.0.jar
target/lib/b-1.0.0.jar
target/lib/c-1.0.0.jar
my-project-1.0.0.jar 中的清单将具有适当的类路径,以便可以双击它并启动应用程序。我使用dependency:copy-dependencies 和jar:jar 目标来完成所有这些工作:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-dependencies</id>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>com.seanbright.myproject.Launch</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
而且...它有效。唯一的问题是c 被复制到lib/ 目录(并添加到清单中的Class-Path)为c-win32-x86-1.0.0.jar 或c-win32-x86_64-1.0.0.jar,具体取决于活动配置文件,我希望它结束改为c-1.0.0.jar。
将dependency:copy 与destFileName 一起使用而不是dependency:copy-dependencies 会产生正确的文件名,但Class-Path 中的条目仍然引用“完全限定”的工件名称(即lib/c-win32-x86-1.0.0.jar)。
我是不是走错了路?有没有更简单的方法来完成我想做的事情?
【问题讨论】:
标签: maven maven-dependency-plugin maven-profiles maven-jar-plugin