如何配置gradle解决依赖关系

时间:2019-09-07 15:19:17

标签: java gradle subproject

我正在尝试将我的MAVEN版本移至gradle 因此,我需要解决以下问题:

项目:

  • 应用程序(此应用程序将创建一个胖子)

插件(应生成单个jar文件,但不包含胖jar!)

  • utils(需要:jarX,jarY,jarZ)
  • 数据(需要:utils,jarX,jarY)
  • 控件(需要:数据,utils,jarY)

我得出的结论是为每个插件创建一个jar(但是我必须完全定义其依赖项,这实际上是gradle的工作

所以我要做的是创建jar文件,将其手动复制到reposiory并声明对这些文件的依赖...

所以我期望的是,我只需要告诉应用程序需要控件,因为控件已经包含了所有需要的东西(总是包括下面内容的依赖项)

我想我必须定义一个项目(是的,我阅读了gradle的帮助页面,但是我无法解决这个问题)

所以现在我在每个插件中都有settings.gradle,但是我读到的是错误的。 我的根目录应该只有一个settings.gradle。

ROOT:settings.gradle:

include ':application', ':controls', ':data', ':util'

ROOT:build.gradle

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.glazedlists:glazedlists:1.11.0'
        classpath 'org.jooq:jooq:3.12.1'
        classpath 'org.controlsfx:controlsfx:8.40.12'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

,每个插件都有: build.gradle

plugins {
    id 'java-library'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

但是这也不起作用...

预先感谢

1 个答案:

答案 0 :(得分:0)

假设您需要进行多项目设置,请执行以下操作:

settings.gradle

rootProject.name = 'mvn2Gradle'
include ':application', ':controls', ':data', ':util'

build.gradle

allprojects {
    repositories {
        google()
        jcenter()
    }
}

subprojects {
    apply plugin: 'java'

    ext {
        jarYVersion = '1.x'
    }

    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    dependencies {
        implementation "org.example:jarY:$jarYVersion" // Every subproject gets this dependency
    }
}

wrapper { // Run this task once before you start building
    gradleVersion = '5.6.3'
    distributionType = Wrapper.DistributionType.ALL
}

utils:build.gradle

plugins {
    id 'java-library'
}

ext {
    jarXVersion = '1.x'
    jarZVersion = '1.x'
}

dependencies {
    implementation "org.example:jarX:$jarXVersion"
    api "org.example:jarZ:$jarZVersion" // "api" because none of the other projects depends on this jar
}

data:build.gradle

plugins {
    id 'java-library'
}

dependencies {
    implementation project(':utils') // Also inherits "jarX"
}

controls:build.gradle

plugins {
    id 'java-library'
}

dependencies {
    implementation project(':data') // Also inherits "project:utils"
}

application:build.gradle

plugins {
    id 'application'
}

application {
    mainClassName 'my.package.Main'
}

dependencies {
    implementation project(':controls')
}