JAR, WAR, EAR 패키징
Gradle의 내장 플러그인을 사용해 다양한 유형의 배포 가능한 아티팩트(JAR, WAR, EAR)를 만듭니다.
JAR, WAR, EAR 패키징은(는) CoddyKit의 무료 Groovy & Gradle: JVM Automation and Build Engineering 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Groovy & Gradle: JVM Automation and Build Engineering 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Groovy & Gradle: JVM Automation and Build Engineering 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Packaging for Deployment
When you build an application, you need a way to package it for distribution and deployment. This is where deployable artifacts come in!
These are standardized packages like JARs, WARs, and EARs that bundle your application's code, resources, and dependencies. Gradle provides powerful, built-in plugins to create them efficiently.
Java Archives (JARs)
The most common artifact is a JAR (Java Archive). It's a standard format for packaging multiple Java class files, associated metadata, and resources into a single file.
JARs are typically used for:
- Java libraries
- Desktop applications
- Command-line tools
Gradle's built-in java plugin automatically configures your project to produce a JAR file.
Basic JAR Creation
To create a basic JAR, you simply apply the java plugin in your build.gradle file. Gradle will automatically find your source code (usually in src/main/java) and build a JAR.
Here's a minimal build.gradle:
plugins {
id 'java'
}
group 'com.coddykit'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}Making JARs Executable
For command-line applications, you often want an executable JAR. This means you can run it directly from your terminal using java -jar your-app.jar.
To achieve this, you need to configure the JAR's manifest file to specify which class contains your application's main method. You also typically bundle all project dependencies into this JAR.
Building an Executable JAR
Let's create a simple Java class that we'll package into an executable JAR. This class will have a main method.
Place this in src/main/java/com/coddykit/app/Greeter.java:
package com.coddykit.app;
public class Greeter {
public String getGreeting() {
return "Hello from CoddyKit!";
}
public static void main(String[] args) {
System.out.println(new Greeter().getGreeting());
}
}Executable JAR Configuration
Now, let's configure build.gradle to make the previous Java code into an executable JAR. We specify the main class and include runtime dependencies.
After running gradle jar, you can execute it with java -jar build/libs/your-project-name-version.jar.
plugins {
id 'java'
}
group 'com.coddykit'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
jar {
manifest {
attributes 'Main-Class': 'com.coddykit.app.Greeter'
}
// This bundles all runtime dependencies into the JAR
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}Web Application Archives (WARs)
A WAR (Web Application Archive) is a package specifically designed for web applications. It contains all the necessary components for a web application, such as:
- Servlet classes
- JSP files
- HTML pages, CSS, JavaScript
- Configuration files (e.g.,
web.xml) - Dependent JARs
WARs are typically deployed to a servlet container or application server (e.g., Tomcat, Jetty).
Using the `war` Plugin
To create a WAR file, you apply Gradle's built-in war plugin. This plugin automatically applies the java plugin and adds tasks to build WAR files according to the standard web application structure.
By default, web content (like HTML, CSS, JS) should be placed in src/main/webapp.
plugins {
id 'war' // This implicitly applies the 'java' plugin
}
group 'com.coddykit'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
// Example: add a servlet API dependency for compilation
providedCompile 'javax.servlet:javax.servlet-api:4.0.1'
}
war {
archiveFileName = "my-web-app.war"
// You can customize the webAppDirectory if needed
// webAppDirectory = file('src/main/webroot')
}Enterprise Archives (EARs)
An EAR (Enterprise Application Archive) is a package used to bundle multiple JARs and WARs into a single deployable unit. It's primarily used for larger Java EE (Enterprise Edition) applications deployed to full Java EE application servers (e.g., WildFly, WebLogic).
An EAR can contain:
- One or more WAR modules (web applications)
- One or more EJB-JAR modules (Enterprise JavaBeans)
- Utility JARs (common libraries)
Using the `ear` Plugin
Gradle's ear plugin allows you to create EAR files. You declare which JARs and WARs should be included as modules within the EAR. This is often used in multi-project builds where subprojects produce the individual JARs and WARs.
The deploy configuration is used to add modules to the EAR.
plugins {
id 'ear'
id 'java' // For potential utility JARs within the EAR
id 'war' // For potential WARs within the EAR
}
group 'com.coddykit'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
// Example: Add a utility JAR and a WAR (e.g., from subprojects)
// deploy project(':my-utility-jar')
// deploy project(':my-web-app-war')
// For this example, let's just make a dummy dependency
deploy 'org.apache.commons:commons-lang3:3.12.0'
}
ear {
archiveFileName = "my-enterprise-app.ear"
// Optional: Customize application.xml (deployment descriptor)
deploymentDescriptor {
// applicationName = "MyEnterpriseApp"
}
}Artifact Type Check
Consider the different types of deployable artifacts and their primary use cases.
Recap: Packaging Artifacts
In this lesson, we explored how Gradle helps you package your applications into standard deployable artifacts:
- JARs: For libraries and standalone Java applications, configured with the
javaplugin. You can make them executable by defining aMain-Class. - WARs: For web applications, using the
warplugin and deployed to servlet containers. - EARs: For bundling multiple JARs and WARs in large enterprise applications, using the
earplugin.
Mastering these packaging techniques is crucial for effective deployment and distribution of your Java applications.
자주 묻는 질문
“JAR, WAR, EAR 패키징” 강의는 무료인가요?
네 — “JAR, WAR, EAR 패키징” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Groovy & Gradle: JVM Automation and Build Engineering 강의 전체를 잠금 해제할 수 있습니다. Groovy & Gradle: JVM Automation and Build Engineering 강의에는 총 4개의 강의가 포함되어 있습니다.
“JAR, WAR, EAR 패키징”에서 뭘 배우나요?
Gradle의 내장 플러그인을 사용해 다양한 유형의 배포 가능한 아티팩트(JAR, WAR, EAR)를 만듭니다. 브라우저에서 직접 실행하는 실습 코드로 Groovy & Gradle: JVM Automation and Build Engineering을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Groovy & Gradle: JVM Automation and Build Engineering을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Groovy & Gradle: JVM Automation and Build Engineering은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.
“JAR, WAR, EAR 패키징” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Groovy & Gradle: JVM Automation and Build Engineering 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Groovy & Gradle: JVM Automation and Build Engineering 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- JAR, WAR, EAR 패키징
- 저장소에 게시
- CI/CD 통합
- 버전 관리와 릴리스 자동화