简体   繁体   中英

How to run maven compile phase with test-jar dependencies

The project I'm working on is made of multiple modules, being built with maven. The test code in some modules has dependencies on test code from other modules. These dependencies are declared as below.

In the dependency module:

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

In the module which has the dependency on the previous module:

<dependency>
    <groupId>${project.groupId}</groupId>
    <artifactId>themodulename</artifactId>
    <version>${project.version}</version>
    <type>test-jar</type>
    <scope>test</scope>
</dependency>

Using this configuration, the maven install phase can be executed successfully. But trying to run the compile or test phase fails, because the test jar file dependency cannot be resolved.

Looking at the test-jar goal, it seems to be configured to run by default during the package phase, which I think is the cause of the problem.

Then, I tried to force this goal to run during the compile phase, by modifying the first config into:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.2</version>
    <executions>
        <execution>
            <phase>compile</phase>
            <goals>
                <goal>test-jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Looking at the debug logs, I can see that the goal is now executed during the compile phase, but also this:

[WARNING] JAR will be empty - no content was marked for inclusion!

I tried to configure the includes to **/* and confirmed that the default testClassesDirectory was set to the right one, but I still get the same warning.

I could see that the test-classes folder didn't exist after running the compile phase, which seems normal, but even though it exists after running the test phase, and it contains files, I still get the "JAR will be empty" warning.

Does anyone have any idea on fixing this configuration so that I can run successfully the compile or test phase?

I think that these plugin configuration works for you.

Just overwrite the skip to false in resources preparation and compiling.

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <executions>
      <execution>
        <id>default-testResources</id>
        <configuration>
          <skip>false</skip>
        </configuration>
        <phase>process-test-resources</phase>
        <goals>
          <goal>testResources</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <executions>
      <execution>
        <id>default-testCompile</id>
        <configuration>
          <skip>false</skip>
        </configuration>
        <phase>test-compile</phase>
        <goals>
          <goal>testCompile</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
      <execution>
        <configuration>
          <skip>false</skip>
        </configuration>
        <goals>
          <goal>test-jar</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

Your issue is down to dependency resolution in multi-module builds. It is not specifically related to test code.

I have exactly this setup. A common module contains runtime code along with shared test code (test doubles & mocks etc). The test code is used by tests in other modules. It works very well for us.

"mvn compile" only compiles runtime code.

Running an "mvn test-compile", "mvn test" or "mvn package" at the parent level (reactor build) everything works perfectly. The reactor can sort it all out.

If you run a build at the module level then all the depedencies of that module have to be in a repo. This means that you must have prevously run an "mvn install" for each of the dependent modules. This rule applies equally to both regular dependencies and test ones.

If you were hoping that it would follow the parent link up to the parent and down to the other modules I have to disappoint you. The parent link is only used to inherit common settings in the pom and not for dependency resolution.

Personally I almost always do a full reactor build from the parent. I only do an individual module build if I have previously run an mvn install at the parent level and I know the other modules haven't changed.

Hope that helps.

I strongly believe that tests should only be part of one module. You should not depend on tests in other modules. It is very hard to predict what happens if you update the tests to behave differently.

If you need to share common test data or common test classes then it is better to create a separate module with that shared test source. And then let all tests have a dependency on the shared test-jar with scope test.

+-- MyProject
+-- common-test-util
|   +-- src
|   |    +-- main
|   |        +-- java
|   +-- pom.xml
+-- moduleA
|   +-- src
|   |    +-- main
|   |    |   +-- java
|   |    +-- test
|   |        +-- java
|   +-- pom.xml
+-- moduleB
|   +-- src
|   |    +-- main
|   |    |   +-- java
|   |    +-- test
|   |        +-- java
|   +-- pom.xml
+-- pom.xml

Make sure that you only depend on common-test-util with <scope>test</scope> and then you will be able to call

mvn test

on the top level and all tests will run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM