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

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'
}

Related

Can I remove the 'jar' task in gradle build?

When I use the code below, a file of jar will generate after gradle build.
apply plugin 'java'
Is there any settings won't generate the file of jar??
I can write a custom plugins,but the code below was wrong.
dependencies {
compile project(':crm.common')
testCompile group: 'junit', name: 'junit', version: '4.12'
}
I want find a way that donot generate the file of jar.
And can run compile in dependencies.
Is there any way can do that???
You can do that by 2 ways:
explicitly exclude the jar task from execution:
gradle build -x jar
disable the jar task in build.gradle:
apply plugin: 'java'
jar.enabled = false
This worked for me:
configurations.archives.with {
artifacts.remove artifacts.find { it.toString().contains("jar") }
}

Gradle dependency for compile time only and test

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,

Gradle - unable to track down transitive dependency

I have two modules: common and domain. Domain is a dependency of common. In domain, I'm trying to add the latest version of Spring Data Elasticsearch but it keeps reverting back to an old version. My domain's build.gradle file looks like this:
domain build.gradle
apply plugin: 'spring-boot'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE")
}
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-redis")
compile("org.springframework.data:spring-data-elasticsearch:2.0.1.RELEASE")
compile 'org.slf4j:slf4j-api'
compile 'com.google.guava:guava:19.0'
compile 'com.google.code.gson:gson:2.4'
testCompile "org.mockito:mockito-core:1.+"
}
The version for elasticsearch here is 2.0.1.RELASE However, if I run dependencyInsight in common, it is retrieving 1.3.4.RELEASE instead:
gradle dependencyInsight --dependency elasticsearch --configuration compile
:common:dependencyInsight
Download https://repo1.maven.org/maven2/org/springframework/data/spring-data-elasticsearch/1.3.4.RELEASE/spring-data-elasticsearch-1.3.4.RELEASE.pom
org.elasticsearch:elasticsearch:1.5.2 (selected by rule)
\--- org.springframework.data:spring-data-elasticsearch:1.3.4.RELEASE
\--- project :domain
\--- compile
org.springframework.data:spring-data-elasticsearch:1.3.4.RELEASE (selected by rule)
org.springframework.data:spring-data-elasticsearch:2.0.1.RELEASE -> 1.3.4.RELEASE
\--- project :domain
\--- compile
common build.gradle
apply plugin: 'spring-boot'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE")
}
}
dependencies {
compile project(':domain')
compile 'com.google.code.gson:gson:2.4'
compile 'org.owasp.encoder:encoder:1.2'
compile 'com.ning:async-http-client:1.9.31'
compile 'org.slf4j:slf4j-api'
compile 'org.springframework.security:spring-security-core'
compile 'org.springframework.security:spring-security-acl:4.0.3.RELEASE'
compile 'javax.mail:javax.mail-api:1.5.4'
compile 'com.sun.mail:javax.mail:1.5.4'
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile "org.mockito:mockito-core:1.+"
}
Is there a reason why version 1.3.4.RELEASE is replacing 2.0.1.RELEASE?
You're applying Spring Boot's Gradle plugin to your common project. That means that its dependency management will be controlling the versions of the project's dependencies. To get the version of Spring Data Elasticsearch that you want, you can override Boot's dependency management by adding the following:
dependencyManagement {
dependencies {
dependency 'org.springframework.data:spring-data-elasticsearch:2.0.1.RELEASE'
}
}

Gradle: run a specific java class

My gradle file looks like this:
apply plugin: 'java'
apply plugin:'application'
mainClassName = "com.example.Main"
project.buildDir = 'target'
version = '0.1'
jar{
destinationDir=project.buildDir
}
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.+'
}
When I want to run, I type gradle run and it will execute com.example.Main class. I was wondering if there is a mechanism to execute specific class without changing the build.gradle. What I ultimately want is something similar to mvn exec:java -Dexec.mainClass="com.example.Main", where you can specify the main class.
You can set gradle project properties with -P, for example:
gradle run -PclassToExecute=com.myClass
and in the script:
mainClassName=classToExecute
If you change your build script to:
mainClassName = System.getProperty("exec.mainClass") ?: "default.Main"
You can execute a particular class with: gradle run -Dexec.mainClass=com.example.Main

Gradle Build Error

My build failed due to this error:
A problem occurred evaluating project ':DBSupport'. > Could not find
method providedCompile() for arguments [project ':Core:Platform '] on
project ':DBSupport'.
Any idea what that means?
description = 'DBSupport main component of DBSupportTool'
dependencies {
providedCompile project(':Core:Platform')
providedCompile project(':Core:Verification')
providedCompile project(':DBSupportWeb')
providedCompile project(':DBSupportEJB')
compile(group: 'commons-lang', name: 'commons-lang', version:'1.0.1') {
/* This dependency was originally in the Maven provided scope, but the project was not of type war.
This behavior is not yet supported by Gradle, so this dependency has been converted to a compile dependency.
Please review and delete this closure when resolved. */
}
compile(group: 'commons-logging', name: 'commons-logging', version:'1.0.4') {
/* This dependency was originally in the Maven provided scope, but the project was not of type war.
This behavior is not yet supported by Gradle, so this dependency has been converted to a compile dependency.
Please review and delete this closure when resolved. */
}
compile(group: 'javax', name: 'j2ee', version:'1.0') {
/* This dependency was originally in the Maven provided scope, but the project was not of type war.
This behavior is not yet supported by Gradle, so this dependency has been converted to a compile dependency.
Please review and delete this closure when resolved. */
}
I assume these modules should in fact be treated as provided (e.g. should not be packages in a WAR archive). If not just change it to compile.
providedCompile configuration is not available in Gradle out of the box. If this is a web module you can just add/apply a war plugin:
apply plugin: 'war'
If not you should be able to add this configuration manually:
configurations {
providedCompile
}
dependencies {
providedCompile project(':Core:Platform')
...
}
sourceSets.main.compileClasspath += configurations.providedCompile
sourceSets.test.compileClasspath += configurations.providedCompile
sourceSets.test.runtimeClasspath += configurations.providedCompile
There is also a propdeps-plugin which claims to do the same thing transparently.
To define a dependency provided as in Maven you need to go the following way:
project(':webgui') {
apply plugin: 'war'
dependencies {
compile project (':domain')
providedCompile 'javax:javaee-api:6.0'
}
}
or an other way would be in case of a project (module) like this:
dependencies {
compile module(":compile:1.0") {
dependency ":compile-transitive-1.0#jar"
dependency ":providedCompile-transitive:1.0#jar"
}
providedCompile "javax.servlet:servlet-api:2.5"
providedCompile module(":providedCompile:1.0") {
dependency ":providedCompile-transitive:1.0#jar"
}
runtime ":runtime:1.0"
providedRuntime ":providedRuntime:1.0#jar"
testCompile "junit:junit:4.11"
moreLibs ":otherLib:1.0"
}

Resources