2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Copy source jar files into directory (Gradle 5 compatible)

Last updated at Posted at 2019-04-22

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
    }
}
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?