Maven 建议不要使用测试罐,但我最终会产生循环依赖
Maven recommends not using test-jars, yet I would end up with a cyclic dependency
我有 2 个模块,结构如下
- 库
- src/main/java
- 我的界面
- src/test/java
- MyMockObject 实现 MyInterface
- MyTest1 - 使用 MyMockObject
- 主要依赖于:-lib test-jar 范围:test & -lib
- src/test/java
- MyTest2 - 使用 MyMockObject
目前这很好,因为我的主模块会添加对 lib 的 test-jar 模块的依赖,但是如此处所示 https://maven.apache.org/plugins/maven-jar-plugin/examples/create-test-jar.html 建议改为创建一个 -test 模块并公开它,而不是使用测试罐。
然而,在我的情况下,这将导致如下循环依赖:
- 测试-dependsOn-lib
- src/main/java
- MyMockObject 实现 MyInterface
- lib - dependsOn: -test 作用域测试
- src/main/java
- 我的界面
- src/test/java
- MyTest1 使用 MyMockObject
- main dependsOn: -test scope test & -lib
- src/test/java
- MyTest2 - 使用 MyMockObject
在这种情况下,如何为 maven 保持一个有组织的结构?
Mavens 推荐不使用 test-jar 依赖的原因是
The downside of this solution is that you don't get the transitive test-scoped dependencies automatically. Maven only resolves the compile-time dependencies, so you'll have to add all the other required test-scoped dependencies by hand.
因此,如果这不是问题,只需使用 test-jar 依赖项即可。
您也可以只将那些测试 类 和资源打包到您真正想要共享的测试罐中:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
<configuration>
<includes>
<!-- include only test resources from that package--> <include>**/com/prefabwarek/web/ui/page/include/*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
我有 2 个模块,结构如下
- 库
- src/main/java
- 我的界面
- src/test/java
- MyMockObject 实现 MyInterface
- MyTest1 - 使用 MyMockObject
- src/main/java
- 主要依赖于:-lib test-jar 范围:test & -lib
- src/test/java
- MyTest2 - 使用 MyMockObject
- src/test/java
目前这很好,因为我的主模块会添加对 lib 的 test-jar 模块的依赖,但是如此处所示 https://maven.apache.org/plugins/maven-jar-plugin/examples/create-test-jar.html 建议改为创建一个 -test 模块并公开它,而不是使用测试罐。
然而,在我的情况下,这将导致如下循环依赖:
- 测试-dependsOn-lib
- src/main/java
- MyMockObject 实现 MyInterface
- src/main/java
- lib - dependsOn: -test 作用域测试
- src/main/java
- 我的界面
- src/test/java
- MyTest1 使用 MyMockObject
- src/main/java
- main dependsOn: -test scope test & -lib
- src/test/java
- MyTest2 - 使用 MyMockObject
- src/test/java
在这种情况下,如何为 maven 保持一个有组织的结构?
Mavens 推荐不使用 test-jar 依赖的原因是
The downside of this solution is that you don't get the transitive test-scoped dependencies automatically. Maven only resolves the compile-time dependencies, so you'll have to add all the other required test-scoped dependencies by hand.
因此,如果这不是问题,只需使用 test-jar 依赖项即可。 您也可以只将那些测试 类 和资源打包到您真正想要共享的测试罐中:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
<configuration>
<includes>
<!-- include only test resources from that package--> <include>**/com/prefabwarek/web/ui/page/include/*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>