8

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?

4

3 回答 3

6

我认为这些插件配置对你有用。

只需在资源准备和编译中将跳过覆盖为 false 即可。

  <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>
于 2013-09-04T02:56:10.143 回答
4

您的问题归结为多模块构建中的依赖关系解析。它与测试代码没有特别的关系。

我有这个设置。一个通用模块包含运行时代码以及共享测试代码(测试替身和模拟等)。测试代码被其他模块中的测试使用。它对我们非常有效。

“mvn compile”只编译运行时代码。

在父级(反应堆构建)运行“mvn test-compile”、“mvn test”或“mvn package”一切正常。反应堆可以解决所有问题。

如果您在模块级别运行构建,则该模块的所有依赖项都必须在 repo 中。这意味着您必须事先为每个依赖模块运行“mvn install”。此规则同样适用于常规依赖项和测试依赖项。

如果您希望它会跟随父链接到父模块并向下到其他模块,我不得不让您失望。父链接仅用于继承 pom 中的常用设置,不用于依赖解析。

就我个人而言,我几乎总是从父级构建完整的反应堆。如果我之前在父级别运行过 mvn install 并且我知道其他模块没有更改,我只会构建单个模块。

希望有帮助。

于 2012-08-24T09:48:01.473 回答
1

我坚信测试应该只是一个模块的一部分。您不应该依赖其他模块中的测试。如果您将测试更新为不同的行为,则很难预测会发生什么。

如果您需要共享通用测试数据或通用测试类,那么最好使用该共享测试源创建一个单独的模块。然后让所有测试都依赖于具有范围测试的共享测试jar。

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

确保你只依赖common-test-utilwith<scope>test</scope>然后你就可以打电话了

mvn test

在顶层,所有测试都将运行。

于 2012-08-24T08:02:02.310 回答