使用gradle构建Java时,跳过源代码树的部分内容

时间:2017-07-18 16:15:32

标签: java gradle

假设我有一个我想用gradle构建的Java项目,但是我想让gradle跳过源树的某些包/部分。

如果源树具有非标准布局,我可以通过以下方式指定:

sourceSets {
    main {
        java {
            srcDirs = ['src']
        }
        resources {
            srcDirs = ['src']
        }
    }
}

有没有办法告诉gradle忽略src/org/example/foo/barproject/ui中的所有内容但是构建其余内容?如果是这样,怎么样?

1 个答案:

答案 0 :(得分:1)

这是我的解决方案:

apply plugin: 'java'

sourceSets {
    main {
        java {
            srcDir = ['src']
            exclude "main/*"
            exclude "hello/*"
            exclude "test/*"
        }
    }
}

我的文件夹布局:

$ ls -R src
Library.java hello        main         test

src/hello:
Application.java     HelloController.java

src/main:
java

src/main/java:
Library.java hello

src/main/java/hello:
Application.java     HelloController.java

src/test:
java

src/test/java:
LibraryTest.java

输出:

$ ./gradlew clean build -x test
Creating properties on demand (a.k.a. dynamic properties) has been deprecated and is scheduled to be removed in Gradle 2.0. Please read http://gradle.org/docs/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html for information on the replacement for dynamic properties.
Deprecated dynamic property: "srcDir" on "source set 'main'", value: "[src]".
:clean
:compileJava
:processResources UP-TO-DATE
:classes
:jar
:assemble
:check
:build

BUILD SUCCESSFUL

Total time: 2.935 secs

如果有效,请告诉我。