Creating a JAR File
A JAR (Java ARchive) file is used to bundle Java classes and associated metadata and resources into a single file for distribution.
Step 1: Setting Up the Project
Create a new directory for your project and navigate into it:
mkdir gradle-jar-example
cd gradle-jar-example
Initialize a new Gradle project:
gradle init --type java-application
Step 2: Configuring the build.gradle
File
Open the build.gradle
file and update it as follows:
plugins {
id 'java'
id 'application'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.apache.commons:commons-lang3:3.12.0'
implementation 'com.google.guava:guava:31.1-jre'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.0'
}
application {
mainClassName = 'com.example.App'
}
test {
useJUnitPlatform()
}
Step 3: Creating Source Code
Create a simple Java application in src/main/java/com/example/App.java
:
package com.example;
public class App {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Step 4: Building the JAR File
To build the JAR file, run:
gradle jar
The JAR file will be created in the build/libs
directory.
Creating a WAR File
A WAR (Web Application Archive) file is used to package a web application for deployment on a server.
Step 1: Setting Up the Project
Create a new directory for your project and navigate into it:
mkdir gradle-war-example
cd gradle-war-example
Initialize a new Gradle project:
gradle init --type java-library
Step 2: Configuring the build.gradle
File
Open the build.gradle
file and update it as follows:
plugins {
id 'java'
id 'war'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'javax.servlet:javax.servlet-api:4.0.1'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.0'
}
war {
archiveFileName = 'example.war'
}
test {
useJUnitPlatform()
}
Step 3: Creating a Simple JSP File
Create a simple JSP file in src/main/webapp/index.jsp
:
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>
Step 4: Building the WAR File
To build the WAR file, run:
gradle war
The WAR file will be created in the build/libs
directory.
Step 5: Deploying to Tomcat
To deploy the WAR file to a Tomcat server, follow these steps:
- Download Tomcat: Download the latest version of Tomcat from the official website.
- Deploy the WAR File: Copy the
example.war
file to thewebapps
directory of your Tomcat installation. - Start Tomcat: Start the Tomcat server. The application will be accessible at
http://localhost:8080/example
.
Creating an EAR File
An EAR (Enterprise Archive) file is used to package multiple JAR and WAR files for deployment on a Java EE server.
Step 1: Setting Up the Project
Create a new directory for your project and navigate into it:
mkdir gradle-ear-example
cd gradle-ear-example
Initialize a new Gradle project:
gradle init --type basic
Step 2: Configuring the build.gradle
File
Open the build.gradle
file and update it as follows:
plugins {
id 'java'
id 'ear'
}
repositories {
mavenCentral()
}
dependencies {
deploy project(':jar-module')
deploy project(':war-module')
}
subprojects {
apply plugin: 'java'
repositories {
mavenCentral()
}
}
project(':jar-module') {
dependencies {
implementation 'org.apache.commons:commons-lang3:3.12.0'
}
}
project(':war-module') {
apply plugin: 'war'
dependencies {
implementation 'javax.servlet:javax.servlet-api:4.0.1'
}
war {
archiveFileName = 'example.war'
}
}
ear {
deploymentDescriptor {
version = '7'
applicationName = 'ExampleEAR'
}
}
Step 3: Creating Subprojects
Create subprojects for the JAR and WAR modules:
JAR Module:
- Create the directory
jar-module/src/main/java/com/example
and add a simple Java class.
- Create the directory
WAR Module:
- Create the directory
war-module/src/main/webapp
and add a simple JSP file.
- Create the directory
Step 4: Building the EAR File
To build the EAR file, run:
gradle ear
The EAR file will be created in the build/libs
directory.
Conclusion
In this guide, we have covered how to create JAR, WAR, and EAR files using Gradle. We also demonstrated how to set up a simple Java application, a web application with JSP, and an enterprise application with multiple modules. By following these steps, you can effectively package and deploy your Java applications using Gradle.
For more detailed information on Gradle, refer to the Gradle User Guide.
Comments
Post a Comment
Leave Comment