Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle: Run multiple webapps with Jetty

Tags:

gradle

war

jetty

I want to run two webapps created by the same Gradle project into a Jetty server. Let's call these two webapps "ninja" and "warrior".

Both webapps are very similar, they only differ in the application-context file (referenced in the web.xml file) and resources.

In order to deploy them, these two options are accepted:

  • Use different ports:
http://www.example.com:8080/app (ninja webapp)
http://www.example.com:8081/app (warrior webapp)
  • Use different paths:
http://www.example.com:8080/ninja_app
http://www.example.com:8080/warrior_app

Having one or two instances of Jetty should be ok for this project.

This is my project layout:

/src/main/java
/src/main/resources
/src/main/webapp (ninja webapp)
/src/main/webapp-warrior

First question: How to create two war files with Gradle?

Second question: How to deploy the two war files in the Jetty Server with Gradle?

like image 463
Israel Varea Avatar asked Dec 20 '22 15:12

Israel Varea


2 Answers

Please have a look at Gretty gradle plugin: https://github.com/akhikhl/gretty

It supports multiple web-apps out of the box. It helps you to:

  1. run web-app projects "inplace", i.e. from compiled classes
  2. run web-app projects as WAR files
  3. run non-project WAR-files from the file system
  4. run non-project WAR-files from maven repositories (of course, WAR is downloaded first)

Additionally you can:

  • Debug multiple web-apps on the same Jetty server under any Java IDE debugger.
  • Perform integration tests with multiple web-apps on the same Jetty server.
  • Perform code coverage with Jacoco - both client-side and server-side.

There is a lot of documentation helping with everything: http://akhikhl.github.io/gretty-doc/

Disclosure: I am author of Gretty plugin.

Happy coding :)

like image 78
akhikhl Avatar answered Dec 26 '22 11:12

akhikhl


If you don't want to create two different projects, you may want to create two different gradle profiles, using the apply from: feature from Gradle.

For each webapp instance, ninja and warrior, you must create a script file with all the information specific for the profile.

In these new gradle build files, ninja-profile.gradle and warrior-profile.gradle, you can set the specific configurations that differ from ninja to warrior, which in this case could be:

  1. Resources folder: you can create a separated resources folder for each of the instances
  2. Jetty configuration: if you want to run two different instances of the 2 webapps, each of them in a separated jetty instance.

In your "main" build file you define everything that is common for all profile and build needs, plus you add the following line:

apply from: "${profile}-profile.gradle"

When you run Gradle you can pass the name of the profile using the -P option:

$ gradle -Pprofile=ninja tasks 

or

$ gradle -Pprofile=warrior tasks
like image 40
menajosep Avatar answered Dec 26 '22 10:12

menajosep