I am basically looking for a way to mimic the maven dependency provided
. I am building a jar (an extension to a db driver), which depends on another jar (the db driver), but I do not want to include that jar.
I am able to use compileOnly
to achieve that, however now the tests won't run or compile as the required jar is not included in tests.
I tried through the list of available dependencies like testCompile
, however I could not find one that makes the jar available at compile time and when the tests run and compile.
How would I include that jar properly?
Edit: As requested, the build.gradle file:
group 'com.mygroup'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compileOnly "org.mongodb:mongodb-driver:3.3.0"
testCompile "org.mongodb:mongodb-driver:3.3.0"
}
Listing the dependency twice does work, however obviously is not a very nice solution
compileOnly dependencies are available while compiling but not when running them. This is equivalent to the provided scope in maven. It means that everyone who wants to execute it needs to supply a library with all classes of the CompileOnly library.
The compileOnly configuration is used to itemize a dependency that you need to compile your code, same as compile above. The difference is that packages your java code use from a compileOnly dependency will not be listed as Import-Package manifest entries.
Configuration inheritance and composition For example the testImplementation configuration extends the implementation configuration. The configuration hierarchy has a practical purpose: compiling tests requires the dependencies of the source code under test on top of the dependencies needed write the test class.
By default, Gradle will run all tests that it detects, which it does by inspecting the compiled test classes. This detection uses different criteria depending on the test framework used. For JUnit, Gradle scans for both JUnit 3 and 4 test classes.
You can extend your testCompile
configuration from the compileOnly
configuration:
configurations {
testCompile.extendsFrom compileOnly
}
I use the following;
sourceSets {
// Make the compileOnly dependencies available when compiling/running tests
test.compileClasspath += configurations.compileOnly
test.runtimeClasspath += configurations.compileOnly
}
which is a line longer than the answer from tynn, but makes the intent clearer IMHO,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With