Hello :wave: I have a little problems with convent...
# community-support
p
Hello šŸ‘‹ I have a little problems with convention plugins. I receive a following error in the consumer project:
Copy code
> Could not resolve all artifacts for configuration ':classpath'.
   > Could not resolve com.example:gradle-convention-plugin:0.0.11.
     Required by:
         project : > com.example.spotless-conventions:com.example.spotless-conventions.gradle.plugin:0.0.11
      > Dependency requires at least JVM runtime version 21. This build uses a Java 17 JVM.
This is how the project looks like:
Copy code
.
ā”œā”€ā”€ build.gradle
ā”œā”€ā”€ gradle
│   └── wrapper
│       ā”œā”€ā”€ gradle-wrapper.jar
│       └── gradle-wrapper.properties
ā”œā”€ā”€ gradlew
ā”œā”€ā”€ settings.gradle
└── src
    ā”œā”€ā”€ main
    │   ā”œā”€ā”€ groovy
    │   │   └── com.example.spotless-conventions.gradle
    │   └── resources
    └── test
        └── resources
build.gradle
Copy code
plugins {
    id 'groovy-gradle-plugin'
    id 'maven-publish'

    // When updating, update below in dependencies too
    id 'com.diffplug.spotless' version '6.25.0'
}

group = 'com.example'

dependencies {
    implementation 'com.diffplug.spotless:spotless-plugin-gradle:6.25.0'
}

publishing {
    repositories {
        maven {
            name = "GitHubPackages"
            url = uri("<https://maven.pkg.github.com/example/gradle-convention-plugin>")
            credentials {
                username = 'xxx'
                password = 'xxx'
            }
        }
    }
}

repositories {
    gradlePluginPortal()
}
com.example.spotless-conventions.gradle
Copy code
plugins {
    id 'com.diffplug.spotless'
}

spotless {
    java {
        target 'src/**/*.java'

        palantirJavaFormat('2.47.0')
        importOrder('', 'java', '\\#')
        removeUnusedImports()
    }
}
and when I apply a
./gradlew spotlessApply
everything goes well, the problem is when I add, to the consumer project a
com.github.tjni.captainhook:captain-hook:0.1.5
plugin and try call:
Copy code
apply plugin: 'com.github.tjni.captainhook'

captainHook {
    preCommit = './gradlew spotlessApply --stacktrace'
}
The mentioned error occur. Any thoughts?
c
Have you tried addressing the error messsge? Ensuring that a compatible jvm version is used?
p
I have set java:
Copy code
java -version
openjdk version "21.0.2" 2024-01-16 LTS
OpenJDK Runtime Environment Temurin-21.0.2+13 (build 21.0.2+13-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.2+13 (build 21.0.2+13-LTS, mixed mode, sharing)
Also in the consumer project:
Copy code
java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}
But how and where to set in the convention plugin?
And a little bit incomprehensible for me is that line of the error:
Copy code
Could not resolve com.example:gradle-convention-plugin:0.0.11.
c
Gradle is running with Java 17 where the convention plug-in requires Java 21. That message indicates there is no variant of the convention plug-in that works with Java 17.
a
If you're creating a convention plugin it should be in a /buildSrc directory but yours looks to just be in a /src directory
šŸ‘Ž 1
p
It's because I am creating this for sharing purposes through multiple repositories. I was following these steps from https://docs.gradle.org/current/samples/sample_publishing_convention_plugins.html.
v
That's fine. Aaron seems to have missed the fact that you publish your convention plugin and consume the published one
āœ… 1
p
What is more, when I move a
com.github.tjni.captainhook
to the convention plugin, and in the
com.thulium.spotless-conventions.gradle
file have following:
Copy code
apply plugin: 'com.github.tjni.captainhook'

captainHook {
    preCommit = './gradlew spotlessApply --status'
}
When the hook is called I receive:
Copy code
pre-commit > ./gradlew spotlessApply
Starting a Gradle Daemon, 2 incompatible and 6 stopped Daemons could not be reused, use --status for details
And the Java 17 is used:
Copy code
/usr/lib/jvm/java-17-openjdk-amd64/bin/java
v
But at Chris said. What toolchain you set in the consumer is irrelevant as that is for the production code of that project. But the problem is the Java version you run Gradle with.
If you run
./gradlew --version
, you will see which Java is used
And that will be 17, while either your convention plugin is only compatible with 21, or some dependency it has.
If it should be compatible with 17, you should set the toolchain in the project building the convention plugin.
šŸ‘ 1
p
Both, the consumer and the plugin has:
Copy code
09:03 $ ./gradlew --version

------------------------------------------------------------
Gradle 8.8
------------------------------------------------------------

Build time:   2024-05-31 21:46:56 UTC
Revision:     4bd1b3d3fc3f31db5a26eecb416a165b8cc36082

Kotlin:       1.9.22
Groovy:       3.0.21
Ant:          Apache Ant(TM) version 1.10.13 compiled on January 4 2023
JVM:          21.0.2 (Eclipse Adoptium 21.0.2+13-LTS)
OS:           Linux 6.5.0-41-generic amd64
However, what's weird is that when I run
com.github.tjni.captainhook
in the consumer, it works. But when I move it to the plugin, it runs
/usr/lib/jvm/java-17-openjdk-amd64/bin/java
, and I don't understand why.
v
Can you share a build
--scan
URL of the failure?
p
Sure, let me recap all: PLUGIN:
build.gradle
Copy code
buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.github.tjni.captainhook:captain-hook:0.1.5'
    }
}

plugins {
    id 'groovy-gradle-plugin'
    id 'maven-publish'

    // When updating, update below in dependencies too
    id 'com.diffplug.spotless' version '6.25.0'
}

group = 'com.thulium'

apply plugin: 'com.github.tjni.captainhook'

captainHook {
    autoApplyGitHooks = false
}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

dependencies {
    implementation 'com.diffplug.spotless:spotless-plugin-gradle:6.25.0'
    implementation 'com.github.tjni.captainhook:captain-hook:0.1.5'
}

publishing {
    repositories {
        mavenLocal()
    }
}

repositories {
    gradlePluginPortal()
}
com.thulium.spotless-conventions.gradle
Copy code
plugins {
    id 'com.diffplug.spotless'
}

plugins.apply('com.github.tjni.captainhook')

captainHook {
    autoApplyGitHooks = true
    preCommit = './gradlew spotlessApply'
}

spotless {
    java {
        target 'src/**/*.java'

        palantirJavaFormat('2.47.0')
        importOrder('', 'java', '\\#')
        removeUnusedImports()
    }
}
CONSUMER:
settings.gradle
Copy code
pluginManagement {
    repositories {
        gradlePluginPortal()
        mavenLocal()
    }
}

rootProject.name = 'demo'
build.gradle
Copy code
plugins {
    id 'java'
    id 'org.springframework.boot' version '3.3.1'
    id 'io.spring.dependency-management' version '1.1.5'

    id 'com.thulium.spotless-conventions' version '0.0.11'
}

group = 'com.example'

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}


repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

tasks.named('test') {
    useJUnitPlatform()
}
When I trying to commit (githook is registered)
Copy code
pre-commit > ./gradlew spotlessApply
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring root project 'demo'.
> Could not resolve all artifacts for configuration ':classpath'.
   > Could not resolve com.thulium:gradle-convention-plugin:0.0.11.
     Required by:
         project : > com.thulium.spotless-conventions:com.thulium.spotless-conventions.gradle.plugin:0.0.11
      > Dependency requires at least JVM runtime version 21. This build uses a Java 17 JVM.
Scan URL: https://scans.gradle.com/s/23aplptzfbiki
As I said, the plugin works well without
com.github.tjni.captainhook
v
That build
--scan
is not with that failure and thus useless
p
If it isn't that scan, I have no idea how to get it
v
Yes, that's the one. And if you open it, you see in the bottom in the "Infrastructure" part, that you are running that build with Java 17, which is then also matching what the error message complains about.
p
Ahh, I see it, but I have no idea why... Maybe somehow this plugin is forcing the use of this Java version. But why does everything work fine when I set the plugin in CONSUMER, and the problem only occurs with PLUGIN?
v
A plugin cannot force what Java version you use to run Gradle. Whatever you did when creating that last build scan, you used Java 17 to run Gradle. And your convention plugin is built in a way that enforces Java 21 is used to run Gradle.
I can hardly guess what you do to run sometimes with 21, sometimes with 17 šŸ™‚
p
It was a good lead. Somehow, I don't understand how yet, the JAVA_HOME was not set in the preCommit hook. When I set it like this, it works like a charm
Copy code
captainHook {
    autoApplyGitHooks = true
    preCommit = 'JAVA_HOME="$HOME/.sdkman/candidates/java/current" ./gradlew spotlessApply'
}
But the issue was crazy 🤯
v
You probably set
JAVA_HOME
in
.bashrc
or
.bash_profile
and when the hook is run, it might well be that it runs in a different environment where those effects are not present. But that is a question for Git, not Gradle. šŸ™‚