#development #gradle #java #kotlin

If you have a Gradle project running on an older version (in my case, it was using Gradle 6.8) and you want to update it to Gradle 7.0, it will just take a couple of steps.

First, we will check if there are any warnings we need to fix before we can update. This can be done by executing:

1$ ./gradlew wrapper --warning-mode all
2
3> Configure project :
4The JavaApplication.setMainClassName(String) method has been deprecated. This is scheduled to be removed in Gradle 8.0. Use #getMainClass().set(...) instead. See https://docs.gradle.org/6.8/dsl/org.gradle.api.plugins.JavaApplication.html#org.gradle.api.plugins.JavaApplication:mainClass for more details.
5        at Build_gradle$5.execute(build.gradle.kts:40)
6        (Run with --stacktrace to get the full stack trace of this deprecation warning.)
7
8BUILD SUCCESSFUL in 935ms
91 actionable task: 1 executed

In this case, the way you specify the main class for your application needs to be changed. So, instead of setting it like this in your build.gradle.kts file:

1application {
2    mainClassName = "MainKt"
3}

It needs to become:

1application {
2    mainClass.set("MainKt")
3}

Now, all that is left is to perform the actual Gradle update. This is done by executing:

1$ ./gradlew wrapper --gradle-version 7.0                   
2
3BUILD SUCCESSFUL in 6s
41 actionable task: 1 executed

We can check that everything went fine by executing:

 1$ ./gradlew wrapper --version
 2
 3------------------------------------------------------------
 4Gradle 7.0
 5------------------------------------------------------------
 6
 7Build time:   2021-04-09 22:27:31 UTC
 8Revision:     d5661e3f0e07a8caff705f1badf79fb5df8022c4
 9
10Kotlin:       1.4.31
11Groovy:       3.0.7
12Ant:          Apache Ant(TM) version 1.10.9 compiled on September 27 2020
13JVM:          11.0.8 (JetBrains s.r.o 11.0.8+10-b944.6916264)
14OS:           Mac OS X 10.16 x86_64

PS: if you want to go very fancy for checking the deprecations, you can also do a build scan:

$ ./gradlew help --scan

This will create a nicely formatted online report of the deprecations found in your project (if there are any).

I've made a simple sample project showing this. You can find the relevant commit here.