Thanks to this article, we can copy source jar files into directory with Gradle 2.x. But the script doesn't work in Gradle 5.1. So that I made Gradle 5.x compatible version.
この記事で提供いただいているスクリプトを使うことで Gradle 2 系で source jar を任意のディレクトリにコピーすることができます。しかし当該スクリプトは Gradle 5 系では動作しなかったため、Gradle 5 対応版を作成しました。
import org.gradle.api.artifacts.query.ArtifactResolutionQuery
import org.gradle.internal.component.external.model.DefaultModuleComponentIdentifier
import org.gradle.jvm.JvmLibrary
import org.gradle.language.base.artifact.SourcesArtifact
// Usage example: collectSourcesJar(configurations.compile, "./my-source-jar-directory")
// Specify configurations.testCompile to copy source jars of test dependencies.
def collectSourcesJar(configuration, directory) {
DependencyHandler dependencyHandler = project.dependencies
def componentIdentifiers = configuration.resolvedConfiguration.lenientConfiguration.getArtifacts { Dependency dependency ->
configuration.dependencies.contains(dependency)
}.collect { ResolvedArtifact artifact ->
ModuleVersionIdentifier id = artifact.moduleVersion.id
DefaultModuleComponentIdentifier.newId(id)
}
ArtifactResolutionQuery query = dependencyHandler.createArtifactResolutionQuery()
query.forComponents(componentIdentifiers)
query.withArtifacts(JvmLibrary, SourcesArtifact)
def sourcesJars = []
ArtifactResolutionResult resolutionResult = query.execute()
resolutionResult.resolvedComponents.each { ComponentArtifactsResult componentArtifactsResult ->
componentArtifactsResult.getArtifacts(SourcesArtifact).each { ArtifactResult artifactResult ->
if (artifactResult instanceof ResolvedArtifactResult) {
sourcesJars << artifactResult.file
}
}
}
project.copy {
from sourcesJars
into directory
}
}