简体   繁体   English

依赖于同一Gradle依赖项目的main和test代码

[英]Depend on main and test code for same Gradle dependency project

I maintain at least 2 Java Gradle projects. 我至少维护了2个Java Gradle项目。 Let's say that I have a common project named A and another project B that depends on A . 假设我有一个名为A的公共项目和另一个依赖于A的项目B. A is technically common to many other projects, but that isn't a minor detail to my problem. A在技​​术上与许多其他项目相同,但这不是我的问题的细节。 Anyways, the current situation is that B declares a dependency on A as a plain-old external dependency in its build.gradle file, like so: 无论如何,目前的情况是B在其build.gradle文件中声明对A的依赖是一个普通的外部依赖,如下所示:

compile group:'com.example', name: 'A', version: '0.1'

We have code specific to tests in A that we share with B by placing it in src/main/java . 我们有一个特定于A中测试的代码,我们通过将它放在src / main / java中B共享。 This code has to stay in A instead of B because there are other projects depending on A that use this test code. 此代码必须保留在A而不是B中,因为还有其他项目取决于使用此测试代码的A. I'd like to avoid putting the code in this directory because it has no purpose being deployed to production, due to its test-only nature. 我想避免将代码放在这个目录中,因为它没有专门用于生产的目的,因为它只具有测试性质。 Also, simply moving the code from A to B wouldn't be possible because other projects in my organization depend on the same code and I want to avoid code duplication. 此外,简单地将代码从A移动到B是不可能的,因为我组织中的其他项目依赖于相同的代码,我希望避免代码重复。 I would rather move this test code in A to src/test/java , but then it won't be published to A 's JAR file. 我宁愿将A中的测试代码移到src / test / java中 ,但是它不会发布到A的JAR文件中。

Thus, I am trying to pursue a solution where this code lives in src/test/java in A and is deployed in a test-only JAR file. 因此,我试图寻求一种解决方案,其中此代码位于A中的src / test / java中,并部署在仅测试的JAR文件中。 There are posts that discuss possible solutions, such as this blog post or SOF posts such as Multi-project test dependencies with gradle and Multi-project test dependencies with gradle . 有些帖子讨论了可能的解决方案,例如此博客文章或SOF帖子,例如使用gradle的 多项目测试依赖 使用gradle的 多项目测试依赖项 HOWEVER , I continuous run into a problem caused by the fact that I am not maintaining a multi-project Gradle build, but rather two completely separate single-project Gradle builds, and this cannot change. 但是 ,我不断遇到一个问题,这个问题是由于我没有维护多项目Gradle构建,而是两个完全独立的单项目Gradle构建,这不能改变。

Where I am at right now is that I am configuring build.gradle for A in the exact way suggested in the blog post, like so: 我现在所处的位置是我正在以博客文章中建议的确切方式为A配置build.gradle,如下所示:

configurations {
    testOutput.extendsFrom testCompile
}

task jarTest (type: Jar, dependsOn: testClasses) {
    from sourceSets.test.output
    classifier = 'test'
}

artifacts {
    testOutput jarTest
}

And then I have A declared as both compile and testCompile dependencies in build.gradle of B : 然后,我有一个声明为两者compiletestCompileB的依赖关系的build.gradle:

compile(group: 'com.example', name: 'A', version: '0.1')
testCompile(group: 'com.example', name: 'A', version: '0.1', configuration: 'testOutput')

This doesn't work. 这不起作用。 I am not getting any of the testCompile dependencies visible in the test classpath for B ; 我没有在B的测试类路径中看到任何testCompile依赖项; these testCompile dependencies are not being reported by gradle dependencies nor are recognized by my IDE's code search (IntelliJ). 这些testCompile依赖项不是由gradle dependencies报告的,也不是由我的IDE代码搜索(IntelliJ)识别的。 I threw up a Hail Mary and even tried to replace configuration: 'testOutput' with classifier: 'test' , but to no avail. 我抛出了一个Hail Mary,甚至尝试用classifier: 'test'替换configuration: 'testOutput' classifier: 'test' ,但无济于事。 Gradle documentation doesn't quite seem to help either, as it seems that my use case isn't really covered in their intended use cases. Gradle文档似乎也没有帮助,因为我的用例似乎并未真正涵盖在他们的预期用例中。

Any way I can achieve my desired usage of Gradle, or am I stuck with exporting test-only code in the main JAR file of A ? 我可以用任何方式实现我想要的Gradle用法,或者我在A的主JAR文件中导出仅测试代码? All help much appreciated. 所有人都非常感谢。

I would treat code that is only needed during testing exactly the same as libraries that are only used during testing (eg JUnit or Mockito): The code should be in a separate module with its own name. 我会将测试期间仅需要的代码与仅在测试期间使用的库完全相同(例如JUnit或Mockito):代码应该位于具有自己名称的单独模块中。

So I suggest that you split A into two modules (of the same multi-project build): 所以我建议你将A分成两个模块(相同的多项目构建):

  • A 一个
  • A-test-support A-测试支持

The dependencies of A would have to change to: A的依赖关系必须改为:

dependencies {
    testCompile project(':A-test-support')
}

In B you have to use these dependencies: B中,您必须使用这些依赖项:

dependencies {
    compile(group: 'com.example', name: 'A', version: '0.1')
    testCompile(group: 'com.example', name: 'A-test-support', version: '0.1')
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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