Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is "test-jar" dependency required for "mvn compile"

I'm having trouble using test-jar dependencies in a multi-module project. For example, when I declare that the cleartk-syntax module depends on the cleartk-token module's test-jar like this (the full code is here):

<modelVersion>4.0.0</modelVersion>
<groupId>org.cleartk</groupId>
<artifactId>cleartk-syntax</artifactId>
<version>0.5.0-SNAPSHOT</version>
<name>cleartk-syntax</name>
...
<dependencies>
    ...
    <dependency>
        <groupId>org.cleartk</groupId>
        <artifactId>cleartk-token</artifactId>
        <version>0.7.0-SNAPSHOT</version>
        <type>test-jar</type>
        <scope>test</scope>
    </dependency>

I get the following error if I run mvn compile using maven 2:

[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Failed to resolve artifact.

Missing:
----------
1) org.cleartk:cleartk-token:test-jar:tests:0.7.0-SNAPSHOT

If I use maven 3 I get the error:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.654s
[INFO] Finished at: Mon Jan 24 21:19:17 CET 2011
[INFO] Final Memory: 16M/81M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project cleartk-syntax: Could not resolve
dependencies for project org.cleartk:cleartk-syntax:jar:0.5.0-SNAPSHOT: Could
not find artifact org.cleartk:cleartk-token:jar:tests:0.7.0-SNAPSHOT

In the latter case, I'm particularly confused because I would have thought it should be looking for an artifact of type test-jar not of type jar.

With maven 2 or maven 3, I can get it to compile by running mvn compile package -DskipTests. With maven 3, I can also get it to compile by running mvn compile test-compile.

But why is either maven 2 or maven 3 looking for a test-jar dependency during the compile phase? Shouldn't it wait until the test-compile phase to look for such dependencies?

Update: The answer was that the maven-exec-plugin, used during my compile phase, requires dependency resolution of artifacts in scope:test. I've created a feature request to remove the scope:test dependency.

like image 767
Steve Avatar asked Jan 24 '11 20:01

Steve


People also ask

What is test dependency in Maven?

A test -scoped dependency is a dependency that is available on the classpath only during test compilation and test execution. If your project has war or ear packaging, a test -scoped dependency would not be included in the project's output archive.

What is classifier in Maven POM?

classifier: The classifier distinguishes artifacts that were built from the same POM but differ in content. It is some optional and arbitrary string that - if present - is appended to the artifact name just after the version number.

What are Maven coordinates?

Maven coordinates define a set of identifiers which can be used to uniquely identify a project, a dependency, or a plugin in a Maven POM.


2 Answers

In my case the root cause was that the module which should be used as a dependency in test scope with type test-jar did not include the required maven-jar-plugin configuration. Without the snippet below no test jar will be deployed when you call mvn deploy on the respective module.

<build>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
      <execution>
        <goals>
          <goal>test-jar</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>

See https://maven.apache.org/guides/mini/guide-attached-tests.html for more details.

like image 165
Tobias Meyer Avatar answered Oct 04 '22 01:10

Tobias Meyer


This looks like a definite bug to me.

I have the same problem and tested Maven 3.0.1 and 3.0.2. Validate doesn't fail, only the compile step fails. With Maven 3 mvn compile breaks but mvn test-compile works.

It appears that the compile phase is looking for test-jar artifacts in the reactor and then repo, but it shouldn't since the dependency is in test scope. Test scope artifacts should be resolved during test-compile, not compile.

As a result, I thought this could be worked around by mapping the maven-compiler-plugin's testCompile goal to the compile phase, instead of the default test-compile phase.

I added this to my pom, right next to the part that adds the test-jar creation in the upstream pom:

  <!-- there is a bug in maven causing it to resolve test-jar types
       at compile time rather than test-compile. Move the compilation 
       of the test classes earlier in the build cycle -->
  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <executions>
      <execution>
        <id>default-testCompile</id>
        <phase>compile</phase>
        <goals>
          <goal>testCompile</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

But that won't work either because the five phases between compile and test-compile haven't run and set up things like the test classpath.

I guess the real workaround until this bug is fixed is to use test-compile in place of compile.

like image 32
Scott Carey Avatar answered Oct 03 '22 23:10

Scott Carey