Gradle dependency for compile time only and test - gradle

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

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,

Related

IntelliJ doesn't recognize dependencies downloaded via Gradle

I'm having a problem importing dependencies into my code in IntelliJ
My build.gradle file is as follows:
plugins {
id 'groovy'
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.codehaus.groovy:groovy-all:2.3.11'
testImplementation group: 'junit', name: 'junit', version: '4.12'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
}
test {
useJUnitPlatform()
}
Gradle doesn't seem to struggle to find and download the dependencies as I can find them locally in
C:\Users%USER%.gradle\caches\modules-2\files-2.1\com.squareup.retrofit2\retrofit\2.9.0
I've updated IntelliJ to make sure it's up-to-date and have also tried invalidating cache and restarting several times. I'm out of ideas at this point, any help would be kind.

Starting with IntelliJ and Gradle : how to do jUnit tests?

Good evening,
because I want to initiate myself to LibGDX, I recently gave a try to IntelliJ Idea IDE and Gradle instead of my old Eclipse-Maven habits.
I have to recognize that such a change is not easy because I really don't find anything.
To start learning I created a project with a simple Pojo and also a unit test class.
I have no error in the editor, both Pojo and jUnit seem OK, but when I launch the unit test, I get such errors :
Can someone help me understand what's going wrong ?
EDIT : build.gradle file content :
plugins {
id 'java'
}
group 'com.citizenweb.training'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
// https://mvnrepository.com/artifact/org.projectlombok/lombok
compile group: 'org.projectlombok', name: 'lombok', version: '1.18.16'
}
Thanx by advance.
It seems you did not configure lombok dependencies properly: your test classes cannot see lombok-generated stuff (getters, setters, build). Lombok is based on annotation processor so you need to declare following dependencies in your build.gradle :
ext {
lombokVersion = "1.18.6"
}
dependencies {
// Lombok
compileOnly ("org.projectlombok:lombok:${lombokVersion}")
annotationProcessor ("org.projectlombok:lombok:${lombokVersion}")
// to make lombok available for test classes
testCompileOnly ("org.projectlombok:lombok:${lombokVersion}")
testAnnotationProcessor ("org.projectlombok:lombok:${lombokVersion}")
testImplementation("junit:junit:4.12")
}

What is the proper Gradle setup for a Kotlin Project to compile to MacOSX?

I am having difficulty combing through the Kotlinlang and Gradle documentation. There seems to be a confusing mix of examples on how to make a native compiling kotlin project. I was looking at this tutorial:
https://kotlinlang.org/docs/tutorials/native/gradle-for-kotlin-native.html
but it seems to be wildly different from this other gradle setup:
https://kotlinlang.org/docs/reference/using-gradle.html#configuring-dependencies
I generated a new Kotlin-native-common project in Intellij and the resulting auto-generated build script looks like this:
plugins {
id 'java'
id 'kotlin-platform-common' version '1.2.71'
}
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-common"
testCompile "org.jetbrains.kotlin:kotlin-test-annotations-common"
testCompile "org.jetbrains.kotlin:kotlin-test-common"
testCompile group: 'junit', name: 'junit', version: '4.12'
}
The above build-script doesn't look like the examples given in the above tutorials. What exactly should this build-script look like to compile to MacOSX native and why does the tutorial example show using konan for native compiling while the auto-generated script does not?
Thanks

How do I view the dependency tree for Gradle's buildSrc?

I can normally view a dependency tree for a project by running ./gradlew dependencies, but I cannot figure out how to view the dependency tree for the Gradle buildSrc directory.
I have tried accessing it as a sub project, ./gradlew buildSrc:dependencies but that does not work.
build.gradle (for buildSrc)
repositories { mavenCentral() }
dependencies {
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile 'junit:junit:4.12'
}
Consider the following (the only way that I know of):
$ cd buildSrc
$ gradle dependencies
Note, given your build.gradle example, that buildSrc is its own project in Gradle and needs a proper file. Your example doesn't declare any plugins. Assuming you are using Java, a fix is:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile 'junit:junit:4.12'
}

Confused with Gradle Dependencies

I am trying to add my dependencies to Gradle Dependencies library in eclipse, and when I run this, it downloads these dependencies, however my other dependencies are in the Gradle Dependencies folder under Gradle Project in eclipse but this one is not. Please help, I just need to add a Gradle Dependency in eclipse.
repositories {
mavenCentral()
maven {
url "http://clojars.org/repo"
}
}
dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
compile "org.clojars.jmeeks:jfugue-with-musicxml:4.0.3"
testCompile group: 'junit', name: 'junit', version: '4.+'
}
You should tell to gradle about your local repository adding mavenLocal() first in repositories section. Using your current configuration you are telling to gradle that everything is stored in mavenCentral or the custom repo.

Resources