Sharing src/test classes with Maven without versio

2019-05-15 10:14发布

问题:

I'm sharing src/test classes between number of modules, in a similar way described in attaching tests guide and the following question.

So, I have the following pom.xml dependencies:

       <dependency>
            <groupId>com.myco.app</groupId>
            <artifactId>foo</artifactId>
        </dependency>

        <dependency>
            <groupId>com.myco.app</groupId>
            <artifactId>foo</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <type>test-jar</type>
            <scope>test</scope>
        </dependency>

BUT, in opposite to the question above, when attaching the test-jar, i don't want to specify the specific test-jar version. As in the compile level dependency:

   <dependency>
      <groupId>com.myco.app</groupId>
      <artifactId>foo</artifactId>
      <type>test-jar</type>
      <scope>test</scope>
    </dependency>

In this case, my pom.xml become erroneous with message about the missing version. Why is this happen? Why i can specify dependency without versions but not the test-jar one? Is there a way to overcome this and make the test-jar to use the latest jar it can find?

回答1:

The reason the main code dependency could be used without the version, is the existence of a "main pom" in our project that automatically generates appropriate version for each dependency in section. Therefore, each dependency can be specified without specific version number.

Test-jar dependency on the other hand, don't have it's version defined anywhere else in the transitive dependency so, the specific version must be specified.