简体   繁体   English

从已编译的源集中过滤类文件,以将其添加到Gradle / Groovy的JAR中

[英]Filter class files from compiled sourcesets to be added in a JAR in Gradle/Groovy

I'm trying to include the compiled .class files from Project1 into the jar for Project2 since my project structure requires it to be done. 我正在尝试将Project1的已编译.class文件包含到Project2的jar中,因为我的项目结构要求完成该工作。 For that, in the build.gradle for Project2 , I write : 为此,在Project2build.gradle中,我写道:

jar {
    from project(':Project1').sourceSets.main.output.classesDir
}

Which successfully does what I had to do. 哪个成功完成了我必须做的事情。 But, I now want to filter some of the classes that are added based on path and/or some pattern. 但是,我现在想过滤一些基于路径和/或某种模式添加的类。 For example, to include only delegate files, I tried this : 例如,仅包括委托文件,我尝试了以下方法:

jar {
    from project(':Project1').sourceSets.main.output.classesDir {
        include '**/*Delegate*.class'
    }
}

But unfortunately it doesn't work. 但不幸的是,它不起作用。 Is there a way to achieve this in Gradle/Groovy? 有没有办法在Gradle / Groovy中实现这一目标?

Using Gradle 2.12, the following works for me (this is build.gradle for Project 2): 使用Gradle 2.12,以下对我build.gradle (这是Project 2的build.gradle ):

task myBuild(type: Jar) {
    baseName "myjar"
    from project(':Project1').sourceSets.main.output.classesDir, 
         { include "**/*Delegate*.*" } 
}

From the doc for Jar.from , note that it takes 2 arguments (hence, the comma is used). Jar.from的文档中 ,请注意,它需要2个参数(因此,使用逗号)。

Thanks Michael 谢谢迈克尔

Although I got my answer as well, I was just missing a parantheses. 尽管我也得到了答案,但我只是缺少了一个寄生虫。 The correct and working code goes something like this : 正确且有效的代码如下所示:

jar {
    from (project(':Project1').sourceSets.main.output.classesDir) {
        include '**/*Delegate*.class'
    }
}

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

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