I am currently using Gradle to build a project, and it has a dependency on a third party component that I need at compile time, but which will be provided at runtime. In maven I would declare this dependency as provided, and in Gradle I declare it as below:
compileOnly group: 'org.apache.spark', name: 'spark-sql_2.11', version: '2.4.0.cloudera1'
The fact that I am depending on the above spark artifact is not really germane to the question (I provide this info to make the example more concrete.)
Now, suppose I want to write some unit tests for my app (or library as the case may be). When using Gradle, the only way I figured out how to do this is clunky: I redeclare the dependency as a testCompile dependency, and my test is able to run:
compileOnly group: 'org.apache.spark', name: 'spark-sql_2.11', version: '2.4.0.cloudera1'
testCompile group: 'org.apache.spark', name: 'spark-sql_2.11', version: '2.4.0.cloudera1'
I really don't like the repetition and clutter of declaring my dependency twice, and I'm wondering if there is any better way to do this in Gradle ?
Conclusion
The answer from Mike put me on to the solution that I chose, which was to put this at the top level gradle file of my multi-project build.
subprojects {
sourceSets {
test.compileClasspath += configurations.compileOnly
test.runtimeClasspath += configurations.compileOnly
}
}
compileOnly
resolution, which will be triggered by your approach should emit a deprecation warning and become illegal in an upcoming Gradle version.