簡體   English   中英

如何為本地JAR依賴指定“sources”JAR?

[英]How to specify “sources” JAR for local JAR dependency?

我的Gradle / Buildship項目中有一個*.jar文件,它位於一個lib文件夾中。 我將它包含在我的build.gradle

compile files('libs/local-lib.jar')

我還有一個相應的local-lib-sources.jar文件,我想附加到它上面。 在Eclipse中,對於手動管理的依賴項,這可以通過構建路徑條目->屬性-> Java源附件的上下文菜單工作。 但是,對於gradle-managed依賴項,該選項不可用。

有沒有人知道gradle / buildship這樣做的方式是什么樣的? 我的依賴是沒有存儲庫,所以我現在一直困在compile files中。

如果你想使用Buildship with Eclipse,那么你運氣不好,因為gradle目前不支持它(參見https://discuss.gradle.org/t/add-sources-manually-for-a-dependency-which-缺乏他們/ 11456/8 )。

如果您沒有使用Buildship並手動生成Eclipse點文件,則可以在build.gradle中執行以下操作:

apply plugin: 'eclipse'

eclipse.classpath.file {
  withXml {
    xml ->
    def node = xml.asNode()
    node.classpathentry.forEach {
      if(it.@kind == 'lib') {
        def sourcePath = it.@path.replace('.jar', '-sources.jar')
        if(file(sourcePath).exists()) {
          it.@sourcepath = sourcePath
        }
      }
    }
  }
}

然后,您將從命令行運行gradle eclipse並使用Import - >“Existing Projects into Workspace”將項目導入Eclipse

另一個(可能更好)選項是使用這樣的平面文件存儲庫:

repositories {
    flatDir { 
        dirs 'lib'
}

請參閱https://docs.gradle.org/current/userguide/dependency_management.html#sec:flat_dir_resolver

然后,您可以像其他任何一樣包含您的依賴項; 在你的情況下:

compile ':local-lib'

這樣Buildship將自動找到-sources.jar文件,因為flatDir在大多數情況下就像常規存儲庫一樣。

在與src相同的目錄級別上使用名為lib或類似的額外文件夾,或者構建腳本。

dependencies {
//local file
     compile files('lib/local-lib-sources.jar')
// others local or remote file
 }

暫無
暫無

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

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