How do you use 'maven-publish' on a static JAR file and dependencies?

I have pre-built JAR. Can maven-publish plugin be used to publish this, and also include a defined set of dependencies?

Yes, you can publish a pre-built JAR file. Additional dependencies could be added using the pom.withXml hook. Here’s an example:

publishing {
    publications {
        maven(MavenPublication) {
            artifact 'my-file-name.jar'
                          pom.withXml {
                def dependency = asNode().appendNode('dependencies').appendNode('dependency')
                dependency.appendNode('groupId', 'commons-lang')
                dependency.appendNode('artifactId', 'commons-lang')
                dependency.appendNode('version', '2.7')
            }
        }
    }
}

Thanks I appreciate the response. I was hoping I could just inject a configuration to the publication. Kinda like how a software component already supports this. That way I can stick with the common dependency definition syntax. Maybe this could be a feature added in the future?

Right now we have a set of closed-source JARs that I need to create POMs for and upload to our maven server. Can be done many ways, but I like putting all this logic into a Gradle script for consistency across our whole project.

It wouldn’t be hard to code this up in a Gradle script, but seems like a lot of people could use this feature.

Oh yes, we also would like to have more convenient support with respect to the maven-publish based POM creation: please see below for our current convenience abuse

apply plugin: ‘maven-publish’

apply plugin: “java” // ! abuse

dependencies {

runtime project(path: ‘:c.c.a.ajavaproject’)

runtime project(path: ‘:anotherNonJavaProject’) // ! a zip task is added to archives artifacts in ‘anotherNonJavaProject’

}

publishing {

publications {

product(MavenPublication) {

from components.java // ! abuse

artifacts = []

}

}

}

I think just pointing to a (dependency) configuration to add all its dependencies would sound ideal. Or is there a better approach already, that we have missed?