使用多模块 gradle 发布 Artifactory

Posted

技术标签:

【中文标题】使用多模块 gradle 发布 Artifactory【英文标题】:Artifactory publish with multi-module gradle 【发布时间】:2021-11-22 08:21:53 【问题描述】:

希望一切顺利。

今天我正在研究多模块 gradle 项目,并尝试将其中一个依赖项发布到 artifactory

我的项目结构是:

build.gradle(主)

buildscript 
    ext 
        appVersion = "0.0.2-RC3"
        appBuildCode = 2

        // dependencies version
        springVersion = "2.6.0"

        swaggerApiVersion = "1.5.12"
        jjwtVersion = "0.11.2"
        lombokVersion = "1.18.22"

        // firebase versions
        firebaseAdminVersion = "8.1.0"
    

    repositories 
        mavenCentral()
        maven 
            url "https://plugins.gradle.org/m2/"
        
    

    dependencies 
        classpath("org.springframework.boot:spring-boot-gradle-plugin:$springVersion")
        classpath("org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:3.2.0")
        classpath("io.spring.gradle:dependency-management-plugin:1.0.11.RELEASE")
        classpath("org.jfrog.buildinfo:build-info-extractor-gradle:latest.release")
    


apply(plugin: "org.sonarqube")
apply(plugin: "jacoco")

allprojects 
    apply(plugin: 'java')
    apply(plugin: 'org.springframework.boot')
    apply(plugin: 'io.spring.dependency-management')


    group 'com.example'

    repositories 
        mavenCentral()

        maven 
            url "$artifactory_contextUrl/libs-release/"
            allowInsecureProtocol true
        
    

    configurations 
        all*.exclude module: 'spring-boot-starter-logging'
    

    dependencies 
    ....
    

build.gradle (api)

plugins 
    id 'java-library'
    id 'maven-publish'
    id 'com.jfrog.artifactory'


version "$appVersion"

artifactory 
    contextUrl = artifactory_contextUrl

    publish 
        contextUrl = artifactory_contextUrl

        repository 
            repoKey = artifactory_repoKey
            username = artifactory_user
            password = artifactory_password
        

        defaults 
            publishConfigs('published')
            publishIvy = false
            publications("mavenJava")
        
    


//jar 
//////    archiveName 'admin-api.jar'
//////    manifest
//////        attributes ("Fw-Version" : "2.50.00", "$parent.manifestSectionName")
//////    
//////    baseName 'admin-api'
////    archivesBaseName = "$archivesBaseName"
//
//    from 
//        configurations.runtimeClasspath.collect  it.isDirectory() ? it : zipTree(it) 
//    
//

task sourcesJar(type: Jar, dependsOn: classes) 
//    classifier = 'sources'
    from sourceSets.main.allSource


artifacts 
    archives sourcesJar


publishing 
    publications 
        mavenJava(MavenPublication) 
            from(components.java)
//            artifactId = 'admin-api'

            artifact(sourcesJar) 
                classifier 'sources'
            
        
    



task dist(type: Zip, dependsOn: build) 
    classifier = 'buildreport'

    from('build/test-results') 
        include '*.xml'
        into 'tests'
    

    from('build/reports/codenarc') 
        into 'reports'
    

    from('build/docs') 
        into 'api'
    

    from(sourcesJar) 
        into 'source'
    

    from('build/libs') 
        exclude '*-sources.jar'
        into 'bin'
    


//artifactoryPublish.dependsOn('clean', 'build', 'groovydoc', 'sourcesJar', 'dist')
//publish.dependsOn(artifactoryPublish)

任务artifactoryPublish 产生:

> Task :admin-api:extractModuleInfo
[pool-7-thread-1] Deploying artifact: http://10.0.0.3:8081/artifactory/libs-release-local/com/example/admin-api/0.0.2-RC3/admin-api-0.0.2-RC3-plain.jar
[pool-7-thread-1] Deploying artifact: http://10.0.0.3:8081/artifactory/libs-release-local/com/example/admin-api/0.0.2-RC3/admin-api-0.0.2-RC3.module
[pool-7-thread-1] Deploying artifact: http://10.0.0.3:8081/artifactory/libs-release-local/com/example/admin-api/0.0.2-RC3/admin-api-0.0.2-RC3.pom
> Task :artifactoryDeploy

现在的问题是,

模块,如admin-api 使用名称admin-api-0.0.2-RC3-plain.jar 构建,并且在另一个项目中解析依赖项失败,需要通过报告以下问题来使用此API:

Could not resolve com.example.admin-api:0.0.2-RC3.
Required by:
    project :admin-api

Possible solution:
 - Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html

gradle 依赖声明:

dependencies 
    compileOnly('com.example:admin-api:0.0.2-RC3')

请帮助我解决这里出了什么问题。

另外,他们有什么方法可以让模块jar 构建没有后缀-plain

提前致谢:)

【问题讨论】:

【参考方案1】:

您好,我终于通过以下配置实现了这一点:

allprojects     
    ....
    bootJar 
        enabled = false
    
    
    jar 
        archiveClassifier.convention('')
        archiveClassifier.set('')
    
    ....

【讨论】:

以上是关于使用多模块 gradle 发布 Artifactory的主要内容,如果未能解决你的问题,请参考以下文章

多模块 Spring Boot 项目中的 Gradle 依赖插件

Gradle多项目构建与jar包发布

使用 Spring Boot 从 Gradle 多模块项目构建的 Jar 不起作用

使用gradle BOM和spring-boot(多模块应用程序)哪种方式更好

Gradle 5 Kotlin DSL:多模块项目中的常见任务和 Maven 工件

Spring boot 和 Gradle 多模块项目,无法正确加载依赖项