Search code examples
gradleconfigurationdependencies

shared gradle files, need to exclude useJUnitPlatform for projects that do not use junit 5


I use shared gradle files for code quality on multiple projects which use junit 4. I am creating a new service in which I would like to take advantage of junit 5. The problem I have is that I need a way to only include useJUnitPlatform when the project has the junit-jupiter-engine dependency. I can get the dependencies with configurations.testCompile but I have been unable to find a good solution to add a conditional that will include useJUnitPlatform if and only if the junit-jupiter-engine exists.

I would like to stay with shared gradle files for the code quality and keep older projects that utilize junit4 to continue to use it until I decide to upgrade.

Any ideas would be appreciated.

test {
    configurations.testCompile.each { println it }

    if (???) {
        useJUnitPlatform()
    }
}

Solution

  • I found an inefficient way:

     def junit5 = false
     configurations.testCompile.each { lib -> 
        if( lib.toString().contains("junit-jupiter-api") ) {
            junit5 = true;          
        }
     }
    
     if (junit5) {
     }
    

    I was looking for a more direct or efficient way of doing this.