gradlegradle-groovy-dsl

How replace deprecated getDependencyProject() in a backwards compatible way?


I am really stuck on this. I maintain a plugin written in Groovy DSL. There is one place where the now deprecated getDependencyProject() (to be removed in Gradle 9) is used:

private static List<Project> getAllDependentProjectsExt(Project project, Set<Project> handledProjects) {
    if(handledProjects.contains(project)) return []
    handledProjects << project
    def projectDependencies = project.configurations*.dependencies*.withType(ProjectDependency).flatten() as Set<ProjectDependency>
    List<Project> dependentProjects = projectDependencies*.dependencyProject
    dependentProjects.each {
        dependentProjects += getAllDependentProjectsExt(it, handledProjects)
    }
    return dependentProjects.unique()
}

The Gradle upgrade documentation has this to say:

To access or configure the target project, consider this direct replacement:

val projectDependency: ProjectDependency = getSomeProjectDependency()

// Old way: val someProject = projectDependency.dependencyProject

// New way: val someProject = project.project(projectDependency.path)

That's alright so far, but ProjectDependency.path was only introduced in 8.11. And all that I found is that prior to Gradle 8.11, the only way to get the path of a ProjectDependency is using the now deprecated dependencyProject.

Does anyone know how to solve this without bumping the minimum Gradle version to 8.11 for the plugin?


Solution

  • This was solved by someone following the GitHub issue where I mentioned this. Simply use @CompileDynamic and different branches for the Gradle versions:

        private static boolean GRADLE_VER_GTE_811 = GradleVersion.current().compareTo(GradleVersion.version('8.11'))
    
        @CompileDynamic
        private static Project getDependencyProject(Project project, ProjectDependency dep) {
            if(GRADLE_VER_GTE_811) {
                return project.project(dep.path)
            } else {
                return dep.dependencyProject
            }
        }