Grails 3 and Deployment to Tomcat Container

Any web application consists mainly of two tasks, i.e, development, and deployment. For web application development, we can use any tools but for deployment, we need to be careful of what environments are supported by the framework.

At the time of Grails 2, we just needed to execute grails war and put the generated war to a container, most probably Tomcat’s webapps folder, and optionally restarting the container.

Starting with Grails 3, it’s much simpler, due to the embedded, default, Tomcat container, which is specified inside the build.gradle as follows:

[sourcecode language=”java”]

dependencies {
compile "org.springframework.boot:spring-boot-starter-tomcat"
}

[/sourcecode]

We just need to execute grails war or ./gradlew bootRepackage and then execute the resulted war using java -jar build/libs/my-app-0.1.war. This would listen by default on port 8080.

See! No more specialized containers!

But wait, what if we need to deploy it to a container? Just putting this resulted war inside Tomcat’s webapps folder won’t work anymore.

Any workaround? Yes, there is.

Just change the scope of Tomcat’s dependency to provided from the compile as shown below:

[sourcecode language=”java”]

dependencies {
provided "org.springframework.boot:spring-boot-starter-tomcat"
}

[/sourcecode]

And then just generate the war and run it any way you want. Easy right?

The bonus is, our development environment is still intact by this change of the dependency scope, we are still running our grails run-app without any issue.

As a note on the Tomcat version, we need to use Tomcat 7.0.55, or else it won’t work. I’m currently using Grails 3.1.4. More information on supported containers can be found at https://grails.org/wiki/Deployment.

For further information on this topic head to http://grails.github.io/grails-doc/3.1.4/guide/deployment.html.

Thanks for reading till here. See you next time.

FOUND THIS USEFUL? SHARE IT

comments (2)

  1. Bing Zhu

    Thanks for the post. The error message from running the grails 3 app in an external Tomcat is difficult to pinpoint the exact problem.
    The deployment problem went away after using your method. One question is why it is not used as default set up since, I guess, 95% of the apps are to be deployed under an existing Tomcat.

    Reply
  2. Israel Segundo

    I just want to stop by and tell ‘THANK YOU’. I have been struggling with deploying the app for DAYS until I found your post.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *