調査 #79
未完了Gradleで使用するライブラリとその依存ライブラリをダウンロードする
80%
説明
調査目的:ビルドマシンからインターネット接続はできないセキュアな環境で、mavenリポジトリ等から使用するライブラリを予めダウンロードし揃えられるようにする。
調査結果:Wiki Gradleの使い方 に記載する。
完了条件:gradleで所望のライブラリとその依存ライブラリを、他の不要な(依存していない)ライブラリと分離して揃える方法を見出す。
高徹 高橋 徹 さんが7年以上前に更新
mavenリポジトリにあるcom4jを対象にダウンロード方法の試行。
gradle 4.6を使用、作業ディレクトリに次のbuild.gradleを作成
apply plugin: 'java'
repositories {
    mavenCentral()
}
dependencies {
    compile 'org.jvnet.com4j:com4j:2.1'
}
	dependenciesの記述は、mavenリポジトリ(次URL)でcom4jを検索、
https://mvnrepository.com/
検索結果から、COM4Jに辿り(次URL)
https://mvnrepository.com/artifact/org.jvnet.com4j/com4j
最新バージョンの2.1を辿り、GradleタブをクリックするとGradle用の記述が出てくる。compile group: 'org.jvnet.com4j', name: 'com4j', version: '2.1'
これを、よく見かけるGradleの記述に形式を変えて記載したもの。
次に、gradle dependenciesを実行します。
実行結果を 表示
ここで、Downloadで始まる行を見ると、pomファイルだけダウンロードしています。
Gradleのローカルリポジトリ(%USERPROFILE%\.gradle\caches\modules-2\files-2.1\org.jvnet.com4j\...)を見ると、pomファイルだけ置かれています。
高徹 高橋 徹 さんが7年以上前に更新
mavenリポジトリにあるcom4jを対象にダウンロード方法の試行(別法1)。
次のブログの記載を元に試行した。
Gradleでjarの取得だけを行う-闘うITエンジニアの覚え書き
apply plugin: 'java'
def libDir = 'lib'
repositories {
    mavenCentral()
}
dependencies {
    compile 'org.jvnet.com4j:com4j:2.1'
}
task deleteDependencies << {
    delete libDir
}
task copyDependencies(dependsOn: deleteDependencies) {
    doLast {
        configurations.compile.each {
        def fromJarFile = it.absolutePath
        copy {
        from fromJarFile
        into libDir
        }
    }
    println "------ 以下のjarファイルを取得しました ------" 
    FileCollection copyFiles = files { file(libDir).listFiles() }
    copyFiles.each { File file -> println file.name }
    }
}
	実行結果は
$ gradle copyDependencies :deleteDependencies :copyDependencies Download https://repo.maven.apache.org/maven2/org/jvnet/com4j/com4j/2.1/com4j-2.1.jar ------ 以下のjarファイルを取得しました ------ com4j-2.1.jar Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0. See https://docs.gradle.org/4.6/userguide/command_line_interface.html#sec:command_line_warnings BUILD SUCCESSFUL in 3s 2 actionable tasks: 2 executed $ ls lib com4j-2.1.jar*
高徹 高橋 徹 さんが7年以上前に更新
mavenリポジトリにあるcom4jを対象にダウンロード方法の試行(別法2)。
次の掲示板の記載を元に試行した。
Download some dependencies and copy them to a local folder
apply plugin: 'java'
repositories {
    mavenCentral()
}
configurations {
    libraries
}
dependencies {
    libraries 'org.jvnet.com4j:com4j:2.1'
}
task copyLibraries(type: Copy) {
    from configurations.libraries
    into "lib" 
}
	実行結果は次のとおり。
$ gradle copyLibraries :copyLibraries BUILD SUCCESSFUL in 1s 1 actionable task: 1 executed $ ls lib com4j-2.1.jar*
高徹 高橋 徹 さんが7年以上前に更新
mavenリポジトリにあるcom4jを対象にダウンロード方法の試行(別法3)。
次の掲示板の記載を元に試行した。
How to download maven dependencies into project local directory and set eclipse classpath?
apply plugin: 'java'
repositories {
    mavenCentral()
}
dependencies {
    compile 'org.jvnet.com4j:com4j:2.1'
}
task copyDeps(type: Copy) {
    from (configurations.compile + configurations.testCompile)
    into "lib" 
}
	実行結果は
$ gradle copyDeps :copyDeps BUILD SUCCESSFUL in 1s 1 actionable task: 1 executed $ ls lib/ com4j-2.1.jar*
高徹 高橋 徹 さんが7年以上前に更新
mavenリポジトリにあるjunit5を対象にダウンロード方法の試行。
JUnit 5 User Guide によれば、GradleでJUnit 5を使用するときの依存関係記述は次のとおり。
dependencies {
    testCompile("org.junit.jupiter:junit-jupiter-api:5.1.0")
    testRuntime("org.junit.jupiter:junit-jupiter-engine:5.1.0")
}
	そこで、次のダウンロード用設定を記述
apply plugin: 'java'
repositories {
    mavenCentral()
}
dependencies {
    testCompile 'org.junit.jupiter:junit-jupiter-api:5.1.0'
    testRuntime 'org.junit.jupiter:junit-jupiter-engine:5.1.0'
}
task copyDeps(type: Copy) {
    from configurations.testRuntime
    into "lib" 
}
	実行結果
$ gradle copyDeps :copyDeps Download https://repo.maven.apache.org/maven2/org/junit/jupiter/junit-jupiter-api/5.1.0/junit-jupiter-api-5.1.0.pom Download https://repo.maven.apache.org/maven2/org/junit/jupiter/junit-jupiter-engine/5.1.0/junit-jupiter-engine-5.1.0.pom Download https://repo.maven.apache.org/maven2/org/junit/platform/junit-platform-commons/1.1.0/junit-platform-commons-1.1.0.pom Download https://repo.maven.apache.org/maven2/org/junit/platform/junit-platform-engine/1.1.0/junit-platform-engine-1.1.0.pom Download https://repo.maven.apache.org/maven2/org/junit/jupiter/junit-jupiter-engine/5.1.0/junit-jupiter-engine-5.1.0.jar Download https://repo.maven.apache.org/maven2/org/junit/platform/junit-platform-engine/1.1.0/junit-platform-engine-1.1.0.jar Download https://repo.maven.apache.org/maven2/org/junit/platform/junit-platform-commons/1.1.0/junit-platform-commons-1.1.0.jar Download https://repo.maven.apache.org/maven2/org/junit/jupiter/junit-jupiter-api/5.1.0/junit-jupiter-api-5.1.0.jar Download https://repo.maven.apache.org/maven2/org/apiguardian/apiguardian-api/1.0.0/apiguardian-api-1.0.0.jar Download https://repo.maven.apache.org/maven2/org/opentest4j/opentest4j/1.0.0/opentest4j-1.0.0.jar BUILD SUCCESSFUL in 3s 1 actionable task: 1 executed $ ls lib apiguardian-api-1.0.0.jar* junit-platform-commons-1.1.0.jar* junit-jupiter-api-5.1.0.jar* junit-platform-engine-1.1.0.jar* junit-jupiter-engine-5.1.0.jar* opentest4j-1.0.0.jar*
高徹 高橋 徹 さんが7年以上前に更新
前回ダウンロードしたJARを削除してからダウンロードする定義
  apply plugin: 'java'
+ def libDir = 'lib'
+  
  repositories {
      mavenCentral()
  }
  dependencies {
      testCompile 'org.junit.jupiter:junit-jupiter-api:5.1.0'
      testRuntime 'org.junit.jupiter:junit-jupiter-engine:5.1.0'
  }
+ task deleteDeps(type: Delete) {
+     delete libDir
+ }
- task copyDeps(type: Copy) {
+ task copyDeps(type: Copy, dependsOn: deleteDeps) {
      from configurations.testRuntime
      into libDir
  }
Gradleのタスク定義で、type: Copyやtype: Deleteは、タスクの設定を記述するので<<を付けない模様。