我对使用 Maven 构建我的 Java 项目还很陌生,并且遇到了我不知道如何处理的情况。
我有一个具有 3 个依赖项的 Java 应用程序,我们称它们a
为 、b
和c
. 但是,c
根据我们构建的平台,这将是一个不同的工件,所以我使用配置文件来实现这一点。这是我的一个片段pom.xml
:
<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>
和工件在 POM 级别被列为依赖项,因为它们与平台无关并且不会与配置文件一起激活a
。b
为简洁起见,此处未显示它们。
现在我想为我的项目构建一个可执行的 JAR,并在从我的代码生成的 JAR 旁边的目录中包含、a
和b
,所以我最终会得到这样的结果:c
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
andjar: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
withdestFileName
而不是会dependency:copy-dependencies
产生正确的文件名,但其中的条目Class-Path
仍然是指“完全限定”的工件名称(即lib/c-win32-x86-1.0.0.jar
)。
我会以错误的方式解决这个问题吗?有没有更简单的方法来完成我想要做的事情?