Gradle从SourceSet排除文件不起作用 [英] Gradle exclude file from sourceSet not working

查看:949
本文介绍了Gradle从SourceSet排除文件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,试图将Java源文件树中的单个文件包含到库项目中.无论我做什么,我都不能仅包含该文件.我在另一个项目中取得了成功,除了那个项目以外的所有文件都没有,但是这仅在从库项目运行gradle时有效.从应用程序目录运行该文件时,将包括所有文件.在尝试创建一个简单的项目来重现该问题时,将所有项目都排除在外也是行不通的.

I've got a project where I'm trying to include a single file from a tree of java source files into a library project. Whatever I do I am not able to include only that file. I have had success in another project excluding all files except the one, but that only worked when running gradle from the library project only. When it was run from the application directory, all files were included. While trying to create a simple project that reproduces the issue, that same exclude all does not work either.

样本文件树很简单:

application - android app that includes the two libraries
library1/
  build.gradle
  src/main/AndroidManifest.xml
library2/
  build.gradle
  src/main/AndroidManifest.xml
java/src/com/company/product/
  /pkg1
    Source1.java, Source2.java, Source3.java
    libraryoneonly/Library1Source.java
  /pkg2
    Source1.java, Source2.java, Source3.java

library1 build.gradle看起来像这样.以下包含或排除的工作均不包含.

The library1 build.gradle looks like this. Neither of the includes or excludes work below.

library1/build.gradle

apply plugin: 'com.android.library'
android {
    sourceSets {
        main.jni.srcDirs = []
        main.jniLibs.srcDir 'src/main/libs'
        main.java {
            srcDir "../../java/src"
            exclude  {  FileTreeElement elem ->
                println "Searching for files, found: " + elem.relativePath
                !(elem.isDirectory() || elem.name.equals('Library1Source.java'))
            }
            //include ("**/libraryoneonly/**")
            //include ('com/company/product/pkg1/libraryoneonly/Library1Source.java')
        }
    }
dependencies {
    compile project(':library2')
}

和library2 build.gradle.令人惊讶的是,下面未注释的排除确实起作用.

And library2 build.gradle. Surprisingly, the uncommented exclude below does work.

library2/build.gradle

apply plugin: 'com.android.library'

android {
        sourceSets {
            main.jni.srcDirs = []
            main.jniLibs.srcDir 'src/main/libs'
            main.java {
                srcDir "../../java/src"
                //exclude  {  FileTreeElement elem -> ((elem.path.contains('/libraryoneonly/')) ) }
                exclude ('com/company/product/pkg1/libraryoneonly/Library1Source.java')
            }
        }

仅需注意,在建立library1时,我从未看到正在搜索文件,已找到"文本.我不知道为什么.我确实在这个测试所基于的较大项目中看到了它,具体取决于构建从何处运行.

Just to note, I never see the "Searching for files, found" text when library1 is building. I'm not sure why. I do see it in the larger project this test was modeled from, depending on where the build was run from.

如果您想提取示例项目并进行尝试,请访问以下链接: https://github.com/gbak/gradleTest

If you'd like to pull the sample project and try it out, here is a link: https://github.com/gbak/gradleTest

推荐答案

我终于找到了包含单个文件而不修改srcDir路径的方法:

I finally figured out the way to include a single file, without modifying the srcDir path:

main.java {
    srcDir "../../java/src"
    setIncludes(new HashSet(['com/company/product/pkg1/libraryoneonly/*.java']))
}

路径'**/*.java'被自动包含为包含,添加任何包含,就像将直接包含添加到源文件一样,不会执行任何操作,因为无论如何它已包含在路径中.

The path '**/*.java' is automatically included as an include, adding any more includes, like a direct include to the source file does nothing, since it is already included path anyway.

setIncludes()调用将替换任何现有的包含,包括'**/*.java',并将提供的路径设置为唯一的包含路径.

The setIncludes() call replaces any existing includes, including the '**/*.java' and sets the provided path[s] as the only include path.

通读文档并在main.java帮助后添加建议的printf语句.

Reading over the docs and adding the suggested printf statements after the main.java helped.

main.java.getIncludes().each { println "Added include: $it" }
main.java.sourceFiles.each { println "File in source set: " + it }

感谢所有提供帮助的人.

Thanks to all who have helped out.

这篇关于Gradle从SourceSet排除文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆