How can I create a custom maven publication with dependencies?


(Kevin) #1

I have modified my build.gradle in a way that I get a custom jar file that copies in class files from a sibling project like this:

configurations {
    localCompile.transitive = false
    compile.extendsFrom(localCompile)
}

dependencies {
    localCompile project(":shared")
}

jar {
    from {
        configurations.localCompile.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

This creates a jar file that has the contents of this project aswell as the contents of the shared project.

Now I want to publish this to maven so I add the maven-publish plugin and the floolwing publication definition to my build.gradle:

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

This works mostly nicely the only problem is the generated pom lists the shared project alongisde all other dependencies from this project:

<dependency>
      <groupId>com.example</groupId>
      <artifactId>shared</artifactId>
      <version>0.0.1</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>

Since I’m not publishing that this dependency can never be resolved by any programms that consume my client library.

Instead I also tried to define the publication like this:

mavenJava(MavenPublication) {
    artifact jar
}

This works a bit better as it doen’t define any dependencies, but that is also not what I want because the dependencies are there.

A problem both methodes have is that the dependencies that the shared project has don’t appear in the client pom file.

What I would like to do is define a custom maven publication that includes the dependencies of my client library aswell as the dependencies of the shared siblingproject. But not include the sibling project as a dependency.

How can I do this?


(Kevin) #2

I found half an answer. If I create the publication like this:

mavenJava(MavenPublication) {
    artifact jar
    pom.withXml {
        def dependenciesNode = asNode().appendNode('dependencies')

        configurations.compile.allDependencies.stream()
                .filter {!configurations.localCompile.dependencies.contains(it)}
                .each {
            def dependencyNode = dependenciesNode.appendNode('dependency')
            dependencyNode.appendNode('groupId', it.group)
            dependencyNode.appendNode('artifactId', it.name)
            dependencyNode.appendNode('version', it.version)
            dependencyNode.appendNode('scope', 'compile')
        }
    }
}

It puts the dependencies of my client project (this project) into the pom file, but it dosen’t add the dependencies of my sibling project to the pom file.

How can I get the dependencies of a sibling project? Or can I get the transitive dependencies (only one layer deep) of a dependency?