Hi,
Gradle 1.4 Java 1.7u17
I have a project that has a module that produces a WAR. I’m able to use “gradle uploadArtifacts” and that will upload the WAR with it’s sources to my repository.
If I want to share only the classes of the WAR and the Sources of those classes in a separate JAR, I’m a little lost. So far, I have done this:
In the module, I have defined a configuration like this:
configurations {
api
apiSources
}
task api(type: Jar) {
description = 'Creates a JAR containing API classes'
classifier = 'api'
from sourceSets.main.output.classesDir
}
task apiSources(type: Jar) {
description = 'Creates a JAR containing API sources'
classifier = 'api-sources'
from sourceSets.main.allSource
}
artifacts {
archives api
archives apiSources
}
Then, in my other project, which requires those classes, I have this:
dependencies {
compile group: 'com.fubar', name: 'api', version: "${fubarApiVersion}", classifier: 'api'
}
This works grand, in that gradle pulls down the JAR containing the API classes.
However, what is not happening, is that the SOURCES for that API jar are not being pulled down (they do exist in the repository under the name of compile(group: ‘com.fubar’, name: ‘api’, version: ‘3.7.0-SNAPSHOT’, classifier: ‘api-sources’)).
Please may I have some help so that I can successfully pull down the sources as well as the classes for the API.
Thank you.
-=david=-
The sources will only be pulled down when invoking one of the IDE tasks or adding the sources as explicit dependencies. Are you saying that one of these isn’t working?
Hi Peter,
Interesting. I did issue ‘gradle idea’ and restarted IntelliJ after configuring my build script. When I expanded out the jar, there were no sources shown (IntelliJ did it’s best at decompiling the class.)
I’ll have another look.
-=david=-
Hi,
Re-invoked “gradle idea”, it built all the .iml files, reopened project, expanded out jar, hit a class, but no sources shown, just a decompiled attempt.
Further information:
Here is the output of the cache directory
.
./pom
./pom/334b1a0f74087a4ae8898e1ff54a58141f42333e
./pom/334b1a0f74087a4ae8898e1ff54a58141f42333e/api-3.7.0-SNAPSHOT.pom
./jar
./jar/a88c23caffdc4fb6ad00c8936fcfde615930470
./jar/a88c23caffdc4fb6ad00c8936fcfde615930470/api-3.7.0-SNAPSHOT-api.jar
./source
./source/caabb6e7a6e9980aecc2a3504e379b2d571092d7
./source/caabb6e7a6e9980aecc2a3504e379b2d571092d7/api-3.7.0-SNAPSHOT-sources.jar
They all have the same timestamp (12:16 - I deleted the cache’d directory and redid ‘gradle idea’)
Notice the sources have been downloaded, but they don’t seem to be found…
Is this because…
a. it has -sources and not -api-sources? (I thought I was building and publishing api-sources in my snippet above) b. intellij can’t match these two jars up? c. the idea plugin for gradle can’t work it out that this -api jar also matches the -sources jar?
-=david=-
From what I know, you can only have one sources Jar per Maven module, and it cannot have a classifier. I believe this is even the case when using Maven.
In general, I’d lean towards having a separate Gradle project that produces the Jar, and adding it as a dependency to the War project. This will make things easier.
From what I know, you can only have one sources Jar per Maven module, and it cannot have a classifier. I believe this is even the case when using Maven.
There indeed is no standard convention for having source jars of artifacts with classifiers. This is one of the problems of using classifiers for variants.
Maybe things would work if you found a way to fix (a). First step would be to check if ‘api-3.7.0-SNAPSHOT-sources.jar’ contains the expected sources. The fact that it isn’t named ‘api-sources’ makes me doubt that.
The matching is done by Gradle’s ‘idea’ plugin (have a look at the ‘.iml’ file). I’m not sure how/if it does the matching for artifacts with a classifier.
Thank you Peter for your helpful comments. I will experiment with a few things and hopefully post some results. 
-=david=-
Hi,
Following your advice, I created a new project called “fubar-classes” that contained source files, then put a dependency on “fubar-classes” in the “fubar” project. I now have a separate project that uploads as separate artifact and includes the sources jar as expected.
All is well.
Thank you for your helpful advice 
-=david=-