问题描述
有两个独立的Maven项目。既没有共同的父级文件,也没有聚合器 POM 文件。
第二个项目需要一些由 MotionEvent
提供的类。更准确地说,我会在依赖项目中重用一些常见的测试用例,并在 shared-tests
和 test
阶段运行它们。
第一个项目 (integration-test
) 包含一些共享测试用例。
shared-tests
运行后
mvn 全新安装
测试 jar 与生产 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.zforgo.stackoverflow</groupId>
<artifactId>shared-tests</artifactId>
<version>0.1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.release>11</maven.compiler.release>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</project>
第二个项目将 spinner@zaphod:~/.m2/repository/io/github/zforgo/stackoverflow/shared-tests/0.1.0-SNAPSHOT$ ll
total 28
drwxrwxr-x 2 spinner spinner 4096 Dec 20 15:16 ./
drwxrwxr-x 3 spinner spinner 4096 Dec 20 15:16 ../
-rw-rw-r-- 1 spinner spinner 931 Dec 20 15:23 maven-Metadata-local.xml
-rw-rw-r-- 1 spinner spinner 248 Dec 20 15:23 _remote.repositories
-rw-rw-r-- 1 spinner spinner 3183 Dec 20 15:23 shared-tests-0.1.0-SNAPSHOT.jar
-rw-rw-r-- 1 spinner spinner 3299 Dec 20 14:41 shared-tests-0.1.0-SNAPSHOT.pom
-rw-rw-r-- 1 spinner spinner 3961 Dec 20 15:23 shared-tests-0.1.0-SNAPSHOT-tests.jar
定义为测试范围的依赖项,并在 shared-tests
阶段尝试解包。
generate-test-sources
但是当我跑步时
mvn 安装
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.zforgo.stackoverflow</groupId>
<artifactId>project</artifactId>
<version>0.1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>io.github.zforgo.stackoverflow</groupId>
<artifactId>shared-tests</artifactId>
<version>0.1.0-SNAPSHOT</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.2</version>
<executions>
<execution>
<id>share-tests</id>
<phase>generate-test-sources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>io.github.zforgo.stackoverflow</groupId>
<artifactId>shared-tests</artifactId>
<version>0.1.0-SNAPSHOT</version>
<type>test-jar</type>
<outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
的生产工件将被解包而不是 test-jar。
shared-tests
根据日志,已配置所需的工件
配置的工件:io.github.zforgo.stackoverflow:shared-tests:0.1.0-SNAPSHOT:test-jar
但是解压错了。
解压 [...]/shared-tests/0.1.0-SNAPSHOT/shared-tests-0.1.0-SNAPSHOT.jar 到 ...
这是一个错误还是有可能将 test-jar 解压到其他项目中?可能使用 [INFO] ---------------< io.github.zforgo.stackoverflow:project >---------------
[INFO] Building project 0.1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:3.1.2:unpack (share-tests) @ project ---
[INFO] Configured Artifact: io.github.zforgo.stackoverflow:shared-tests:0.1.0-SNAPSHOT:test-jar
[INFO] Unpacking /home/spinner/.m2/repository/io/github/zforgo/stackoverflow/shared-tests/0.1.0-SNAPSHOT/shared-tests-0.1.0-SNAPSHOT.jar to /work/source/stackoverflow/junit-test-sharing/test-jar/project/target/alternateLocation with includes "" and excludes ""
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
可能是一种解决方案,但这不是为了指定工件的类型。
解决方法
经过一些调试后,这似乎是一个错误。
unpack
目标可以很好地读取配置并使用正确的参数创建一个 ArtifactItem
对象。但是当它基于 Artifact
创建一个 ArtifactItem
对象时,没有设置类型参数,将使用默认值 (jar
)。
可以在here找到源代码。
相关部分是:
protected Artifact getArtifact( ArtifactItem artifactItem )
throws MojoExecutionException
{
Artifact artifact;
try
{
// ...
ProjectBuildingRequest buildingRequest = newResolveArtifactProjectBuildingRequest();
if ( localRepositoryDirectory != null )
{
buildingRequest =
repositoryManager.setLocalRepositoryBasedir( buildingRequest,localRepositoryDirectory );
}
// Map dependency to artifact coordinate
DefaultArtifactCoordinate coordinate = new DefaultArtifactCoordinate();
coordinate.setGroupId( artifactItem.getGroupId() );
coordinate.setArtifactId( artifactItem.getArtifactId() );
coordinate.setVersion( artifactItem.getVersion() );
coordinate.setClassifier( artifactItem.getClassifier() );
// ...
artifact = artifactResolver.resolveArtifact( buildingRequest,coordinate ).getArtifact();
}
catch ( ArtifactResolverException e )
{
throw new MojoExecutionException( "Unable to find/resolve artifact.",e );
}
return artifact;
}
虽然
// Map dependency to artifact coordinate
DefaultArtifactCoordinate coordinate = new DefaultArtifactCoordinate();
将 type 设置为 java
作为它从未更新的默认值。
此行为在此处报告:MDEP-732
解决方法可以使用 <classifier>
而不是 <type>
像:
<!-- ... -->
<configuration>
<artifactItems>
<artifactItem>
<groupId>io.github.zforgo.stackoverflow</groupId>
<artifactId>shared-tests</artifactId>
<version>0.1.0-SNAPSHOT</version>
<!-- note: classifier must be tests not test-jar -->
<classifier>tests</classifier>
<outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
<!-- ... -->