0Pricing
Groovy & Gradle: JVM Automation and Build Engineering · 课时

发布到仓库

将项目构件发布到 Maven Central、Artifactory 或 Nexus 仓库。

发布到仓库 是 CoddyKit 上的免费 Groovy & Gradle: JVM Automation and Build Engineering 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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!

常见问题解答

「发布到仓库」课时是免费的吗?

是的 — 「发布到仓库」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 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 导师会在你学习这节课的过程中回答你的问题。

学习 Groovy & Gradle: JVM Automation and Build Engineering 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Groovy & Gradle: JVM Automation and Build Engineering 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「发布到仓库」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Groovy & Gradle: JVM Automation and Build Engineering 课中编写并运行代码吗?

能。每节 Groovy & Gradle: JVM Automation and Build Engineering 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 打包 JAR、WAR 与 EAR
  2. 发布到仓库
  3. 持续集成与持续交付
  4. 版本管理与发布自动化
← 返回 Groovy & Gradle: JVM Automation and Build Engineering