Can't Publish Custom-named Jar to Maven


(tobyp) #1

I’m having some problems getting a gradle build to work as I want it to. I am gradle noob, so I may be approaching this wrongly (though I have looked hard at the docs/forum/stack overflow).

I have a single Java project that contains a bunch of interdependent source and that needs to be packaged in several independent jar files where each jar is an OSGI bundle (the packaging into bundles is for OSGI purposes).

We are using the bnd gradle plugin (biz.aQute.bnd.builder) to prepare the manifest and build the jar files.

For each package we want to produce I have added a task of type Bundle (aQute.bnd.gradle.Bundle). Within that task I have specified:

  • A custom name for the resulting jar
  • An include to identify the classes to add
  • Manifest attributes

For example:

task bundle_lib(dependsOn: ['classes'], type: Bundle) {
	from sourceSets.main.output

	baseName = baseName + '.' + 'lib'

	include([
	   'src/pattern/to/include'
	   //...
	   ])  
	manifest {
		attributes(
			'Bundle-Version': project.version
			//...
		)		
	}
}

So that these bundle tasks are hooked into the build appropriately in relation to the other default tasks (e.g. assemble) I have made each of these bundle tasks dependencies of the default java task. I also removed the actions of that default task (as we are generating the jar in the bundle tasks). So my build.gradle has this in relation to the jar task:

jar {
	dependsOn(['bundle_lib'])
	deleteAllActions()
}

This is working largely as required. But now I want to publish the generated jar files to a Maven repo and I can’t get this to work.

Whilst the publication step is working, the current problem I have is that, however I add my artifact to the MavenPublication block, the custom name I have used for the bundle jar is ignored and the default is used (it uses the default baseName i.e. the root project name, rather than the name I have used).

publishing {
	publications {
		mavenJava(MavenPublication) {
			// all of these have the same result
			//artifact bundle_lib
			//artifact bundle_lib.archivePath
			//artifact 'path/to/core-0.0.1.jar'
			//artifact file ('path/to/core-0.0.1.jar')
		}
	}
	...
}

How can I publish the correctly named artifact to Maven?


(Alexander Volanis) #2

I believe what you are attempting to do requires multiple publications, one per jar file. The Maven Pom model supports only one main artifact named the same as the pom.xml base name. Additional artifacts are expected to come with a classifier to distinguish from the main artifact but regardless all of them have the same base name.

You would need to create multiple publications like this:

publishing {
    publications {
       bundleOne(MavenPublication) {
           artifactId = 'core' // this is the base name of the published jar file which can be different from the name used in the bundle task
           artifact bundle_lib // this is the task creating the jar file
       }
       bundleTwo(MavenPublication) {
           artifactId = 'service'
           artifact bundle_service
       }
    }
}

(tobyp) #3

Many thanks, Alex. That helped.

Using multiple mavenPublications and realising that artifactId in conjunction with groupId and version give you all the control necessary over what gets published has resolved the issue.