如何解决多项目Gradle构建中的依赖关系问题

时间:2018-12-21 08:05:42

标签: gradle

我正在尝试对依赖于Astrix子项目的Alpha子项目进行gradle clean构建,而Alpha子项目的build.gradle如下

group 'com.loyalty.alpha'
version '0.7.23'

repositories {
maven { url "https://repository.cloudera.com/artifactory/repo/" }
maven {
    credentials {
        username "${nexusUsername}"
        password "${nexusPassword}"
    }
    url "https://nexus.loyalty.com/nexus/content/groups/public/"
}
}

dependencies {
compile project (':Astrix')
compile group: 'org.scalaj', name: 'scalaj-http_2.11', version: '2.3.0'
compile group: 'com.loyalty.rewards', name: 'DataModelling', version: '0.1.5'


 //Shortcut which works but bad way of doing it.
//compile fileTree(dir:'../Astrix/build/libs',include: ['Astrix-0.0.7.jar'])
//compile group: 'org.rogach', name: 'scallop_2.11', version: '2.1.1'


testCompile "org.specs2:specs2-core_$versions.scala_major:3.6.6"
testCompile "org.specs2:specs2-junit_$versions.scala_major:3.6.6"
}

Alpha所依赖的Astrix子项目的build.gradle如下

group 'com.loyalty.sessions'
version '0.0.7'

repositories {
maven { url "https://repository.cloudera.com/artifactory/repo/" }
maven {
    credentials {
        username "${nexusUsername}"
        password "${nexusPassword}"
    }
    url "https://nexus.loyalty.com/nexus/content/groups/public/"
}
}

dependencies {
compile group: 'org.rogach', name: 'scallop_2.11', version: '2.1.1'

//compile group: 'org.scalaj', name: 'scalaj-http_2.10', version: 
"2.3.0"
}

但是当我进入Alpha子项目目录并进行gradle clean构建时,它会出现以下错误并指示缺少Astrix项目的依赖项。

* What went wrong:
Execution failed for task ':Alpha:compileScala'.
> Compilation failed

我不确定自己在做什么错。我已经将子项目包含在父settings.gradle中,但它似乎还是失败了。

我的主项目build.gradle看起来像这样

buildscript {
repositories {
    maven {
        credentials {
            username "${nexusUsername}"
            password "${nexusPassword}"
        }
        url "https://nexus.artifactory.com/nexus/content/groups/public/"
    }
}
dependencies {
    classpath "com.loyalty.blender:blender-nexus-plugin:0.2.1"
    classpath "com.github.jengelman.gradle.plugins:shadow:1.2.3"

    classpath 'com.github.skhatri:gradle-s3-plugin:1.0.4'
    classpath 'joda-time:joda-time:2.4'
}
}

allprojects {
apply plugin: 'idea'
}

task wrapper(type: Wrapper) {
   gradleVersion = '4.1' //version required
}


subprojects {
apply plugin: 'java'
apply plugin: 'java-library'
apply plugin: 'scala'
apply plugin: 'blender.nexus'
apply plugin: "com.github.johnrengelman.shadow"
apply plugin: 's3'

sourceCompatibility = 1.6

ext {
    versions = [
            scala: '2.11.8',
            scala_major: '2.11',
            spark: '2.1.0',
            avro: '1.7.7'
    ]
}

build.dependsOn clean
compileScala.mustRunAfter clean

configurations.create('shadowConfigurations')


shadowJar {
    zip64 = true
    exclude 'META-INF/**'
    //entryCompression = org.gradle.api.tasks.bundling.ZipEntryCompression.STORED
    configurations = [project.configurations.shadowConfigurations]
}.dependsOn(build)

dependencies {
    compile group: 'org.scala-lang', name: 'scala-library', version: '2.11'

    compile group: 'org.apache.spark', name: "spark-core_$versions.scala_major", version: "$versions.spark"
    compile group: 'org.apache.spark', name: "spark-sql_$versions.scala_major", version: "$versions.spark"
    compile group: 'org.apache.spark', name: "spark-mllib_$versions.scala_major", version: "$versions.spark"

    compile group: 'org.postgresql', name: 'postgresql', version: '9.4.1208'
    compile group: 'joda-time', name: 'joda-time', version: '2.9.4'
    compile group: 'com.amazonaws', name: 'aws-java-sdk', version: '1.11.7'

    compile group: 'com.loyalty.data', name:  'utils', version: "0.6.18"

    testCompile group: 'junit', name: 'junit', version: '0.4.12'

    testCompile "org.specs2:specs2-core_$versions.scala_major:3.6.6"
    testCompile "org.specs2:specs2-junit_$versions.scala_major:3.6.6"
}

configurations {
    /* We don't want the spark dependencies in our shadowJar */
    shadowConfigurations.extendsFrom runtime
    shadowConfigurations.exclude module: "spark-core_$versions.scala_major"
    shadowConfigurations.exclude module: "spark-sql_$versions.scala_major"
    shadowConfigurations.exclude module: "spark-mllib_$versions.scala_major"
    shadowConfigurations.exclude module: "aws-java-sdk"
}

idea {
    module {
        //downloadJavadoc = true
        downloadSources = true
    }
}

}

这是多项目设置,项目结构为

RewardsProject
    README.md
    PreferredSMS
    bin
    cluster_spoolup
    crontab
    deploy
    gradle.properties
    gradlew
    gradlew.bat
    sparkprod
    gradle
    build.gradle
    PreferredByCampaign
    ClubSeries
    DataModelling
    target
    Social
    settings.gradle
    Astrix
    Magnifier
    Picture
    Alpha
    GoogleAdwords
    HelloWorld
    PreferredCampaigns

我的settings.gradle文件如下所示

include 'PreferredByCampaign','PreferredCampaigns','ClubSeries', 'Social', 'HelloWorld', 'Alpha', 'Astrix', 'DataModelling', 'Magnifier','GoogleAdwords','Picture'

但是,如果我在Astrix项目上执行了gradle clean构建,然后将如下所示从该构建生成的jar文件包含在Alpha的build.gradle中,那么它将起作用。但这不是可持续的方式。

//Shortcut which works but bad way of doing it.
//compile fileTree(dir:'../Astrix/build/libs',include: ['Astrix-0.0.7.jar'])
//compile group: 'org.rogach', name: 'scallop_2.11', version: '2.1.1'

如果有人可以帮助我,那将是非常有帮助的。如果这可行,那么其余项目将很容易进行gradle构建。但是目前我被困在这里。

0 个答案:

没有答案