maven-dependency-plugin:unpack 忽略类型配置

maven-dependency-plugin:unpack ignores type configuration

有两个独立的Maven项目。既没有公共父 POM 文件也没有聚合器 POM 文件。

第二个项目需要 shared-tests 提供的一些 类。更准确地说,我会在相关项目中重复使用一些常见的测试用例,并在 testintegration-test 阶段 运行 它们。

第一个项目 (shared-tests) 包含一些共享测试用例。

<?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.Whosebug</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>

宁运行后

mvn clean install

测试 jar 已与生产 jar 一起存储。

spinner@zaphod:~/.m2/repository/io/github/zforgo/Whosebug/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 阶段尝试解包。

<?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.Whosebug</groupId>
    <artifactId>project</artifactId>
    <version>0.1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>io.github.zforgo.Whosebug</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.Whosebug</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>

但是当我运行

mvn install

shared-tests 的生产工件将被解压,而不是 test-jar。

[INFO] ---------------< io.github.zforgo.Whosebug: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.Whosebug:shared-tests:0.1.0-SNAPSHOT:test-jar
[INFO] Unpacking /home/spinner/.m2/repository/io/github/zforgo/Whosebug/shared-tests/0.1.0-SNAPSHOT/shared-tests-0.1.0-SNAPSHOT.jar to /work/source/Whosebug/junit-test-sharing/test-jar/project/target/alternateLocation with includes "" and excludes ""
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

根据日志,已配置所需的工件

Configured Artifact: io.github.zforgo.Whosebug:shared-tests:0.1.0-SNAPSHOT:test-jar

但是解压错了

Unpacking [...]/shared-tests/0.1.0-SNAPSHOT/shared-tests-0.1.0-SNAPSHOT.jar to ...

这是一个错误还是有可能将 test-jar 解压到其他项目中?可能使用 <classifier/> 可能是一种解决方案,但这并不是为了指定工件的类型。

经过一些调试后,这似乎是一个错误。

unpack 目标可以很好地读取配置并创建具有正确参数的 ArtifactItem 对象。但是,当它基于 ArtifactItem 创建一个 Artifact 对象时,未设置类型参数,将使用默认值 (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();

将类型设置为 java 作为默认值,它从未更新过。 此处报告了此行为:MDEP-732

解决方法可以使用 <classifier> 而不是 <type>,例如:

<!-- ... -->
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>io.github.zforgo.Whosebug</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>
<!-- ... -->