简体   繁体   English

Gradle构建具有传递依赖项的多模块项目

[英]Gradle build multi modules project with transitive dependencies

I have problem with building my simple app. 我在构建简单的应用程序时遇到问题。 It contains 3 modules, ejb, rest and ear in which rest should be included. 它包含3个模块,ejb,休息和耳朵,应在其中包括休息。 To achieve it I wrote build.gradle as one below. 为此,我将build.gradle编写为以下内容之一。 But I still have problems. 但是我仍然有问题。 Built ear looks like: 内置的耳朵看起来像:

ear-1.0.ear
|--ejbs-1.0.jar
|--rest-1.0.jar
|--lib
   |--ejbs-1.0.jar
   |--other libs..

As you can see, I have here duplicated ejbs-1.0.jar . 如您所见,我在这里复制了ejbs-1.0.jar It's no something I want so I have tried to work this around. 我没有想要的东西,所以我尝试解决此问题。 I have tried 2 approaches I found on web but neither of them worked. 我尝试了两种在网络上找到的方法,但都没有用。 First one (comments with label 1) excluded all rest dependencies from going into lib dir. 第一个(带有标签1的注释)从lib dir中排除了所有rest依赖项。 Second (label 2) did the same but also included rest-1.0.jar into lib, making it even worse. 第二个(标签2)做了同样的事情,但还把rest-1.0.jar包含在lib中,这使它更糟。 Now I have no idea how to write my build.gradle so it puts jars made from subprojects in root dir, and their dependencies in lib dir. 现在,我不知道如何编写build.gradle因此它将由子项目创建的jar放入根目录,并将其依赖项放入lib dir。 I have also tried to write something like comments with label 3, but this makes script fail. 我也尝试用标签3编写类似注释的内容,但这会使脚本失败。 Is there easy way to excluded it like that with similar syntax? 是否有简单的方法可以使用类似的语法将其排除在外?

project(":ejbs") {
    apply plugin: "java"

    dependencies {
        compile group: 'javax.ejb', name: 'javax.ejb-api', version: '3.2'
        compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.2'
        compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.2'
    }
}

project(":rest") {
    apply plugin: "java"

    dependencies {

        compile project(':ejbs')
        compile group: 'javax.ejb', name: 'javax.ejb-api', version: '3.2'
        compile group: 'javax.ws.rs', name: 'jsr311-api', version: '0.11'
        compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.2'
        compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.2'
    }
}

project(":ear") {
    apply plugin: "java"
    apply plugin: "ear"

    dependencies {
        //1: def nonTransitive = {transitive = false}
        deploy project(":ejbs")
        deploy project(":rest")
        earlib project(path:":ejbs", configuration:"compile")
        earlib project(path:":rest", configuration:"compile")//1: , nonTransitive
        //2: add('earlib', project(':rest')) {
        //    transitive = false
        //}
        //3: earlib project(path:":rest", configuration:"compile") {
        //    exlude project(':ejbs')
        //}
    }
}

I finally found a solution. 我终于找到了解决方案。 I made it this way: 我是这样写的:

project(":ear") {
    apply plugin: "java"
    apply plugin: "ear"

    dependencies {
        deploy project(":ejbs")
        deploy project(":rest")
        earlib project(path:":ejbs", configuration:"compile")
        earlib(project(path:":rest", configuration:"compile")) {
            exclude module: 'ejbs'
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM