Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

gradle - BootJar + MavenJar. Artifact wasn't produced by this build

I have a sample project with the following hierearhy:

Sample (root)
   -- model (simple jar)
   -- api   (springboot jar)

I want to publish both generated jars: plain jar & bootJar to my localRepository.

gradlew clean build -xTest publishToMavenLocal    

However, the following error occures:

* What went wrong:
Execution failed for task ':api:publishMavenJavaPublicationToMavenLocal'.
> Failed to publish publication 'mavenJava' to repository 'mavenLocal'
   > Artifact api.jar wasn't produced by this build.

The root build.gradle is a follows:

plugins {
    id 'java'
    id "org.springframework.boot" version "2.2.5.RELEASE" apply false
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
}
group 'sample'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}
ext {
    artifactVersion = version
    springBootVersion = "2.2.5.RELEASE"
}

allprojects {
    apply plugin: 'java'
    apply plugin: 'idea'
    apply plugin: 'maven'
    tasks.withType(JavaCompile) {
        options.encoding = 'UTF-8'
    }
    repositories {
        mavenCentral()
        jcenter()
    }
}

subprojects {
    apply plugin: "io.spring.dependency-management"
    apply plugin: "maven-publish"

    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8

    dependencyManagement {
        imports {
            mavenBom "org.springframework.boot:spring-boot-dependencies:${springBootVersion}"
        }
    }
    dependencies {
        implementation "org.springframework.boot:spring-boot-dependencies:${springBootVersion}"
    }

    publishing {
        publications {
            mavenJava(MavenPublication) {
                groupId project.group
                artifactId project.name
                version project.version

                from components.java
            }
        }
    }
}

api build.gradle

apply plugin: 'org.springframework.boot'

dependencies {
    compile project(":model")
    implementation "org.springframework.boot:spring-boot-starter-web"
}

bootJar {
}

Adding bootJava task to api build.gradle allowes to publish the bootJar directly from api module, but the root publish task remains broken.

publishing {
    publications {
        bootJava(MavenPublication) {
            artifact bootJar
        }
    }
}

I've tried almost every solution from docs & google, but none seem to work. Can anyone explain, what is misconfigured?

Gradle version: 6.3

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As stated by gradle documentation here:

Starting from Gradle 6.2, Gradle performs a sanity check before uploading, to make sure you don’t upload stale files (files produced by another build). This introduces a problem with Spring Boot applications which are uploaded using the components.java component

More explanation is available in the link above. They propose the following workaround that I personally tried and worked for me :

configure the outgoing configurations

configurations {
       [apiElements, runtimeElements].each {
           it.outgoing.artifacts.removeIf { it.buildDependencies.getDependencies(null).contains(jar) }
           it.outgoing.artifact(bootJar)
       }
    }

here after the configuration from my build.gradle:

....
apply plugin: 'maven-publish'
...

configurations {
    [apiElements, runtimeElements].each {
        it.outgoing.artifacts.removeIf { it.buildDependencies.getDependencies(null).contains(jar) }
        it.outgoing.artifact(bootJar)
    }
    ....
}


publishing {
    publications {
        myPublication(MavenPublication) {
            groupId groupId
            artifactId artifactId
            version version
            from components.java
            versionMapping {
                usage('java-api') {
                    fromResolutionOf('runtimeClasspath')
                }
                usage('java-runtime') {
                    fromResolutionResult()
                }
            }
        }
    }
    repositories {
        maven {
            url azureRepoUrl
            name azureRepoName
            credentials {
                username azureRepoUserName
                password azureRepoAccessToken
            }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...