
Gradle is an open-source build automation tool that streamlines multi-language development workflows, especially for large-scale Java applications. To get started, you can install Gradle on Ubuntu 24.04, which automates tasks such as compiling, testing, packaging, and deploying applications using Java, Kotlin, Scala, Android, Groovy, C++, or Swift. It also integrates with other tools and platforms to simplify the build, test, and deployment processes.
This article explains how to install Gradle on Ubuntu 24.04 and set it up to run applications on a server.
You can install Gradle using the latest release file, APT or Snap on Ubuntu 24.04. Follow the steps below to use a specific installation method to install Gradle on your server.
The Gradle release ZIP archive enables you to install the latest or a specific version from source. This ensures compatibility and access to up-to-date features for your applications. Follow the steps below to install the required dependency packages and download the latest Gradle release file to install on your server.
Update the server's package index.
Install the required Java Development Kit (JDK) package.
View the installed Java version.
Visit the Gradle release page, identify the latest version, and copy its binary direct download link. Then, use wget to download the file. For example, download the Gradle 8.12 version.
Extract the Gradle ZIP archive contents to a system-wide directory such as /opt
Setting up a global environment variable with your Gradle installation directory allows you to execute the Gradle binary as a global system-wide command. Follow these steps to create a script in the /etc/profile.d directory for the new global environment variable
Create a new gradle.sh script in the /etc/profile.d directory.
Add the following directives to the gradle.sh file.
Save and close the file.
Enable execute permissions on the script.
Load the Gradle environment configuration in your active shell.
Verify the installed Gradle version.
Output:
Follow the steps below to create a basic Java application and run it using Gradle on your server.
Create a new sample_project directory to use with Gradle.
Switch to the sample_project directory.
Initialize the Gradle project to create a basic Gradle project structure.
Output:
Press Enter to use the default Java version in your project.
Verify your project name and press Enter.
Enter 1 to set up a single application project.
Select your desired build language. For example, enter 1 to select Kotlin as the build script language.
Select the target test framework. For example, enter 1 to select JUnit 4.
Press yes to use new APIs with Gradle and initialize your project.
Your output should be similar to the one below when the build process is successful.
Create the src/main/java directory to store your application files.
Create a new App.java Java application file in the src/main/java directory using a text editor such as nano.
Add following contents to the file.
Save and close the file.
The above Java program outputs a Greetings from Vultr message when executed.
Open the build.gradle file to ensure it includes the Java plugin and specifies the main class.
Add the following contents to the file.
Save and close the file.
Build and compile the Java application into a single file using Gradle.
The above command compiles the Java application code, runs tests, and packages the application into a single .jar file.
Output:
Run the application using Gradle.
Output:
Run the bundled .jar file using Java.
Output:
Gradle supports multiple dependencies to perform specific application functions. Follow the steps below to install the Spark Java web server dependency to enable Gradle to run web applications using dedicated ports on your server.
Open the build.gradle file.
Modify the dependencies section to include a new SparkJava directive.
Save and close the file.
Your modified dependencies section should look like the one below.
Back up the App.java application file.
Create the App.java file again.
Add the following code to the App.java file to enable a basic HTTP server using Spark Java.
Save and close the file.
The above modified Java application code imports the Spark Java package, runs the application on port 8080 and outputs a Greetings from Vultr message when accessed.
Allow connections to port 8080 through the default firewall.
Reload UFW to apply the firewall changes.
Start the application using Gradle.
Output:
Access your server's IP address on port 8080 using a web browser such as Firefox and verify that your web application displays.
Follow the steps below to uninstall Gradle on your server if necessary depending on your installation method.
Delete the Gradle binary to disable it on your server.
Remove the Gradle environment variable script.
Run the following command to uninstall Gradle if you installed it using APT.
Uninstall Gradle if installed using Snap.
You have installed Gradle on Ubuntu 24.04 and run sample applications on your server. Gradle is highly scalable and integrates with multiple frameworks to manage and build applications. For more information and configuration options, visit the Gradle documentation.
0 Comments
Be the first to comment and share your perspective with the community.