1

我有以下 ext.libraries 定义,我想将其公开给我使用 gradle 构建的所有项目。我该怎么做。我在哪里可以定义它,以便它对我的项目全局可用?

我的项目结构如下:

 --projects
        ----BaseFrameWork
        ----ProjectA
        ----ProjectB

settings.gradle(项目目录)

includeFlat 'BaseFramework', 'ProjectA', 'ProjectB'

build.gradle(项目目录)

ext.libraries = [
    lib_core:[
        ":junit:4.0:",
        ":log4j:1.2.8:",
        ":ojdbc6:",
        ":struts:1.1-tiger:"
    ],
    lib_jaxb: [
        ":jaxb-api:2.1.3:",
        ":jaxb-impl:2.1.3:"
    ]
]    

allprojects {
    apply plugin: 'java'        
}

build.gradle (BaseFramework 目录)

apply plugin: 'java'
sourceSets.main.java.srcDir  'src/java' 
println "The root project is: " + rootProject
compileJava {
    options.compilerArgs << '-nowarn'
}

jar {
    manifest {
        attributes("Implementation-Title": "BaseFramework", "Implementation-Version": "1")
    }
    from ('./META-INF/') {
        include 'DomainConfig.xml'
        into('META-INF')
    }
}

repositories {
    flatDir dirs: "D:/myrepo"
}

dependencies {
    compile libraries.lib_commons
    compile libraries.lib_core
    compile libraries.lib_jaxb
}

然后我尝试以下并得到一个错误。它无法在根 build.gradle 文件中看到我的库属性。看起来它也将我的 BaseFramework 项目视为根?

D:\projects>cd BaseFramework    
D:\projects\BaseFramework>gradle build
The root project is: root project 'BaseFramework'

FAILURE: Build failed with an exception.

* Where:
Build file 'D:\prod\BaseFramework\build.gradle' line: 25

* What went wrong:
A problem occurred evaluating root project 'BaseFramework'.
> Could not find property 'libraries' on root project 'BaseFramework'.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 2.624 secs

D:\projects\BaseFramework>

请帮忙 :(

4

1 回答 1

2

把它放在你的顶层build.gradle,在任何部分之外。然后,您可以从任何子项目访问它。

您还需要替换includeFlatincludein settings.gradle,否则它将期望找到与目录相同级别的那些projects目录。

includeFlat告诉 Gradle 这些同级目录应该被视为子项目。在您的情况下BaseFrameworkProjectAProjectB不是projects目录的兄弟姐妹,它们是子目录。

另一方面,include告诉 Gradle 这些子目录应该被视为子项目这就是你想要的,所以子项目可以继承在projects.

于 2013-05-30T21:09:33.143 回答