简体   繁体   English

每个 Gradle 子模块创建/发布一个 jar

[英]Creating/publishing a jar per Gradle submodule

I'm trying to create a Spring Boot Starter project using Gradle, with the intention that each submodule becomes a different library (like how you have spring-boot-starter-web, spring-boot-starter-security , etc).我正在尝试使用 Gradle 创建一个 Spring Boot Starter 项目,目的是让每个子模块成为一个不同的库(比如你如何拥有spring-boot-starter-web、spring-boot-starter-security等)。

However I'm having difficulty in getting Gradle to generate the jars properly, and publishing them to a maven repository.但是,我很难让 Gradle 正确生成 jar,并将它们发布到 maven 存储库。 Ideally I'd like to use a DRY approach in Gradle, as I'd like the project to automatically generate a jar and publish for each submodule as the starter project grows (as few code changes as possible).理想情况下,我想在 Gradle 中使用 DRY 方法,因为我希望项目随着启动项目的增长(尽可能少的代码更改)自动生成一个 jar 并为每个子模块发布。

The structure of my project is:我的项目结构是:

base-repository:
- my-spring-boot-starter-base
- my-spring-boot-starter-module1

And I'd like this to publish the jars: my-spring-boot-starter-base.jar and my-spring-boot-starter-module1.jar .我想发布这些罐子: my-spring-boot-starter-base.jarmy-spring-boot-starter-module1.jar

Base build.gradle :基础build.gradle

import org.springframework.boot.gradle.plugin.SpringBootPlugin

plugins {
    id 'org.springframework.boot' version '2.6.7'
    id 'java'
    id 'java-library'
    id 'maven-publish'
}

allprojects {
    apply plugin: 'java'

    repositories {
        mavenCentral()
    }

    bootJar {
        enabled = false
    }
}

subprojects {
    apply plugin: 'java-library'

    dependencies {
        implementation platform(SpringBootPlugin.BOM_COORDINATES)
    }

    jar {
        enabled = true
    }

    publishing {
        publications {
            withSources(MavenPublication) {
                from components.java
            }
        }
        repositories {
            maven {
                url = 'https://my-repo'
            }
        }
    }
}

wrapper {
    gradleVersion = '7.4.2'
}

Base settings.gradle :基本settings.gradle

rootProject.name = 'my-spring-boot-starter'

include 'my-spring-boot-starter-base'
include 'my-spring-boot-starter-module1'

Both submodules have build.gradle files that look similar to:两个子模块都有build.gradle文件,看起来类似于:

dependencies {
   ...
}

The gradle config I have works when you have one submodule, but not multiple.当您有一个子模块而不是多个子模块时,我拥有的 gradle 配置有效。 With multiple, the build fails with the following error:使用多个,构建失败并出现以下错误:

A problem occurred evaluating root project 'my-spring-boot-starter'.
> Maven publication 'withSources' cannot include multiple components

Would appreciate some assistance!将不胜感激一些帮助!

So I found a way to solve my own problem.所以我找到了解决我自己问题的方法。 In the root build.gradle , change to use:在根build.gradle中,更改为使用:

subprojects {
    ...

    publishing {
        publications {
            "$project.name"(MavenPublication) {
                artifactId project.name
                from components.java
            }
        }
        repositories {
            maven {
                url = 'https://my-repo'
            }
        }
    }
}

project.name gets the name of the submodule, and we tell the maven-publish plugin to create an artifact per submodule using this field. project.name获取子模块的名称,我们告诉 maven-publish 插件使用该字段为每个子模块创建一个工件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM