Programmatically obtaining source jars?

I am working on a plugin where I need to do some transformations to dependencies before they are added to the compile classpath. This I have been able to achieve without too much effort. However now that the transformations are made, I would like to apply the equivalent transformations to the source and javadoc jars if they are available. Is there any way I can request the source jars or anything from gradle like the way the binary artifacts are obtained?

If you specify the classifier as ‘sources’ or ‘javadoc’ in a dependency, you will receive the appropriate JAR file instead of the default dependency JAR file you receive without using a classifier. Expecting you don’t want to mix these in with the compile dependencies, you could duplicate the the dependencies in another configuration, add the classifier, and resolve the configuration behind the scenes.

// String Dependency Notation
compile "group:name:version:classifier@extension"

// Map Dependency Notation
comile group: group:, name: name, version: version, classifier: classifier, ext: extension

If either sources or javadoc JARs are not present you will receive the usual unresolvable dependency behavior, so you will likely want to hande that exception when they are not able to be resolved.

Alternatively, depending on how you are doing things, if it is not enough just to get the sources and javadoc JARs, but you need to process them before being included as attached sources or javadoc in an IDE, you may want to hook into the lifecycle and look specifically for the dependencies with the classifier specified that could be requested by the IDE plugins.

Another option is to make use of the ArtifactResolutionQuery if you don’t mind that this feature is still incubating. Details and an example are available in the DSL Reference for ArtifactResolutionQuery.