How to add Project Dependency using gradle? - gradle

I am facing the following problem. I need to build the multi-project, but when I try to add the dependency I got the following error.
Root Project's settings.gradle:
include 'DbServices','HelloWeb'
Root Project's build.gradle:
apply plugin: 'java'
project(':HelloWeb') {
dependencies {
compile (':DbServices')
}
}
DbServices's build.gradle:
apply plugin: 'java'
HelloWeb's build.gradle:
apply plugin: 'java'
However, then I am getting the following error when synchronize with the gradle tasks on Root Project:
Executing command: "tasks"
FAILURE: Build failed with an exception.
Where:
Build file 'D:\MyDeployment\RootProject\build.gradle' line: 21
What went wrong:
A problem occurred evaluating root project 'RootProject'.
Could not find method compile() for arguments [:DbServices] on org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler_Decorated#3b3e3b3e.
Try:
Run with --info or --debug option to get more log output.
Exception is:
org.gradle.api.GradleScriptException: A problem occurred evaluating root project 'RootProject'.

The documentation uses the following syntax:
dependencies {
compile project(':DbServices')
}

The notion of compile dependencies is introduced by the java plugin. Therefore, this plugin has to be applied (to the HelloWeb project) before declaring dependencies. The easiest way to fix this is to move the dependencies block into HelloWeb's build.gradle. Additionally, the project dependency syntax needs to be fixed according to JB Nizet's answer.

Related

Gradle build problem: Could not get unknown property 'libraries' in Caffeine Simulator

I used to work on my fork of Caffeine’s Simulator using Eclipse, and the project was compiled and built OK.
Suddenly I started getting the following error while running Gradle build:
Could not get unknown property 'libraries' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
I indeed don’t understand the syntax “implementation libraries.X” in Caffeine's Simulator build.gradle dependencies.
To study the problem, I generated the project MWE in Eclipse, with the following build.gradle:
plugins {
id 'java-library'
}
repositories {
jcenter()
}
dependencies {
api 'org.apache.commons:commons-math3:3.6.1'
implementation 'com.google.guava:guava:28.2-jre'
testImplementation 'junit:junit:4.12'
implementation libraries.xz
}
This MWE gives the same error.
Once removing the line implementation libraries.xz
the project builds OK.
I guess that "libraries" is a property / variable defined somewhere in Caffeine, but fail to find it.
The manifest of dependencies is defined in dependencies.gradle. The root build file applies this to all of the subprojects,
subprojects {
apply plugin: 'biz.aQute.bnd.builder'
apply plugin: 'java-library'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply from: "${rootDir}/gradle/publish.gradle"
apply from: "${rootDir}/gradle/codeQuality.gradle"
apply from: "${rootDir}/gradle/dependencies.gradle"
...
}
The central manifest allows for referring to the dependency more abstractly, so that versions can be upgraded globally and reduces copy-and-paste issues.
It sounds like a local change to the build causes it to no longer import the manifest and the property cannot be resolved.

Getting gradle error "Could not get unknown property 'srcDirs' for source set 'groovy'" after upgrading to 5.5.1

I have a multi-project build and I'm including the groovy plugin in the master build.gradle like this:
plugins {
id 'groovy'
}
However, one of my subprojects which has groovy sources is doing this:
sourceSets {
main {
java.srcDirs = []
groovy.srcDirs += ["src/main/java", "src/main/groovy"]
}
}
And I get an error:
> Could not get unknown property 'srcDirs' for source set 'groovy' of type org.gradle.api.internal.tasks.DefaultSourceSet.
I'm in the process of upgrading my build scripts to work under gradle v5.5.1 rather than v2.13. This was working previously where I had this in the subproject build file:
apply plugin: 'groovy'
But after upgrading to 5.5.1 from 2.13 it started giving me this:
* What went wrong:
A problem occurred evaluating project ':mysubproj'.
> Failed to apply plugin [class 'org.gradle.api.plugins.GroovyBasePlugin']
> Cannot change attributes of configuration ':mysubproj:apiElements' after it has been resolved
Which is why I thought I should declare the plugin in the master build script.
Shouldn't my plugin reference in the master build script make the subproject aware of the groovy extensions? If not, what's the correct way? Thank you!
The issue is the Groovy plugin isn't being applied to the subproject, you will want to apply it appropriately.
allprojects {
apply plugin: 'groovy'
}
I know in the past the new plugin style didn't work for subprojects, that may have been resolved and if so - use it.
As for the exception, when you seem to be doing the above, I'd need the buildscripts to advise you :)

Plugin with id 'org.ajoberstar.grgit' not found

I'm trying to compile an opensource minecraft mod called VeinMiner.
I use ./gradlew build to compile it.
However it failed and give me this reason:
FAILURE: Build failed with an exception.
* Where:
Build file '/home/xxxx/Desktop/VeinMiner/build.gradle' line: 26
* What went wrong:
A problem occurred evaluating root project 'VeinMiner'.
> Plugin with id 'org.ajoberstar.grgit' not found.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 14.778 secs
I have tried to git clone it again or run it in sudo.
However, neither of these two work.
This build gradle is like this:
(It's too long so I select some part of it)
buildscript {
repositories {
mavenCentral()
maven {
name = "forge"
url = "http://files.minecraftforge.net/maven"
}
maven {
name = "sonatype"
url = "https://oss.sonatype.org/content/repositories/snapshots/"
}
}
dependencies {
classpath 'net.minecraftforge.gradle:ForgeGradle:1.2-SNAPSHOT'
}
}
plugins {
id 'com.matthewprenger.cursegradle' version '1.0.7'
id 'org.ajoberstar.grgit' version '1.3.2'
}
apply plugin: 'forge'
apply plugin: 'maven'
apply plugin: "org.ajoberstar.grgit"
apply plugin: "com.matthewprenger.cursegradle"
ext.git = grgit.open(file('.'))
ext {
configFile = file "build.properties"
revision = git.head().abbreviatedId
depth = git.log().size()
}
I expected to Build Successful. Have anyone met this situation?
The mentioned plugin org.ajoberstar.grgit gets applied twice. The first time it gets applied via the new plugins block, the second time via the old apply plugin: method. Simply remove the line apply plugin: "org.ajoberstar.grgit" and the error should disappear.
To apply plugins via the old apply plugin: method, the plugin package needs to be resolved from an arbitrary repository inside the buildscript block. This works in the same way as resolving project dependencies via the normal repositories and dependencies blocks.
The new plugins block directly resolves plugins from the Gradle plugin portal and applies them in the same step.
I would guess that the way how to apply to plugin was changed to the newer one, but the removal of the old method did not happen. In existing setups the plugin may have been resolved from the local Gradle cache, but on your new setup it could not be found.

Gradle: Dependency insight report cannot be generated because the input configuration was not specified

I'm trying to resolve dependency conflict in my gradle applicaiton. My top-level build.gradle is:
archivesBaseName = 'message-sender-rest'
apply plugin: 'paas-publish'
dependencies {
compile(project(':message-sender-api'))
compile(libraries.springBoot)
compile(libraries.loggingRuntime)
compile(libraries.integration)
compile(libraries.serviceFrameworkServer)
compile(libraries.serviceFrameworkApp)
compile(libraries.serviceFrameworkSpringIntegration)
testCompile(libraries.testing)
testCompile(libraries.springTest)
testCompile(libraries.activeMqBroker)
}
When I try to run gradle dependencyInsight --configuration compile, I get the following error:
* What went wrong:
Configuration with name 'compile' not found.
There is a whole bunch of lower level gradle files, but I guess that should just work using the top-level one, isn't it?
When i just try gradle dependencies, it returns pretty much nothing:
gradle dependencies
dockerBuild tag: dev.docker.orbitbenefits.capita/orbit/message-sender-rest:bOD-9656-ConfigServer-n0
:dependencies
------------------------------------------------------------
Root project
------------------------------------------------------------
No configurations
BUILD SUCCESSFUL
Any idea what I may be missing?
I was not specifying the module I wanted to inspect:
The project had a sub-project (module message-sender-server). Specifying the module name actually worked:
gradlew message-sender-server:dependencyInsight --dependency webmvc

Using Gradle project-report and jacoco plugins together

I'd like to use both the Gradle project-report and jacoco plugins in my project. To illustrate the problem I'm having though, I opened build.gradle at: gradle-1.7-rc-1/samples/testing/jacoco/quickstart and modified it to be:
apply plugin: "java"
apply plugin: "jacoco"
apply plugin: "project-report"
running a gradle projectReport then gives me the error:
gradle projR
:dependencyReport
:propertyReport
:taskReport FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':taskReport'.
> Could not determine the dependencies of task ':jacocoTestReport'.
Any advice on how to get these two plugins to play nice together would be appreciated (note, they both work independently of each other).
Thanks
Covered by GRADLE-2917, will be fixed in version 1.10 as described in the Gradle Forum

Resources