javagradledeprecated

gradle how to substitute a deprecated method [LenientConfiguration.getArtifacts(Spec)]


I am working quite newly with Gradle using SpringFrameWork and came upon the compilation error:

The LenientConfiguration.getArtifacts(Spec) method has been deprecated. This is scheduled to be removed in Gradle 9.0. Use a lenient ArtifactView with a componentFilter instead. Consult the upgrading guide for further information: link. /Project/demo/src/main/java/com/example/demo/TodoList.java:5: error: '{' expected public class TodoList(@Id Long id, String todolist, String owner) {}

As a solution the web link here is given. It seems to cover my problem, but I don't understand how to fix it. I tried just copying the given code into my build.gradle but that doesn't help the error. My program is just a small application (more to learn than to achieve anything) implementing a user-based Todo-List.

If you need any more details (code snippets etc.) let me know - thanks.

EDIT:

It states that the error occurs in the following part of code which just represent the Todo-List object (this is the full TodoList class):

import org.springframework.data.annotation.Id;

public class TodoList(@Id Long id, String todolist, String owner) {
}

My build.gradle is mostly taken from a tutorial I worked through, the (probably) most important part being the versions etc.:

// build.gradle
plugins {
    id 'java'
    id 'org.springframework.boot' version '3.1.4'
    id 'io.spring.dependency-management' version '1.1.3'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'

java {
    sourceCompatibility = '17'
}

repositories {
    mavenCentral()
}


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

    implementation 'org.springframework.data:spring-data-jdbc'
    implementation 'com.h2database:h2'
}


tasks.named('test') {
    useJUnitPlatform()
}

Solution

  • There's two things going on here: A deprecation warning from Gradle and a compiler error from Java.

    The Gradle Deprecation Warning

    The LenientConfiguration.getArtifacts(Spec) method has been deprecated. This is scheduled to be removed in Gradle 9.0. Use a lenient ArtifactView with a componentFilter instead. Consult the upgrading guide for further information: link.

    This warning is coming from the Gradle plugins you're applying, not your own build script (i.e., not the build.gradle file). The solution is for the developers of the plugins to fix the problem and release a new version. Though note that, as of this answer, the latest versions of org.springframework.boot and io.spring.dependency-management is 3.4.0 and 1.1.6, respectively. And based on this issue for the latter, the problem was fixed in version 1.1.5.

    In short, upgrade the plugins you're applying to their latest versions. If that still doesn't stop the deprecation warning, then wait for the plugin developers to release a version where the problem is fixed.

    Note this is only a warning and should not cause the build to fail until Gradle 9.0 is released and you upgrade to it.

    The Java Compiler Error

    /Project/demo/src/main/java/com/example/demo/TodoList.java:5: error: '{' expected public class TodoList(@Id Long id, String todolist, String owner) {}

    Here it looks like you're trying to write a record instead of a normal class. So, it should be:

    import org.springframework.data.annotation.Id;
    
    public record TodoList(@Id Long id, String todolist, String owner) {}
    

    Note record instead of class.