我们有一个与已删除的某些要求相关的软件包,但我们不希望删除该代码,因为将来可能会再次需要删除该代码。因此,在我们现有的ant构建中,我们只是将这个包排除在我们的jar中编译之外。这些类不能编译,因为我们还删除了它们的依赖项,因此它们不能包含在构建中。
我试图在Gradle中模仿该功能,如下所示:
jar {
sourceSets.main.java.srcDirs = ['src', '../otherprojectdir/src']
include (['com/ourcompany/somepackage/activityadapter/**',
...
'com/ourcompany/someotherpackage/**'])
exclude(['com/ourcompany/someotherpackage/polling/**'])
}
即使使用上面的排除调用(并且我在没有方括号的情况下也尝试过它),gradle仍在尝试编译polling
类,这会导致编译失败。如何防止Gradle尝试编译该包?
答案 0 :(得分:31)
如果您不想编译这些软件包,此解决方案有效,但如果您想编译它们并从JAR中排除,则可以使用
// tag::jar[]
jar {
exclude('mi/package/excluded/**')
exclude('mi/package/excluded2/**')
}
// end::jar[]
答案 1 :(得分:27)
如果你有一些你不想编译的资源,你必须为源声明一个过滤器,而不是为Jar中的类文件声明。类似的东西:
sourceSets {
main {
java {
include 'com/ourcompany/somepackage/activityadapter/**'
include 'com/ourcompany/someotherpackage/**'
exclude 'com/ourcompany/someotherpackage/polling/**'
}
}
}
答案 2 :(得分:2)
2018年:
您也可以使用闭包或Spec来指定要包含的文件 或排除。闭包或Spec传递给FileTreeElement,并且必须 返回一个布尔值。
jar {
exclude {
FileSystems.getDefault()
.getPathMatcher("glob:com/ourcompany/someotherpackage/polling/**")
.matches(it.file.toPath())
}
}