1
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.

gradleでMavenリポジトリからバージョンの違うtomcatのzipファイルを複数ダウンロードして展開する方法

Last updated at Posted at 2019-09-18

Mavenリポジトリから同じgroup, artifactのバージョン違いを複数ダウンロードして展開する

難しいところ

例えば、configurationの依存関係から、一つのjarだけ取り出すのはdependenciesブロックのDependencyを保存しておいて、configurationからfileCollectionで取り出すことで対象のjarファイル(正確にはprimary artifact)を取得できる。

configurations {
    CLI
}

dependencies {
    project.ext.swagger2 = CLI 'io.swagger:swagger-codegen-cli:2.4.2'
    project.ext.swagger3 = CLI 'io.swagger.codegen.v3:swagger-codegen-cli:3.0.5'
    project.ext.openApi = CLI 'org.openapitools:openapi-generator-cli:3.3.4'
}

task showCliJar {
    doFirst {
        println(project.configurations.CLI.fileCollection(project.ext.swagger2).singleFile)
        println(project.configurations.CLI.fileCollection(project.ext.swagger3).singleFile)
        println(project.configurations.CLI.fileCollection(project.ext.openApi).singleFile)
    }
}

しかし、同じConfigurationにバージョンの違う、同じartifactの依存関係を設定すると、バージョンの解決のために一つのartifactしか取得できない。
そのため、ほしい数だけのconfigurationを作成することもできるのだけど、configurations.detachedConfigurationで無名のconfigurationを作成できるので、それを使い捨てる感じでつかうと無用なconfigurationを宣言せずに行うことができる。

task downloadTomcatZip {
    doFirst {
        ["8.0.43","8.0.53","9.0.2"]
            .collect{ project.dependencies.create("org.apache.tomcat:tomcat:${it}@zip") }
            .collect{ project.configurations.detachedConfiguration(it) }
            .each { config ->
                project.copy {
                    from(project.zipTree(config.singleFile))
                    into("$buildDir/tomcat")
                }
            }
    }
}
1
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
1
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?