プロジェクト

全般

プロフィール

調査 #79

未完了

Gradleで使用するライブラリとその依存ライブラリをダウンロードする

高橋 徹 さんがほぼ6年前に追加. ほぼ6年前に更新.

ステータス:
解決
優先度:
通常
担当者:
カテゴリ:
-
対象バージョン:
-
開始日:
2018/04/07
期日:
進捗率:

80%

予定工数:

説明

調査目的:ビルドマシンからインターネット接続はできないセキュアな環境で、mavenリポジトリ等から使用するライブラリを予めダウンロードし揃えられるようにする。

調査結果:Wiki Gradleの使い方 に記載する。

完了条件:gradleで所望のライブラリとその依存ライブラリを、他の不要な(依存していない)ライブラリと分離して揃える方法を見出す。

高橋 徹 さんがほぼ6年前に更新

  • ステータス新規 から 進行中 に変更
  • 進捗率0 から 50 に変更

Gradleでライブラリのダウンロード方法を、インターネット上で調査し、手順の候補を挙げる。

  • gradle dependenciesコマンドで依存関係をプリントするが副作用でライブラリをダウンロードするという記述が散見された
  • 専用のタスクを記述しダウンロードする方法を紹介しているブログがあった

高橋 徹 さんがほぼ6年前に更新

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ファイルだけ置かれています。

高橋 徹 さんがほぼ6年前に更新

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*

高橋 徹 さんがほぼ6年前に更新

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*

高橋 徹 さんがほぼ6年前に更新

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*

高橋 徹 さんがほぼ6年前に更新

configurations.compile/runtime の違い

compile: コンパイルに必要な依存関係
runtime: 実行に必要な依存関係でcompileを含む
testCompile: テストコードのコンパイルに必要な依存関係で、コンパイルされたコードとcompileを含む
testRuntime: テストコードの実行に必要な依存関係で、compile, runtime, testCompileを含む

ということで、コピータスクで指定する際は、compileよりruntimeが適切と思われる。

高橋 徹 さんがほぼ6年前に更新

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*

高橋 徹 さんがほぼ6年前に更新

前回ダウンロードした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は、タスクの設定を記述するので<<を付けない模様。

高橋 徹 さんがほぼ6年前に更新

  • 説明 を更新 (差分)
  • ステータス進行中 から 解決 に変更
  • 進捗率50 から 80 に変更

調査結果の記載をした。

他の形式にエクスポート: Atom PDF