Deploying to Payara Server Using Gradle
Originally published on 01 Aug 2018
Last updated on 05 Jan 2021
Introduction
There is a lot of content detailing the use of Maven and Payara Server together (see our blog and documentation) but with Gradle still gaining popularity: this blog will show you how to use it to deploy to Payara Server.
This blog will assume you already have a Gradle project setup, but in case you don't, a sample Gradle project can be found here. The sample contains the Gradle wrapper, so if you don't have Gradle installed you can replace all calls to the gradle
command with the ./gradlew
wrapper program (or ./gradlew.bat
for windows) found in the project root. If you'd rather install Gradle, you can do so by following the guide on Gradle's website.
Deploying the Project
There are two methods for deploying to Payara Server using Gradle:
- Implement Cargo, a tool for deploying web applications to application servers
- Write several Gradle tasks yourself (for Maven users they're similar to Ant scripts tied to maven goals) to achieve the same goals
The first method is more flexible, as it can redirect the server output, deploy to remote instances and change debugging ports etc. The second method is more fun and will also run faster. The local commands for the first method and the domain commands for the second methods will require a local Payara Server installation.
Method 1:
Add the following block to the top of your build.gradle:
plugins { id "com.bmuschko.cargo" version "2.3" }
This code block will download the external Cargo Gradle dependency and apply it to the project.
Add the following blocks to the relevant place in your build file. (For most projects this will be very near the top).
repositories { mavenCentral() // Required for deployment-client:5.0 dependency maven { url "https://maven.java.net/content/repositories/promoted/" } } dependencies { // Required to allow remote deployment to Payara cargo 'org.glassfish.main.deployment:deployment-client:5.0' // Without cargo-ant you'll get following error: // Problem: failed to create task or type cargo cargo 'org.codehaus.cargo:cargo-ant:1.6.8' }
If you already have dependencies, add the ones shown above to your current list.
Add the following block to the bottom of your build.gradle
:
cargo { // Configures which commands require a built artifact cargoRunLocal.dependsOn war cargoStartLocal.dependsOn war cargoDeployRemote.dependsOn war cargoRedeployRemote.dependsOn war cargoRedeployLocal.dependsOn war containerId = 'glassfish5x' // Define deploy artifact properties deployable { context = "/" } // Only necessary for local commands local { homeDir = file(payaraHome) } // Only necessary for remote commands remote { hostname = "localhost" username = "admin" password = "" containerProperties { // this is necessary only to pass blank password correctly to the cargo deployer because of a bug in the cargo gradle plugin property 'cargo.remote.password', payaraPassword } } }
Replace the value of 'context' with the context path that you want the project deployed to. Use '/' to deploy it to the root of the server (at localhost:8080
). The commands that are configured at the top of the block won't require a gradle build
before using them, since they depend on it automatically.
The following commands can be used to manage your application with regard to Payara Server:
gradle cargoStartLocal // Starts a local Payara Server instance with a custom domain. The username is 'admin', and the password is 'adminadmin'. gradle cargoStopLocal // Stops the Payara Server instance created above. gradle cargoRedeployLocal // Redeploys the project artifact to the Payara Server instance created above. gradle cargoDeployRemote // Deploys the project artifact to a remote Payara Server instance. gradle cargoRedeployRemote // Redeploys the project artifact to a remote Payara Server instance.
While the server is running, you can make changes to your project and then redeploy your app to see the changes without needing a server restart. To see the full plugin documentation, refer to this guide.
Method 2:
Insert the following code at the bottom of your build.gradle:
// Configure the asadmin executable location ext.setProperty('asadminDir', "${payaraHome}${File.separator}bin"); ext.setProperty('asadminFile', System.properties['os.name'].toLowerCase().contains('windows')? ['cmd', '/c', 'asadmin.bat']: './asadmin') task startServer(type:Exec) { workingDir asadminDir; commandLine asadminFile; args "start-domain" } task stopServer(type:Exec) { workingDir asadminDir; commandLine asadminFile; args "stop-domain" } task redeploy(dependsOn: 'war', type:Exec) { workingDir asadminDir; commandLine asadminFile; args "deploy", "--force=true", "--contextroot=/", "--name=cargoApp", "${war.archivePath}"; }
You can now start and stop a local Payara Server instance, and redeploy your app with:
gradle startServer gradle stopServer gradle redeploy
You can rename any of the tasks as you see fit.
You've now successfully setup Gradle to deploy your web app to Payara Server!