🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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.warfile to thewebappsdirectory 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/exampleand add a simple Java class.
- Create the directory
WAR Module:
- Create the directory
war-module/src/main/webappand 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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment