定义我的所有gradle项目都可以看到的属性集

时间:2013-05-30 21:03:11

标签: java gradle

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

我的项目结构如下:

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

settings.gradle(projects dir)

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 dir)

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>

请帮助:(

1 个答案:

答案 0 :(得分:2)

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

您还需要在includeFlat中将include替换为settings.gradle,否则它会期望找到与projects目录处于同一级别的目录。

includeFlat告诉Gradle,那些兄弟目录应该被视为子项目。在您的情况下,BaseFrameworkProjectAProjectB不是projects目录的兄弟,他们是孩子。

另一方面,include告诉Gradle,那些目录应该被视为子项目。这就是你想要的,所以子项目可以继承projects中声明的变量。