How to publish multiple artifacts (w/ sources) per project

Hello everyone,

Today I was able to upload a jar + sources for a simple test project to a maven repo. I just had to create a task which created the sources jar and add that archive in the artifacts section, like so:

artifacts {
 archives jar
 archives sourcesJar
}

The uploadArchives section doesn’t have anything special. Just the repo configuration.

In my real project I have an ant file that creates all jars (4 in total): A.jar, B.jar and sources for each. I’m currently able to upload both A and B with: https://gist.github.com/4688748

Now I’m trying to attach the sources by doing the same as my test project. Adding two more archives to my artifacts section which point to the sources, but no luck, they seem to go ignored and if I add more filters then they are uploaded as a separate artifact not as the sources.

The only thing that comes to mind to get the job done is to have 2 different configurations, with one artifact + source per configuration but I wouldn’t want to go that way. Is there any way do it with just the archive configuration?

Thanks in advance.

Hi Alejandro,

It depends on how you are doing this.

Do the different jars (code and source pairs) have different groups and/or names? In the maven model (I’m assuming you’re publishing to an m2 repo) you cannot have more than one artifact sharing what it terms “coordinates”.

I’d have to see more of your code to provide further help.

Thank you for your help Luke.

As I was preparing the Gist to share it with you, I tried a couple of different things I thought of and finally found the solution. I’m posting it here just in case anyone arrives here with a similar problem.

My problem was that I was adding the archives like this:

artifacts {
  archives file: 'A.jar', name: 'A', type: 'jar'
  archives file: 'A-sources.jar', name: 'A-sources', type: 'jar'
  archives file: 'B.jar', name: 'B', type: 'jar'
  archives file: 'B-sources.jar', name: 'B-sources', type: 'jar'
}

The solution I found is:

artifacts {
  archives file: 'A.jar', name: 'A', type: 'jar'
  archives file: 'A-sources.jar', name: 'A', type: 'jar', classifier: 'sources'
  archives file: 'B.jar', name: 'B', type: 'jar'
  archives file: 'B-sources.jar', name: 'B', type: 'jar', classifier: 'sources'
}

The uploadArchives is the same as the one I posted in https://gist.github.com/4688748

Thanks again for your help!

1 Like

hello ,I am getting this error when i try this

Cannot cast java.lang.String to java.io.File

Basically i do not have sources,instead i have put only jar files.

when i run gradle publish it gives this error

Cannot cast java.lang.String to java.io.File

Hi,

I would like to refresh the problem above. I’m trying to publish mutliple artifacts per project, and above solution seems not working any more.

My build.gradle looks like: gist:01b31c26eca7a507c14f · GitHub

I tried to follow the documentation: http://www.gradle.org/docs/current/userguide/maven_plugin.html#sub:multiple_artifacts_per_project

What I’m getting is:

  • What went wrong: Execution failed for task ‘:install’. > Could not publish configuration ‘archives’

A POM cannot have multiple artifacts with the same type and classifier. Already have MavenArtifact my:jar:jar:null, trying to add MavenArtifact my:jar:jar:null.

My env:

Gradle 2.2.1

Build time:

2014-11-24 09:45:35 UTC Build number: none Revision:

6fcb59c06f43a4e6b1bcb401f7686a8601a1fb4a

Groovy:

2.3.6 Ant:

Apache Ant™ version 1.9.3 compiled on December 23 2013 JVM:

1.7.0_71 (Oracle Corporation 24.71-b01) OS:

Windows 8.1 6.3 amd64

Thanks for your help.

Did you find this in the documentation? ‘archives new DefaultPublishArtifact(…)’

This looks wrong (A vs. B): ‘addFilter(“A”) {artifact, file -> artifact.name == “B” }’

Hi

@ archives new DefaultPublishArtifact(…) I have found this in one of the forums, as I tried many approaches to add the jars.

http://stackoverflow.com/questions/7826652/how-to-upload-an-existing-collection-of-3rd-party-jars-to-a-maven-server-in-grad/27416369#27416369

As you can see in the snippet, I also tried different approaches.

What I found in the documentation was the adding filters, when publishing mutliple artifacts, and that’s what I was refering to.

@ (A vs. B): addFilter(“A”) {artifact, file -> artifact.name == “B” } It’s just the spelling mistake, it will not work if it’s changed to:

addFilter(“A”) {artifact, file -> artifact.name == “A” }

I have just tried to simplify the problem I’m having.

If you think of any other solution to publish A.jar and B.jar, please share with me.

Thanks for such a quick response. Przemek

If you can’t get this to work with the ‘maven’ plugin (there should be a way), you could try to switch to the ‘maven-publish’ plugin, for which there is a ‘samples/maven-publish/multiple-publications’ sample in the ‘gradle-all’ download. Another option would be to create an additional configuration for the second artifact (which will give you an additional ‘upload’ task), rather than adding both artifacts to the same configuration and using ‘addFilter’ to tear them apart.

That’s the main problem - We cannot use the maven-publish plugin.There are some colateral impacts on our deply systems :frowning: Therefore I tried to use the solution above, which I found in many flavours all over the net. However, most of the solutions are written 2 years back. I’ll try to create multiple configs in the projects. In the meanwhile I hope someone will look into this issue. Thanks for idea.

I think you need to specify a different artifact name for the 2 jars. I’ve done that by specifying a baseName when defining the Jar task:

task testJar(type: Jar, dependsOn: testClasses) {

baseName = “${project.archivesBaseName}-test”

from sourceSets.test.output

}

artifacts {

archives testJar

}

Specifying the baseName in the Jar task results in the artifact getting a different name, and then there are no complaints when generating the POM.

I’ve tried your approach as well., it’s just another way of specifying the name of the artifact, which in my case doesn’t work. The working solution was to specify each of the jars as the separate gradle sub-project. Nevertheless, I’m still trying to make it work in the way the Alejandro Salas solved it.