Upgrading build.gradle to the Gradle 7.0+ format

Hello,
I am running Gradle 6.0.1 for a Java 8 project. I want to upgrade it to the Gradle 7.0+ format to get rid of the deprecation notifications. The format I use for dependency declaration is the one provided by the mvnrepository site, namely:

compile group: ‘com.google.guava’, name: ‘guava’, version: ‘28.1-jre’

I also have the following for some local .jar files:

compile fileTree(dir: ‘lib’, include: ‘*.jar’)

All guides on upgrading said I should simply replace compile with implementation (or api) and it will just work. But it’s not as simple.

With implementation the MVN dependencies aren’t transitive, so projects that depend on the project with the above configuration don’t get them. With api instead, I get:

Could not find method api() for arguments [{group=com.google.guava, name=guava, version=28.1-jre}] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

And none of them work for the fileTree command.

What’s the proper way to do this in both cases? Thanks!

Hi @dandrei,

I assume you are using the java plugin. Please use the java-library plugin instead. Then the api keyword/configuration is available for declaring dependencies.

See also: https://docs.gradle.org/current/userguide/java_library_plugin.html

1 Like

Thanks, that’s right!
I was using:

apply plugin: ‘java’

I changed it to:

apply plugin: ‘java-library’

Now both api and implementation work perfectly in all cases, including:

implementation fileTree(dir: ‘lib’, include: ‘*.jar’)
implementation group: ‘mysql’, name: ‘mysql-connector-java’, version: ‘8.0.17’
implementation project(‘:common’)

Will the war plugin be moved over to extend the java-library plugin instead? At the moment api isn’t available.

Any word on a fix for the war plugin?

There’s absolutely no need for this. Both the java-library plugin and the war plugin add capabilities on top of the java plugin, but it’s not extension like in OOP. If you want the capabilities of both the java-library plugin and war plugin, you should just be applying both of them to your project. They’re not mutually exclusive and there’s no reason that building a war should automatically force the application of the java-library plugin.