15

Gradleプラグインspring-boot-dependenciesspring-bootを使用するマルチモジュールプロジェクトでは、正しいGradle設定はどのようになりますか?マルチモジュールのGradle依存プラグインSpringブートプロジェクト

私は次のプロジェクトを設定している:

parent 
    | 
    + build.gradle 
    | 
    + alpha 
    | | 
    | + build.gradle 
    | 
    + beta 
    | | 
    | + build.gradle 
  • parentモジュールは、一般的なプロジェクト構成が含まれています。
  • alphaモジュールは、spring-boot-dependencies bomに指定されたバージョン番号を使用して依存関係をインポートするモジュールですが、その結果は標準のjarです。
  • betaモジュールはalphaに依存するモジュールであり、結果は実行可能なSpringブートjarファイル(すべての依存関係を含む)です。したがって、このプロジェクトにはspring-boot-dependenciesspring-bootプラグインの両方が必要です。

GradleファイルDRYを保持するために、私は共通のモジュールスクリプトを親のbuild.gradleファイルに抽出しました。親が

0 build.gradle

allprojects { 
    group = "com.example" 
    version '0.0.1-SNAPSHOT' 

    ext { 
     dependencyManagementPluginVersion = '0.5.3.RELEASE' 
     springBootVersion = '1.3.0.RC1' 
    } 

    apply plugin: 'java' 
    apply plugin: 'eclipse' 
    apply plugin: 'idea' 
} 

subprojects { 
    sourceCompatibility = 1.8 
    targetCompatibility = 1.8 

    buildscript { 
     repositories { 
      jcenter() 
      maven { url "https://repo.spring.io/snapshot" } 
      maven { url "https://repo.spring.io/milestone" } 
     } 
     dependencies { 
      classpath("io.spring.gradle:dependency-management-plugin:${dependencyManagementPluginVersion}") 
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
     } 
    } 

    apply plugin: 'io.spring.dependency-management' 

    dependencyManagement { 
     imports { 
      mavenBom("org.springframework.boot:spring-boot-dependencies:${springBootVersion}") 
//   mavenBom("org.springframework.boot:spring-boot-starter-parent:${springBootVersion}") 
     } 
    } 
} 

アルファgradle.build

> Plugin with id 'io.spring.dependency-management' not found. 

:に結果以下プロジェクト構成を使用して$ gradle buildを実行する

試み件の

ベータgradle.build

apply plugin: 'spring-boot' 

dependencies { 
    compile project(':alpha') 
    compile('org.springframework.boot:spring-boot-starter') 
    compile('org.springframework.boot:spring-boot-starter-web') 
} 

コメント:

  • 春ブーツでspring-bootプラグインwas changedの行動1.3.0.M1
  • Gradleのバージョン:2.8
  • 春ブートバージョン1.3.0.RC1
+0

私の答えが役に立ちましたら、アップしてください。 – Opal

+1

@Opal遅れて申し訳ありませんが、私はあなたの答えを受け入れたと思いました。私は[ブログ投稿](http://www.jayway。com/2015/11/23/reusing-spring-boots-dependency-management /)については、謝辞を確認してください。 :-) – matsev

+0

ありがとう、ちょうど私が持っていた問題---他の人がこの問題を拾い読みするのを助けるために、私は追加します:あなたが素朴に試してみると:あなたのサブプロジェクトにプラグイン 'spring-boot' -management for free)あなたはビルドエラー(少なくとも私はウィンドウの下にあります)を得ます: タスク ':xxxxx:bootRepackage'の実行に失敗しました。 'xxxxx.jar'の名前を 'xxxxx.jar.original'に変更できません – Rop

答えて

16

それはparent/build.gradleは、次のように再配置する必要があることが判明:

buildscript { 
    ext { 
     dependencyManagementPluginVersion = '0.5.3.RELEASE' 
     springBootVersion = '1.3.0.RC1' 
    } 
    repositories { 
     jcenter() 
     maven { url "https://repo.spring.io/snapshot" } 
     maven { url "https://repo.spring.io/milestone" } 
    } 
    dependencies { 
     classpath("io.spring.gradle:dependency-management-plugin:${dependencyManagementPluginVersion}") 
     classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
    } 
} 

allprojects { 
    group = "com.example" 
    version '0.0.1-SNAPSHOT' 

    apply plugin: 'java' 
    apply plugin: 'eclipse' 
    apply plugin: 'idea' 
} 

subprojects { 
    sourceCompatibility = 1.8 
    targetCompatibility = 1.8 

    apply plugin: 'io.spring.dependency-management' 

    dependencyManagement { 
     imports { 
      mavenBom("org.springframework.boot:spring-boot-dependencies:${springBootVersion}") 
//   mavenBom("org.springframework.boot:spring-boot-starter-parent:${springBootVersion}") 
     } 
    } 
} 

問題は、サブプロジェクトのためbuildscriptブロックが実際にうまく構成されていたという事実にあるけど。 ..間違った場所に。このsubprojectsブロックはサブプロジェクトに関連しますが、スクリプトでと評価され、適用しようとしたプラグインに対して宣言された依存関係はありませんでした。

+1

ありがとうございますが、うまくいきません。現在、Gradleはサブモジュールのリポジトリを見つけることができません: '>外部依存関係org.springframework.bootを解決できません:spring-boot-dependencies:1.3.0.RC1リポジトリが定義されていないためです。 必須: com.example:alpha:0.0.1-SNAPSHOT' 'リポジトリ'を 'allprojects'にコピーすると問題は解決しますが、'リポジトリ '_once_を定義して両方からアクセスできるようにする方法があります'buildscript'と' allprojects'ですか? – matsev

+0

いいえ:/残念ながら、これは別の設定です。それは複製する必要があります。 – Opal

関連する問題