Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle dependency for compile time only and test

Tags:

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

like image 771
st-h Avatar asked Jul 22 '16 15:07

st-h


People also ask

What is compile time dependency in Gradle?

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.

What is difference between compile and compileOnly Gradle?

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.

What is testImplementation in Gradle?

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.

Does Gradle build run tests?

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.


2 Answers

You can extend your testCompile configuration from the compileOnly configuration:

configurations {
    testCompile.extendsFrom compileOnly
}
like image 136
tynn Avatar answered Sep 27 '22 19:09

tynn


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,

like image 36
gdt Avatar answered Sep 27 '22 18:09

gdt