How to include custom artifact in distribution in gradle

I'd like to know how to include a custom build artifact in the distribution zip built with gradle in a canonical way. I've managed to do it by referring to the path of the artifact directly, but I'm guessing there's some better way to achieve this:

task sourcesJar(type: Jar, dependsOn: tasks.classes) {
    classifier 'sources'
    from sourceSets.main.allSource
}

tasks.distZip.shouldRunAfter tasks.sourcesJar
tasks.distTar.shouldRunAfter tasks.sourcesJar

artifacts {
    archives sourcesJar
}

distributions {
    main {
        contents {
            from { "${libsDir}/${project.name}-${version}-sources.jar" }
        }
    }
}

How can I refer to the name of my sources artifact based on its definition, or alternatively is there an even better way to include built artifacts in the distributions?

Upvotes: 2

Views: 1660

Answers (2)

You can also just refer to the jar task directly like so:

distributions {
    main {
        contents {
            from { sourcesJar }
        }
    }
}

Upvotes: 1

Opal
Opal

Reputation: 84756

It might be e.g.:

apply plugin: 'java'
apply plugin: 'distribution'

task sourcesJar(type: Jar, dependsOn: tasks.classes) {
    classifier 'sources'
    from sourceSets.main.allSource
}

tasks.distZip.shouldRunAfter tasks.sourcesJar
tasks.distTar.shouldRunAfter tasks.sourcesJar

artifacts {
    archives sourcesJar
}

distributions {
    main {
        contents {
            from { tasks.sourcesJar.archivePath }
        }
    }
}

Upvotes: 1

Related Questions