maven-dependency-plugin:unpack ignores type configuration

zforgo

There are two independent Maven projects. There isn't neither common parent nor aggregator POM files.

The second project needs some classes provided by shared-tests. More precisely I'd re-use some common test cases in dependent project and run them during test and integration-test phases.

The first project (shared-tests) contains some shared test cases.

<?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>

After running

mvn clean install

the test jar has stored alongside production jar.

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

The second project defines the shared-tests as a test-scoped dependency and during generate-test-sources phase it tries to unpack.

<?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>

But when I run

mvn install

the production artifact of shared-tests will be unpacked instead of 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] ------------------------------------------------------------------------

Based on the logs the desired artifact has been configured

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

but the wrong one was unpacked.

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

Is this a bug or is there any possibility to unpack test-jar into an other project? Probably using <classifier/> can be a solution but that is not made to specify type of an artifact.

zforgo

After some debugging it seems this is a bug.

The unpack goal reads configuration well and creates an ArtifactItem object with the right parameters. But when it creates an Artifact object based on ArtifactItem the type parameter wasn't set and the default value (jar) will used.

Source code can be found here.

Related part is:

   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;
    }

While the

            // Map dependency to artifact coordinate
            DefaultArtifactCoordinate coordinate = new DefaultArtifactCoordinate();

sets type to java as a default value it never updated. This behaviour reported here: MDEP-732

Workaround can be using <classifier> instead of <type> like:

<!-- ... -->
                        <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>
<!-- ... -->

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Maven-dependency-plugin (unpack-dependencies) ignores configuration

From Dev

maven-dependency-plugin ignores outputDirectory configuration

From Dev

maven dependency plugin ignores dependency versions?

From Dev

Maven unpack-dependencies of a plugin dependency

From Dev

maven-dependency-plugin:unpack Error

From Dev

Maven maven-dependency-plugin copy-dependencies ignores outputDirectory

From Dev

How to unpack just the files from a sub-folder in maven-dependency-plugin with goal unpack-dependencies?

From

maven-dependency-plugin (goals "copy-dependencies", "unpack") is not supported by m2e

From Dev

maven exclude plugin in dependency

From Dev

Maven resources plugin dependency

From Dev

Maven PlugIn Dependency Exception

From Dev

Maven Plugin Dependency : class not found

From Dev

How to show dependency of a plugin in Maven?

From Dev

How to scan maven plugin dependency?

From Java

Maven dependency management for plugin dependencies

From Dev

Intellij Idea ignores maven assembly plugin

From Dev

maven-war-plugin ignores <archiveClasses>

From Dev

Wildfly Maven Plugin ignores deployment name?

From Dev

Proguard Maven Plugin Ignores Injar Jar Name

From Dev

Maven requires plugin version to be specified for the managed dependency spring-boot-configuration-processor

From Java

Difference between plugins and dependency in maven tool (unpack jar)

From Dev

How do I unpack AAR dependency classes in Maven?

From Dev

wrong configuration of maven war plugin

From Java

Eclipse Maven Plugin Configuration Problem

From Dev

Maven Eclipse plugin bug? failing to auto-add in-workspace dependency JAR to Run Configuration module-path

From Java

How to get the dependency tree in a Maven 3 plugin?

From Dev

Maven - Always download the latest version of dependency or plugin

From Java

How to exclude a direct dependency of a Maven Plugin

From Dev

Maven dependency plugin copy jar with dependencies

Related Related

  1. 1

    Maven-dependency-plugin (unpack-dependencies) ignores configuration

  2. 2

    maven-dependency-plugin ignores outputDirectory configuration

  3. 3

    maven dependency plugin ignores dependency versions?

  4. 4

    Maven unpack-dependencies of a plugin dependency

  5. 5

    maven-dependency-plugin:unpack Error

  6. 6

    Maven maven-dependency-plugin copy-dependencies ignores outputDirectory

  7. 7

    How to unpack just the files from a sub-folder in maven-dependency-plugin with goal unpack-dependencies?

  8. 8

    maven-dependency-plugin (goals "copy-dependencies", "unpack") is not supported by m2e

  9. 9

    maven exclude plugin in dependency

  10. 10

    Maven resources plugin dependency

  11. 11

    Maven PlugIn Dependency Exception

  12. 12

    Maven Plugin Dependency : class not found

  13. 13

    How to show dependency of a plugin in Maven?

  14. 14

    How to scan maven plugin dependency?

  15. 15

    Maven dependency management for plugin dependencies

  16. 16

    Intellij Idea ignores maven assembly plugin

  17. 17

    maven-war-plugin ignores <archiveClasses>

  18. 18

    Wildfly Maven Plugin ignores deployment name?

  19. 19

    Proguard Maven Plugin Ignores Injar Jar Name

  20. 20

    Maven requires plugin version to be specified for the managed dependency spring-boot-configuration-processor

  21. 21

    Difference between plugins and dependency in maven tool (unpack jar)

  22. 22

    How do I unpack AAR dependency classes in Maven?

  23. 23

    wrong configuration of maven war plugin

  24. 24

    Eclipse Maven Plugin Configuration Problem

  25. 25

    Maven Eclipse plugin bug? failing to auto-add in-workspace dependency JAR to Run Configuration module-path

  26. 26

    How to get the dependency tree in a Maven 3 plugin?

  27. 27

    Maven - Always download the latest version of dependency or plugin

  28. 28

    How to exclude a direct dependency of a Maven Plugin

  29. 29

    Maven dependency plugin copy jar with dependencies

HotTag

Archive