如何在我的java项目中引用maven依赖的单元测试类?

Mah*_*ror 6 java junit pom.xml maven

我需要在项目A的测试包src/test/java中引用项目B中的一些JUnit测试(src/test/java),而B是A的maven依赖项.

这甚至可能吗?

<dependency>
    <groupId>XYZ</groupId>
    <artifactId>B</artifactId>
    <version>${project.version}</version>
    <type>jar</type>
    <scope>test</scope>
</dependency> 
Run Code Online (Sandbox Code Playgroud)

这两个项目都在我的控制之下.

谢谢你的建议

小智 10

你在项目B中的pom需要包含这个插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.5</version>
    <executions>
        <execution>
            <goals>
                <goal>test-jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

然后,您可以从项目A访问它,如下所示:

<dependency>
    <groupId>XYZ</groupId>
    <artifactId>B</artifactId>
    <version>${project.version}</version>
    <type>test-jar</type>
    <scope>test</scope>
</dependency> 
Run Code Online (Sandbox Code Playgroud)

将"type"更改为test-jar允许您从该依赖项访问测试类.

  • 为了它的价值,似乎依赖性中的<type> test-jar </ type>只导入测试类.因此,如果您需要常规源(可能是这种情况),则必须使用<type> jar </ type>定义相同的依赖项 (4认同)