0Pricing
Groovy & Gradle: JVM Automation and Build Engineering · 강의

저장소에 게시

프로젝트 아티팩트를 Maven Central, Artifactory 또는 Nexus 저장소에 게시합니다.

저장소에 게시은(는) CoddyKit의 무료 Groovy & Gradle: JVM Automation and Build Engineering 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 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 in gradle.properties or 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 publishMavenJavaPublicationToMyCompanyRepo

Publishing 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-publish plugin.
  • 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!

자주 묻는 질문

“저장소에 게시” 강의는 무료인가요?

네 — “저장소에 게시” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Groovy & Gradle: JVM Automation and Build Engineering 강의 전체를 잠금 해제할 수 있습니다. Groovy & Gradle: JVM Automation and Build Engineering 강의에는 총 4개의 강의가 포함되어 있습니다.

“저장소에 게시”에서 뭘 배우나요?

프로젝트 아티팩트를 Maven Central, Artifactory 또는 Nexus 저장소에 게시합니다. 브라우저에서 직접 실행하는 실습 코드로 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개 중 2번째 강의입니다.

“저장소에 게시” 강의는 얼마나 걸리나요?

대부분의 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(으)로 돌아가기