如何在依赖项目 A 的项目 B 中包含测试并让 Maven 识别它们 "as is"
How to include tests in project B from dependent project A and have them recognized by Maven "as is"
我有一个 (Maven) 项目 B 依赖于项目 A。项目 A 将其测试打包到一个 jar 中,如 here 所述。假设我在项目 A 中有一个测试 class com.forelight.a.FooTest
。 class 在项目 B 的测试范围 class 路径上可见,但不会由 [= 自动执行13=]。我可以像这样在项目 B 的 test/main/java
目录中扩展 FooTest:
package com.forelight.b;
public class FooBarTest extends com.forelight.a.FooTest {}
这完成了工作(mvn test
在命令行和 eclipse 下运行它)但感觉很笨拙。
这是一个有效的自动化解决方案:
- 项目 A 还应提供其 test-sources jar
- 项目 B 应在测试范围内导入项目 A,并在测试范围内导入项目 A 测试源
- 项目 B 将使用 Maven 依赖插件的
unpack-dependencies
自动将测试源 jar 解压到目标文件夹的子文件夹中(比如 project-a-test-sources
)
- 项目 B 将使用 Build Helper Maven 插件的
add-test-source
目标自动添加解压源作为项目 A 中的测试源
- Maven 然后将编译 运行 添加的源作为项目 B 测试的一部分
要实现它,在项目 A 中将以下内容添加到构建部分:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
这实际上会创建一个新的 jar 作为提供测试源的构建的一部分。记得通过 mvn install
.
安装
在项目 B 中,将以下内容添加到依赖项中:
<dependency>
<groupId>com.sample</groupId>
<artifactId>project-a</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sample</groupId>
<artifactId>project-a</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>test</scope>
<classifier>test-sources</classifier>
</dependency>
这样类路径将被项目A填充,第二个依赖是无害的,它会被下面的插件执行使用。
在项目 B 中,还将以下内容添加到构建部分:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>unpack-test-sources</id>
<phase>generate-test-sources</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeGroupIds>com.sample</includeGroupIds>
<includeArtifactIds>project-a</includeArtifactIds>
<includeScope>test</includeScope>
<includeClassifiers>test-sources</includeClassifiers>
<outputDirectory>
${project.build.directory}/project-a-test-sources
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.10</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/project-a-test-sources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
我们在这里解压源并将它们添加为测试源。
Maven 将自动执行添加的测试。
关于这种方法的一些注意事项:
- 它可能看起来像您在问题中提到的方法一样笨拙,即使它是自动化的并且不需要创建新的扩展测试
- 这绝对不是标准的东西,但原来的要求听起来也不像标准做法
- 您可能在测试名称或资源名称上存在冲突(同样,因为这不是标准方法)
- 您可能不想 运行 这些外部测试作为默认构建的一部分,在这种情况下,您可以将上面的配置移动到 Maven profile,比如
run-project-a-tests
并执行他们只是根据 -Prun-project-a-tests
的愿望。这也将使您的默认构建更快(并且更标准)。
我有一个 (Maven) 项目 B 依赖于项目 A。项目 A 将其测试打包到一个 jar 中,如 here 所述。假设我在项目 A 中有一个测试 class com.forelight.a.FooTest
。 class 在项目 B 的测试范围 class 路径上可见,但不会由 [= 自动执行13=]。我可以像这样在项目 B 的 test/main/java
目录中扩展 FooTest:
package com.forelight.b;
public class FooBarTest extends com.forelight.a.FooTest {}
这完成了工作(mvn test
在命令行和 eclipse 下运行它)但感觉很笨拙。
这是一个有效的自动化解决方案:
- 项目 A 还应提供其 test-sources jar
- 项目 B 应在测试范围内导入项目 A,并在测试范围内导入项目 A 测试源
- 项目 B 将使用 Maven 依赖插件的
unpack-dependencies
自动将测试源 jar 解压到目标文件夹的子文件夹中(比如project-a-test-sources
) - 项目 B 将使用 Build Helper Maven 插件的
add-test-source
目标自动添加解压源作为项目 A 中的测试源 - Maven 然后将编译 运行 添加的源作为项目 B 测试的一部分
要实现它,在项目 A 中将以下内容添加到构建部分:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
这实际上会创建一个新的 jar 作为提供测试源的构建的一部分。记得通过 mvn install
.
在项目 B 中,将以下内容添加到依赖项中:
<dependency>
<groupId>com.sample</groupId>
<artifactId>project-a</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sample</groupId>
<artifactId>project-a</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>test</scope>
<classifier>test-sources</classifier>
</dependency>
这样类路径将被项目A填充,第二个依赖是无害的,它会被下面的插件执行使用。
在项目 B 中,还将以下内容添加到构建部分:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>unpack-test-sources</id>
<phase>generate-test-sources</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeGroupIds>com.sample</includeGroupIds>
<includeArtifactIds>project-a</includeArtifactIds>
<includeScope>test</includeScope>
<includeClassifiers>test-sources</includeClassifiers>
<outputDirectory>
${project.build.directory}/project-a-test-sources
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.10</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/project-a-test-sources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
我们在这里解压源并将它们添加为测试源。
Maven 将自动执行添加的测试。
关于这种方法的一些注意事项:
- 它可能看起来像您在问题中提到的方法一样笨拙,即使它是自动化的并且不需要创建新的扩展测试
- 这绝对不是标准的东西,但原来的要求听起来也不像标准做法
- 您可能在测试名称或资源名称上存在冲突(同样,因为这不是标准方法)
- 您可能不想 运行 这些外部测试作为默认构建的一部分,在这种情况下,您可以将上面的配置移动到 Maven profile,比如
run-project-a-tests
并执行他们只是根据-Prun-project-a-tests
的愿望。这也将使您的默认构建更快(并且更标准)。