Groovy & Gradle: JVM Automation and Build Engineering · บทเรียน

การจัดแพ็กเกจ JAR, WAR, EAR

สร้างอาร์ติแฟกต์ที่พร้อมนำไปใช้งานหลายชนิด (JAR, WAR, EAR) ด้วยปลั๊กอินในตัวของ Gradle

บทเรียน 1 จาก 412 ขั้นตอน

การจัดแพ็กเกจ JAR, WAR, EAR เป็นบทเรียน Groovy & Gradle: JVM Automation and Build Engineering ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 java plugin. You can make them executable by defining a Main-Class.
  • WARs: For web applications, using the war plugin and deployed to servlet containers.
  • EARs: For bundling multiple JARs and WARs in large enterprise applications, using the ear plugin.

Mastering these packaging techniques is crucial for effective deployment and distribution of your Java applications.

เริ่มต้นได้ฟรี

เรียนรู้ Groovy ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “การจัดแพ็กเกจ JAR, WAR, EAR” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การจัดแพ็กเกจ JAR, WAR, EAR” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Groovy & Gradle: JVM Automation and Build Engineering ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Groovy & Gradle: JVM Automation and Build Engineering มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การจัดแพ็กเกจ JAR, WAR, EAR”

สร้างอาร์ติแฟกต์ที่พร้อมนำไปใช้งานหลายชนิด (JAR, WAR, EAR) ด้วยปลั๊กอินในตัวของ Gradle คุณปฏิบัติ Groovy & Gradle: JVM Automation and Build Engineering ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Groovy & Gradle: JVM Automation and Build Engineering หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Groovy & Gradle: JVM Automation and Build Engineering บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “การจัดแพ็กเกจ JAR, WAR, EAR” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Groovy & Gradle: JVM Automation and Build Engineering นี้ได้ไหม

ได้ บทเรียน Groovy & Gradle: JVM Automation and Build Engineering ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การจัดแพ็กเกจ JAR, WAR, EAR
  2. การเผยแพร่ไปยังคลังเก็บ
  3. การผสานรวม CI/CD
  4. การกำหนดรุ่นและการทำกระบวนการเผยแพร่อัตโนมัติ
← กลับไปที่ Groovy & Gradle: JVM Automation and Build Engineering