I found the solution after searching the Gretty Possible Running Build in Different Ports Github repository.
All you need to do is change the build.gradle file as follows
gretty {
httpPort = project.hasProperty("httpPort") ? project.httpPort as int : 8080
contextPath = '/'
}
Another way to do this is by adding the following
gradle.taskGraph.whenReady {
graph ->
if (project.hasProperty("httpPort")) {
gretty.httpPort = httpPort as int
}
}
Both solutions will work by passing property arguments to the guild file
gradle -PhttpPort=8888