Using ArtifactResolutionResult to create Uber Source Jar of project dependencies


(Judd) #1

I would like to create an Uber/Shadow/All Sources Jar. It is easy enough to create a binary JAR using the Shadow plugin , however creating a sources uber jar is not supported see, issue 112

I have attempted to use ArtifactResolutionQuery to find sources for dependencies however this is unable to resolve Project dependencies, it only works for Maven deps. Here is a simple sample project I created as a test case.

In summery:

ArtifactResolutionResult result = dependencies.createArtifactResolutionQuery()
            .forComponents(componentIds)
            .withArtifacts(JvmLibrary, SourcesArtifact, JavadocArtifact)
            .execute()

result.resolvedComponents does not contain SourcesArtifact for project deps.

Is there any other way I can obtain sources for project dependencies or is there an alternative to way of creating a source uber jar ?


(Benjamin Muschko) #2

Are you asking about generating a sources JAR file for source files of all subprojects in a multi-project build? If that’s the case it’s fairly easy to achieve. Here’s an example of a task defined in the root project.

task sourcesJar(type: Jar) {
    classifier = 'sources'

    subprojects.each { project ->
        from project.sourceSets.main.allSource
    }
}

(Judd) #3

Thanks, that does solve my problem with a slight tweak, using allprojects.each as I want the parent projects sources.

It does not give sources for external dependencies in a configuration but I don’t need that anyway.


(Judd) #4

A limitation for the accepted solution is that it does not work if the module itself is a sub project in a multi project build.