英文:
maven-jar-plugin: How to create a different artifact name for tests?
问题
让我们假设我将以下插件放入我的pom.xml文件中:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>make-a-jar</id>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<finalName>my-artifact-name</finalName>
</configuration>
</execution>
</executions>
</plugin>
然后我运行“mvn clean install”。
Maven会在目标库中创建两个jar文件。
第一个jar文件编译源文件,第二个jar文件编译测试文件。这两个jar文件都将具有**相同**的构件名称。
如果我想将源代码的jar作为另一个项目的依赖项,我可以在另一个项目中添加以下依赖项:
<dependency>
<groupId>groupId</groupId>
<artifactId>my-artifact-name</artifactId>
<scope>system</scope>
<type>jar</type>
<systemPath>${basedir}/lib/my-artifact-name.jar</systemPath>
</dependency>
到目前为止都很好。
如果我还想为测试文件添加依赖项,就会出现问题。在这种情况下,我将有两个具有相同groupId和artifactId但systemPath不同的依赖项。Maven不会读取具有相同groupId和artifactId的两个依赖项,只会读取其中一个。
我能想到的一个解决方法是让Maven为测试提供一个不同的构件名称。
你知道怎么做吗?
英文:
Let's say that I put the following plugin in my pom.xml file:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>make-a-jar</id>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<finalName>my-artifact-name</finalName>
</configuration>
</execution>
</executions>
</plugin>
and I run "mvn clean install".
Maven creates two jar files in target library.
The first jar file compiles the source files and the second jar compiles the test files. Both of these jar will have the same artifact name.
If I want to use the jar of source code as a dependency in another project, I can put the following dependency in the other project:
<dependency>
<groupId>groupId</groupId>
<artifactId>my-artifact-name</artifactId>
<scope>system</scope>
<type>jar</type>
<systemPath>${basedir}/lib/my-artifact-name.jar</systemPath>
</dependency>
So far so good.
A problem arises if I also want to add a dependency for the test files. In this case I will have two dependencies with the same groupId and artifactId and different systemPath. Maven will not read two dependencies with same groupId and artifactId. Only one of them will be read.
One solution that I can think of is to make Maven to give a different artifact name for test.
Do you know how to do it?
答案1
得分: 2
我的一般回答是:
src/test/java
文件夹中的测试仅用于在构建期间运行。它们不需要放入任何 JAR 包中。
如果您需要作为测试辅助类的类,您可以创建一个单独的 JAR 包,其中包含这些类。然后可以将其用作 test
依赖项。
无论如何,尽量避免使用 <systemPath>
。如果您在计算机上使用 mvn clean install
构建项目,您可以使用类似以下的依赖项引用生成的 JAR 包:
<dependency>
<groupId>groupId</groupId>
<artifactId>my-artifact-name</artifactId>
<version>1.2.3</version>
</dependency>
在同一账户/计算机上的任何位置都可以引用,无需提供 <systemPath>
。
英文:
My general answer to that would be:
The tests in src/test/java
are only for running them during the build. They need not be put into any jar.
If you need classes as helper classes for your tests, you can create a separate jar which contains these classes. This can then be used as test
dependency.
In any case, try to avoid <systemPath>
. If you build your project with mvn clean install
on your computer, you can reference the resulting jar with a dependency like
<dependency>
<groupId>groupId</groupId>
<artifactId>my-artifact-name</artifactId>
<version>1.2.3</version>
</dependency>
anywhere on the same account/computer without giving a <systemPath>
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论