Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'
}
like image 657
tschumann Avatar asked Oct 28 '25 16:10

tschumann


2 Answers

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.

like image 162
tschumann Avatar answered Oct 31 '25 06:10

tschumann


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.

like image 30
dtrunk Avatar answered Oct 31 '25 06:10

dtrunk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!