Gradle:如何从jar中排除特定包?

Sto*_*wke 16 java gradle

我们有一个与已删除的某些要求相关的包,但我们不希望删除该代码,因为将来可能会再次需要它.所以在我们现有的ant构建中,我们刚刚将这个包排除在jar中编译之外.这些类不能编译,因为我们还删除了它们的依赖项,因此它们不能包含在构建中.

我试图在Gradle中模仿该功能,如下所示:

jar {
    sourceSets.main.java.srcDirs = ['src', '../otherprojectdir/src']

    include (['com/ourcompany/somepackage/activityadapter/**',
                 ...
        'com/ourcompany/someotherpackage/**'])

    exclude(['com/ourcompany/someotherpackage/polling/**'])
}
Run Code Online (Sandbox Code Playgroud)

即使使用上面的exclude调用(我也尝试过没有方括号),gradle仍在尝试编译polling类,这会导致编译失败.如何防止Gradle尝试编译该包?

小智 33

如果您不想编译这些软件包,此解决方案是有效的,但如果您要编译它们并从JAR中排除,则可以使用

// tag::jar[]
jar {
    exclude('mi/package/excluded/**')   
    exclude('mi/package/excluded2/**')  
}
// end::jar[]
Run Code Online (Sandbox Code Playgroud)

  • 无法在我的 build.gradle 文件中写入 jar,您能建议我在哪里编码吗?提前致谢。 (2认同)

Pet*_*ser 29

如果您有一些不想编译的源,则必须为源声明一个过滤器,而不是为Jar中的类文件声明.就像是:

sourceSets {
    main {
        java {
            include 'com/ourcompany/somepackage/activityadapter/**'
            include 'com/ourcompany/someotherpackage/**'
            exclude 'com/ourcompany/someotherpackage/polling/**'
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @Peter感谢您的回答。问题是该类也存在于其他较新版本的依赖项中。我想使用较新的版本,并从较旧的版本中排除该类。 (2认同)