簡體   English   中英

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

[英]Gradle: How to exclude a particular package from a jar?

我們有一個與已刪除的某些要求相關的包,但我們不希望刪除該代碼,因為將來可能會再次需要它。 所以在我們現有的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嘗試編譯該包?

如果您不想編譯這些軟件包,此解決方案是有效的,但如果您要編譯它們並從JAR中排除,則可以使用

// tag::jar[]
jar {
    exclude('mi/package/excluded/**')   
    exclude('mi/package/excluded2/**')  
}
// end::jar[]

如果您有一些不想編譯的源,則必須為源聲明一個過濾器,而不是為Jar中的類文件聲明。 就像是:

sourceSets {
    main {
        java {
            include 'com/ourcompany/somepackage/activityadapter/**'
            include 'com/ourcompany/someotherpackage/**'
            exclude 'com/ourcompany/someotherpackage/polling/**'
        }
    }
}

2018年:

您還可以使用閉包或Spec來指定要包含或排除的文件。 閉包或Spec傳遞給FileTreeElement,並且必須返回一個布爾值。

jar {
  exclude {
    FileSystems.getDefault()
      .getPathMatcher("glob:com/ourcompany/someotherpackage/polling/**")
      .matches(it.file.toPath())
  }
}

請參閱Jar.excludeFileTreeElementFinding Files

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM