why is “test-jar” dependency required for “mvn compile”

后端 未结 4 1442
名媛妹妹
名媛妹妹 2021-02-02 13:38

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

相关标签:
4条回答
  • 2021-02-02 14:16

    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.

    0 讨论(0)
  • 2021-02-02 14:21

    So I did some serious debugging, and discovered that the problem seems to be an interaction between the exec:java plugin, test-jar dependencies and mvn compile.

    In short, if you attach exec:java to an execution phase, mvn compile starts looking for test-jar dependencies at compile time. If you remove the <executions> element from the exec:java plugin declaration, mvn compile works fine again.

    I filed a bug report for the exec:java plugin here, though I can't really tell whether the bug is in exec:java, test-jar or mvn compile so perhaps the bug will be moved somewhere else if/when someone figures that out:

    http://jira.codehaus.org/browse/MEXEC-91

    Update: It's not really a bug, the maven-exec-plugin is documented as requiring test dependencies here:

    http://mojo.codehaus.org/exec-maven-plugin/java-mojo.html

    That doesn't mean it wouldn't make a great feature. ;-)

    0 讨论(0)
  • 2021-02-02 14:27

    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.

    0 讨论(0)
  • 2021-02-02 14:29

    I am using maven2. I guess the answer is in the maven lifecycle management. The first step of a default lifecycle is validate, which does 'validate the project is correct and all necessary information is available.' (see http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html ).

    So maven just tries its best to get all the needed dependencies for the later execution.

    0 讨论(0)
提交回复
热议问题