简体   繁体   中英

sourcesJar task in gradle 5

I have inherited a codebase which I suspect was originally built with Gradle 4 (but I don't know for sure). I am using Gradle 5.5.1 and when I run gradle I get errors to do with publication to a Maven repo:

* What went wrong:
A problem occurred evaluating root project 'common'.
> Could not find method sourcesJar() for arguments [build_d1u03z05r8d12r3e8b5qq1fxm$_run_closure3$_closure13$_closure15$_closure16@190bc2b8] on object of type org.gradle.api.publish.maven.internal.publication.DefaultMavenPublication.

Add sourcesJar task to custom Gradle plugin looks like a similar problem but it is a different error and their solution doesn't work anyway.

The relevant parts of my build.gradle are:

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
            artifact sourcesJar {
                classifier "sources"
            }
            artifact testJar {
                classifier "tests"
            }
        }
    }
    repositories {
        maven {
            url 'http://repo.url'
            credentials {
                username "$username"
                password "$password"
            }
        }
    }
}

task sourcesJar(type: Jar) {
    from sourceSets.main.allSource
    classifier = 'sources'
}

task testJar(type: Jar) {
    from sourceSets.test.output
    classifier = 'tests'
}

Okay I think I figured it out: https://docs.gradle.org/5.5.1/userguide/publishing_maven.html#publishing_maven:deferred_configuration says that a publishing block was executed after the rest of the project in Gradle 4, but not in Gradle 5.

So, changing

artifact sourcesJar {
   classifier "sources"
}
artifact testJar {
   classifier "tests"
}

to

afterEvaluate {
    artifact sourcesJar {
        classifier "sources"
    }
    artifact testJar {
        classifier "tests"
    }
}

got me a little further. With that change I then got this error:

* What went wrong:
A problem occurred configuring root project 'common'.
> Cannot create a Publication named 'sourcesJar' because this container does not support creating elements by name alone. Please specify which subtype of Publication to create. Known subtypes are: MavenPublication

https://discuss.gradle.org/t/cannot-create-a-publication-named-x/3726 and Gradle: Using 'maven-publish' plugin in custom standalone plugin seem to suggest that a prefix of project. should fix it.

So changing it to:

afterEvaluate {
    artifact project.sourcesJar {
        classifier "sources"
    }
    artifact project.testJar {
        classifier "tests"
    }
}

seems to work, though I'm a little iffy on the project. prefix.

In my case I had to add the following that Gradle was aware about the artifacts:

java {
    withJavadocJar()
    withSourcesJar()
}

Then I was able to use it this way:

publishing.publications {
    mavenJava(MavenPublication) {
        from components.java
    }
}

Javadoc as well as Sources were published. There seems to be no need to add the afterEvaluate block.

In this block:

artifact sourcesJar {
   classifier "sources"
}
artifact testJar {
   classifier "tests"
}

remove the closures...just make it look like this:

artifact sourcesJar
artifact testJar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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