การเผยแพร่ไปยังคลังเก็บ
เผยแพร่อาร์ติแฟกต์ของโครงการไปยังคลังเก็บ Maven Central, Artifactory หรือ Nexus
การเผยแพร่ไปยังคลังเก็บ เป็นบทเรียน Groovy & Gradle: JVM Automation and Build Engineering ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Groovy & Gradle: JVM Automation and Build Engineering และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Groovy & Gradle: JVM Automation and Build Engineering มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Welcome to Publishing!
Ever wondered how open-source libraries or internal company tools become available for others to use? It's all about publishing!
In this lesson, you'll learn how to use Gradle to publish your project's compiled artifacts (like JARs) to central repositories. This makes them easily consumable as dependencies by other projects.
Artifacts & Repositories
Let's clarify some key terms:
- An artifact is a deployable output of your project, typically a compiled JAR, WAR, or AAR file. It's the 'product' you want to share.
- A repository is a storage location for these artifacts. Think of it as a digital library where artifacts are stored and retrieved. Examples include Maven Central, JCenter, Artifactory, or Nexus.
They are crucial for managing project dependencies.
The Maven Publish Plugin
Gradle uses plugins to extend its core functionality. For publishing artifacts to Maven-compatible repositories, we use the maven-publish plugin.
This powerful plugin provides the necessary tasks and a Domain Specific Language (DSL) to configure exactly what and where you want to publish.
Applying the Plugin
The first step is to apply the maven-publish plugin in your build.gradle file. We typically also apply the java plugin if we're publishing a Java library.
Don't forget to define your project's group and version, as these are critical metadata for your published artifact!
plugins {
id 'java'
id 'maven-publish'
}
group = 'com.coddykit.mylib'
version = '1.0.0'
// Minimal task to compile Java for publishing
java {
withSourcesJar()
withJavadocJar()
}Defining a Publication
Inside the publishing block, you define one or more publications. Each publication specifies what artifacts (e.g., your main JAR, source JAR, Javadoc JAR) will be part of that release.
The from components.java line tells Gradle to include all the standard Java component artifacts (like the compiled JAR) in this publication.
plugins {
id 'java'
id 'maven-publish'
}
group = 'com.coddykit.mylib'
version = '1.0.0'
java {
withSourcesJar()
withJavadocJar()
}
publishing {
publications {
mavenJava(MavenPublication) {
groupId = project.group
artifactId = 'my-awesome-lib'
version = project.version
from components.java // Includes the main JAR
}
}
}Adding Sources & Javadoc
It's good practice to publish your project's source code and Javadoc documentation along with the compiled JAR. This helps consumers understand and debug your library.
The java plugin's withSourcesJar() and withJavadocJar() methods simplify creating these additional artifacts.
plugins {
id 'java'
id 'maven-publish'
}
group = 'com.coddykit.mylib'
version = '1.0.0'
java {
withSourcesJar()
withJavadocJar()
}
publishing {
publications {
mavenJava(MavenPublication) {
groupId = project.group
artifactId = 'my-awesome-lib'
version = project.version
from components.java
artifact sourcesJar // Add sources JAR
artifact javadocJar // Add Javadoc JAR
}
}
}
// These tasks are automatically created by withSourcesJar()/withJavadocJar()
// but shown here for clarity if you need custom configuration.
// task sourcesJar(type: Jar) { from sourceSets.main.allSource archiveClassifier.set('sources') }
// task javadocJar(type: Jar) { from javadoc.destinationDir archiveClassifier.set('javadoc') }Configuring Repositories
Now you need to tell Gradle *where* to publish your artifacts. This is done in the repositories block within publishing.
You can define multiple target repositories, including local ones for testing or remote ones like your company's Artifactory/Nexus instance.
plugins {
id 'java'
id 'maven-publish'
}
group = 'com.coddykit.mylib'
version = '1.0.0'
java { withSourcesJar(); withJavadocJar() }
publishing {
publications {
mavenJava(MavenPublication) { /* ... */ }
}
repositories {
mavenLocal() // Publishes to your local Maven cache (~/.m2/repository)
maven {
name = "myCompanyRepo"
url = uri("https://mycompany.com/maven-repo")
// Credentials will be added here
}
}
}Secure Credentials
For remote repositories, you'll almost always need authentication. It's vital to handle credentials securely and avoid hardcoding them directly in your build.gradle file.
- Environment Variables: Use
System.getenv('VAR_NAME')to read from system environment variables. - Project Properties: Access properties via
project.properties['propName'], which can be set ingradle.propertiesor passed via command line (-PpropName=value).
plugins { id 'java'; id 'maven-publish' }
group = 'com.coddykit.mylib'; version = '1.0.0'
java { withSourcesJar(); withJavadocJar() }
publishing {
publications { mavenJava(MavenPublication) { /* ... */ } }
repositories {
maven {
name = "myCompanyRepo"
url = uri("https://mycompany.com/maven-repo")
credentials {
// Securely retrieve username and password
username = project.properties['repoUser'] ?: System.getenv('REPO_USER')
password = project.properties['repoPass'] ?: System.getenv('REPO_PASS')
}
}
}
}Executing the Publish Task
Once your build.gradle is configured, Gradle automatically generates publishing tasks. The main task is publish, which publishes all defined publications to all configured repositories.
You can also target specific publications or repositories, e.g., publishMavenJavaPublicationToMyCompanyRepo.
# To publish all configured publications to all repositories:
gradle publish
# To publish a specific publication to a specific repository:
gradle publishMavenJavaPublicationToMyCompanyRepoPublishing Quiz
You are configuring a build.gradle file to publish a Java library to a remote repository named "MyCompanyRepo".
Recap: Publish with Gradle
Fantastic work! You've successfully navigated the essentials of publishing artifacts with Gradle.
You now know how to:
- Apply the
maven-publishplugin. - Define what artifacts to publish using a
MavenPublication. - Configure target repositories, including secure authentication.
- Execute Gradle publish tasks to share your project's outputs.
This skill is fundamental for creating reusable libraries and integrating with CI/CD pipelines!
เรียนรู้ Groovy ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การเผยแพร่ไปยังคลังเก็บ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การเผยแพร่ไปยังคลังเก็บ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Groovy & Gradle: JVM Automation and Build Engineering ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Groovy & Gradle: JVM Automation and Build Engineering มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การเผยแพร่ไปยังคลังเก็บ”
เผยแพร่อาร์ติแฟกต์ของโครงการไปยังคลังเก็บ Maven Central, Artifactory หรือ Nexus คุณปฏิบัติ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การเผยแพร่ไปยังคลังเก็บ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Groovy & Gradle: JVM Automation and Build Engineering นี้ได้ไหม
ได้ บทเรียน Groovy & Gradle: JVM Automation and Build Engineering ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การจัดแพ็กเกจ JAR, WAR, EAR
- การเผยแพร่ไปยังคลังเก็บ
- การผสานรวม CI/CD
- การกำหนดรุ่นและการทำกระบวนการเผยแพร่อัตโนมัติ