Gradle Kotlin allprojects dependencies cause build failure - gradle

In my Gradle project, I can declare a dependencies block with implementation entries, no problem. When I try to declare something like this, however, I get an error:
allprojects {
dependencies {
implementation("...")
}
}
The error I get:
Could not find method implementation() for arguments [org.mockito:mockito-core:2.25.0]
on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
I wasn't able to reproduce this with a Java project, so it might have something to do with the Kotlin project. I'm new to Gradle, so I might just be doing something silly? Here's my environment info:
$ gradle --version
------------------------------------------------------------
Gradle 5.2.1
------------------------------------------------------------
Build time: 2019-02-08 19:00:10 UTC
Revision: f02764e074c32ee8851a4e1877dd1fea8ffb7183
Kotlin DSL: 1.1.3
Kotlin: 1.3.20
Groovy: 2.5.4
Ant: Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM: 1.8.0_112 (Oracle Corporation 25.112-b15)
OS: Windows 10 10.0 amd64
Minimal reproduction
I can reproduce the issue with a minimal project.
build.gradle:
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.21'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
// This works.
dependencies {
implementation 'org.mockito:mockito-core:2.25.0'
}
// Causes an error. Using a random dependency to reproduce the issue.
allprojects {
dependencies {
implementation 'org.mockito:mockito-core:2.25.0'
}
}
settings.gradle:
rootProject.name = 'demo'
// Removing this line causes the error to go away, but means the module is missing.
include 'submodule'
submodule/build.gradle
// Empty file. I've tried adding various plugins (java / kotlin) to no avail.

The issue in your build.gradle is you are trying to use a build dependency configuration, in this case implementation without specifying the java plugin. The gradle docs says;
The Java plugin adds a number of dependency configurations to your
project, as shown below. Tasks such as compileJava and test then use
one or more of those configurations to get the corresponding files and
use them, for example by placing them on a compilation or runtime
classpath.
One way to fix this is to include java plugin as below (I have tested on 5.2.1 and it worked fine);
allprojects {
apply plugin: 'java'
dependencies {
implementation 'org.mockito:mockito-core:2.25.0'
}
}
The relationship of build configuration to the java plugin has been comprehensively described on
https://docs.gradle.org/current/userguide/java_plugin.html#sec:java_plugin_and_dependency_management
and
https://docs.gradle.org/current/userguide/managing_dependency_configurations.html#managing_dependency_configurations
Also, make sure you don't duplicate this dependency (or any other which is declared in the root or for all projects) in subprojects.

Related

Gradle and Dependencies supported Java version

How do I make sure that the dependencies I use in Gradle are compatible with a given Java version?
For example, I created an application that uses GraalVM. In Gradle I specify:
implementation 'org.graalvm.js:js:22.2.0'
implementation 'org.graalvm.js:js-scriptengine:22.2.0'
When executing the code I get:
Error loading driver /Users/yusuf-mac/.DbSchema/drivers/MongoDb/truffle-api-22.2.0.jar : java.lang.UnsupportedClassVersionError: META-INF/versions/17/module-info has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 59.0.
Please consider upgrading Java. Your current Java version is AdoptOpenJDK 15.0.2.
How do I ensure in Gradle that the dependencies are compatible with OpenJDK 15? I cannot upgrade to a higher version of OpenJDK. Is any way to make sure that the dependency version 22.2.0 is compatible with OpenJDK 15?
Here the complete build.gradle
buildscript {
repositories {
mavenCentral()
}
}
plugins{
id "java"
id 'org.hidetake.ssh' version '2.7.1'
}
apply plugin: 'application'
apply plugin: 'distribution'
java {
toolchain {
languageVersion = JavaLanguageVersion.of(15)
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.mongodb:mongodb-driver-sync:4.7.1'
implementation 'org.graalvm.js:js:22.2.0'
implementation 'org.graalvm.js:js-scriptengine:22.2.0'
implementation 'com.google.code.gson:gson:2.9.0'
testImplementation 'junit:junit:4.13.2'
//implementation "org.graalvm.sdk:graal-sdk:22.0.0.2"
//implementation "org.graalvm.truffle:truffle-api:22.0.0.2"
}
compileJava{
doFirst {
options.compilerArgs = [
'--module-path', classpath.asPath,
]
}
}
jar {
archiveName ="mongojdbc${version}.jar"
manifest {
attributes 'Main-Class': 'com.wisecoders.dbschema.mongodb.JdbcDriver'
attributes 'Class-Path': configurations.runtimeClasspath.files.collect { it.getName() }.join(' ')
attributes 'Specification-Version': "$version"
attributes 'Specification-Vendor': "Wise Coders"
attributes 'Implementation-Vendor-Id': "dbschema.com"
attributes 'Implementation-Vendor': "Wise Coders"
attributes 'Implementation-Version': new Date().format( 'yyMMdd' )
}
}
task zip(type: Zip, dependsOn:['clean','jar']) {
archiveFileName.set 'MongoDbJdbcDriver.zip'
from configurations.runtimeClasspath.allArtifacts.files
from configurations.runtimeClasspath
from "build/libs" include "*.jar"
}
I was unable to reproduce this behavior in a sample project I created - https://github.com/alexanderankin/so-73508929 - including this library and its use.
I've also included the gradle tool chain api reference in the build.gradle script in my project to show how you would restrict the java version your project is validated against, although I'm pretty sure it would be easier to use a tool like sdkman to quickly switch between java versions.
Your error message also indicates that the jar is coming from a mongo folder, so maybe its from something else in that project/environment.

Trouble consuming Gradle plugin project with 3rd party maven repository

I am attempting to create a Gradle plugin that is set up like this:
plugins {
id 'java-gradle-plugin'
id 'groovy'
}
repositories {
mavenCentral()
jcenter()
// note the custom maven repo
maven { url = "https://maven.gentics.com/maven2" }
}
dependencies {
implementation gradleApi(),
localGroovy(),
"com.gentics.mesh:mesh-rest-client:${MESH_VER}"
testImplementation gradleTestKit()
testImplementation("org.spockframework:spock-core:${SPOCK_VER}") {
exclude module : 'groovy-all'
}
}
// the rest...
However if I try to consume this plugin in another project with includeBuild "./gradle-mesh" in the settings.gradle, I get the following error:
A problem occurred configuring root project 'functionalTest'.
> Could not resolve all artifacts for configuration ':classpath'.
> Could not find com.gentics.mesh:mesh-rest-client:1.7.1.
Searched in the following locations:
- https://plugins.gradle.org/m2/com/gentics/mesh/mesh-rest-client/1.7.1/mesh-rest-client-1.7.1.pom
If the artifact you are trying to retrieve can be found in the repository but without metadata in 'Maven POM' format, you need to adjust the 'metadataSources { ... }' of the repository declaration.
Required by:
project : > project :gradle-mesh
However, I am able to execute the functionalTest in the actual plugin tests without issue. Getting rid of that line in the settings.gradle and renaming the folder to buildSrc works as well.
Is there a way to configure the buildscript dependencies from within the plugin entry point? I tried the following, but it didn't make any difference.
class GradleMeshPlugin implements Plugin<Project> {
public void apply(Project project) {
project.buildscript.repositories.configure {
mavenLocal()
jcenter()
maven { url = "https://maven.gentics.com/maven2" }
}
// dependency is referenced here...
}
}
Any ideas how to go about this?
Full Project: https://github.com/wawebio/gradle-mesh
System:
------------------------------------------------------------
Gradle 6.6.1
------------------------------------------------------------
Build time: 2020-08-25 16:29:12 UTC
Revision: f2d1fb54a951d8b11d25748e4711bec8d128d7e3
Kotlin: 1.3.72
Groovy: 2.5.12
Ant: Apache Ant(TM) version 1.10.8 compiled on May 10 2020
JVM: 11.0.8 (Ubuntu 11.0.8+10-post-Ubuntu-0ubuntu120.04)
OS: Linux 5.4.0-47-generic amd64
Update
I was also able to get it to work by adding the following in the consuming project's settings.gradle
pluginManagement {
repositories {
mavenCentral()
jcenter()
maven { url = "https://maven.gentics.com/maven2" }
}
}
rootProject.name = 'website'
includeBuild "./gradle-mesh"
Is there a way to do this from within the plugin application itself?
You need to specify your mvn package‌‌‌​​‌​‌‌​‌‌‌‌‌‌​​​‌​‌‌​‌‌‌‌ differently, which is the first one.
<configuring-project>

Gradle: Could not get unknown property 'classesDir' for main classes

According to the documentation, I've tried to use aspectj plugin.
This is the message I get when I build my project.
FAILURE: Build failed with an exception.
* Where:
Build file '/home/jesudi/projects/gradle-vscode/build.gradle' line: 22
* What went wrong:
A problem occurred evaluating root project 'security'.
> Failed to apply plugin [id 'aspectj.gradle']
> Could not create task ':compileAspect'.
> Could not get unknown property 'classesDir' for main classes of type org.gradle.api.internal.tasks.DefaultSourceSetOutput.
This is my script:
buildscript {
repositories {
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "org.apache.meecrowave:meecrowave-gradle-plugin:1.2.6"
classpath "gradle.plugin.aspectj:gradle-aspectj:0.1.6"
}
}
plugins {
id 'java'
}
project.ext {
aspectjVersion = '1.9.2'
}
apply plugin: 'aspectj.gradle'
apply plugin: "org.apache.microwave.microwave"
java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
dependencies {
compile("org.apache.meecrowave:meecrowave-core:1.2.6")
compile("org.apache.meecrowave:meecrowave-specs-api:1.2.6")
}
meecrowave {
httpPort = 9090
// most of the meecrowave core configuration
}
This is the gradle -version output:
------------------------------------------------------------
Gradle 5.1.1
------------------------------------------------------------
Build time: 2019-01-10 23:05:02 UTC
Revision: 3c9abb645fb83932c44e8610642393ad62116807
Kotlin DSL: 1.1.1
Kotlin: 1.3.11
Groovy: 2.5.4
Ant: Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM: 11.0.2 (Oracle Corporation 11.0.2+9)
OS: Linux 4.15.0-20-generic amd64
In Gradle 5.x, this property has been renamed to classesDirs from classesDir.
You can find more information here
Possible your plugin is not a root of problem but the version of Gradle is.
First time I had the same error in libgdx game (android studio).
In build.gradle(desktop) file "classDir was renamed to classesDirs and it helped.
from files(sourceSets.main.output.classesDirs)
The classesDir property was deprecated in gradle 4.x, and removed in gradle 5.x (see the release notes).
The plugin has apparently not been maintained.
I got a similar error message, not with this plugin but even with the HelloWorld app created by Grails (3.2.9). In my case, I already used sdkman to set my current Gradle to 3.5 instead of 5, but the problem persists. It turned out that the "grails" cli (https://github.com/grails/grails-core/blob/3.2.x/grails-shell/src/main/groovy/org/grails/cli/gradle/GradleUtil.groovy) uses the default sdkman version for Gradle (which is different from the "current" version).
From Grails 3.3.X, one can set "gradleWrapperVersion=3.5" in gradle.properties instead, or use GRAILS_GRADLE_HOME environment variable as before.

Gradle can't resolve 'kotlin' plugin. Am I missing the correct maven repository?

gradle tasks fails telling me it can't find the plugin 'kotlin'. My build.gradle file starts with:
buildscript {
ext.kotlin_version = '1.3.11'
repositories {
mavenLocal()
mavenCentral()
jcenter()
maven { url "https://dl.bintray.com/kotlin/kotlin-dev/" }
maven { url "http://central.maven.org/maven2/" }
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
plugins {
id 'kotlin'
id 'application'
}
WRT maven repositories, I'm clearly just trying to redundantly add all the kitchen sinks I can find. I have tried none, and commenting out some. Which one am I missing?
From the log:
12:55:04.419 [DEBUG] [org.gradle.launcher.daemon.server.exec.ReturnResult] Daemon is dispatching the build result: Failure[value=org.gradle.initialization.Repor
tedException: org.gradle.internal.exceptions.LocationAwareException: Build file '/home/_/workspace/_/build.gradle' line: 18
Plugin [id: 'kotlin'] was not found in any of the following sources:
- Gradle Core Plugins (not a core plugin, please see https://docs.gradle.org/5.0/userguide/standard_plugins.html for available core plugins)
- Plugin Repositories (plugin dependency must include a version number for this source)]
I have also tried version 1.3.10 because it matches what's listed in gradle version info:
------------------------------------------------------------
Gradle 5.0
------------------------------------------------------------
Build time: 2018-11-26 11:48:43 UTC
Revision: 7fc6e5abf2fc5fe0824aec8a0f5462664dbcd987
Kotlin DSL: 1.0.4
Kotlin: 1.3.10
Groovy: 2.5.4
Ant: Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM: 1.8.0_181 (Oracle Corporation 25.181-b13)
OS: Linux 4.19.6-200.fc28.x86_64 amd64
Kotlin is not a core plugin, therefore you have to include the version. Also the name is different, when using the plugins configuration:
plugins {
id "org.jetbrains.kotlin.jvm" version "1.3.11"
}
If you use the apply function, you could still use the simple name:
apply plugin: "kotlin"
You'll find this information on Targeting the JVM.

How to configure a plugin to depend on a specific version of gradle?

I am writing a set of Gradle plugins, but I want to control the specific versions of groovy and gradle that are used.
I don't want the plugins to depend on whatever versions of Gradle/Groovy are installed, like the following would do:
dependencies {
compile localGroovy()
compile gradleApi()
}
Another reason I don't want to use the local method - when you use a proper dependency specification, Gradle then knows about the source code for those libs and the IDE plugins can hookup the source automatically.
Below are the relevant sections of my build script:
allprojects { Project iProject ->
apply plugin: 'idea'
apply plugin: 'maven'
repositories {
jcenter()
}
}
subprojects { Project iProject ->
apply plugin: 'groovy'
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.2'
}
}
project(':eclipsei2g') {
group = 'eclipsei2g'
version = '0.0.1-SNAPSHOT'
dependencies {
compile 'org.gradle:gradle-core:2.0'
}
}
project(':g2idea13') {
group = 'g2idea13'
version = '0.0.1-SNAPSHOT'
dependencies {
compile 'org.gradle:gradle-core:2.0'
compile 'org.gradle-plugins:gradle-ide:2.0'
}
}
When I run this I get an error resolving the gradle-ide dependency:
Could not resolve all dependencies for configuration ':g2idea13:compile'.
> Could not find org.gradle:gradle-ide:2.0.
Searched in the following locations:
http://jcenter.bintray.com/org/gradle/gradle-ide/2.0/gradle-ide-2.0.pom
http://jcenter.bintray.com/org/gradle/gradle-ide/2.0/gradle-ide-2.0.jar
Required by:
g2idea13:g2idea13:0.0.1-SNAPSHOT
There doesn't seem to be anything on the jcenter repository since 0.9 for the plugins stuff.
I also tried 'org.gradle:gradle-ide:2.0'.
Is this even how I should be doing this? Is there another way to specify a specific gradle version? Am I just using the wrong repository? I couldn't even get gradle-core to resolve on mavenCentral(). Is there an official Gradle repository somewhere that I should be using?
gradleApi() is the way to go. There isn't currently a public list of dependencies for Gradle plugins.

Resources