2012-07-13 190 views
5

我想爲所有子項目(大約30)定義一個jar任務。我嘗試了以下任務:在gradle中爲jar清單定義自定義類路徑

jar { 
      destinationDir = file('../../../../_temp/core/modules') 
      archiveName = baseName + '.' + extension 
      metaInf { 
        from 'ejbModule/META-INF/' exclude 'MANIFEST.MF' 
      } 

      def manifestClasspath = configurations.runtime.collect { it.getName() }.join(',') 
      manifest { 
      attributes("Manifest-Version"  : "1.0", 
       "Created-By"    : vendor, 
       "Specification-Title" : appName, 
       "Specification-Version" : version, 
       "Specification-Vendor" : vendor, 
       "Implementation-Title" : appName, 
       "Implementation-Version" : version, 
       "Implementation-Vendor" : vendor, 
       "Main-Class"    : "com.dcx.epep.Start", 
       "Class-Path"    : manifestClasspath 
      ) 
      } 
    } 

我的問題是,該子項目之間的依賴關係不包括在清單的類路徑。我嘗試將運行時配置更改爲編譯配置,但會導致以下錯誤。

  • 出了什麼問題:發生了評估項目 ':EskoordClient'。

    您無法更改未處於未解決狀態的配置!

這是項目EskoordClient我的完整構建文件:

dependencies {  
    compile project(':ePEPClient') 
} 

我的大部分子項目的建設文件只定義了項目的依賴。第三方lib依賴關係在超級項目的構建文件中定義。

是否有可能將所有需要的類路徑條目(第三方庫和其他項目)包含到所有子項目的超級項目中的清單類路徑中。

+0

你聲明的每子項目,它的任務? (我沒有看到一個'subprojects {}'塊。)出現「無法更改配置」錯誤,因爲您在做這項工作的時間太早(配置階段而不是執行階段)。項目依賴關係對我來說是正確的。你正在使用哪個Gradle版本? – 2012-07-13 13:05:45

+0

我正在使用gradle版本1.0 目前,我在配置'操作中配置了jar目標:configure(subprojects.findAll {it.name.endsWith('Service')|| it.name.endsWith('Common')) || it.name.endsWith('Client')})' – user1490402 2012-07-16 10:49:58

回答

6

這就是我如何運作的。僅獲得使用調用項目的依賴關係:

getAllDependencies().withType(ProjectDependency) 

然後將每個項目的libsDir的內容,我的Class-Path清單條目。

jar { 
    manifest { 
     attributes 'Main-Class': 'com.my.package.Main' 
     def manifestCp = configurations.runtime.files.collect { 
     File file = it 
     "lib/${file.name}" 
     }.join(' ') 


     configurations.runtime.getAllDependencies().withType(ProjectDependency).each {dep-> 

      def depProj = dep.getDependencyProject() 
      def libFilePaths = project(depProj.path).libsDir.list().collect{ inFile-> "lib/${inFile}" }.join(' ') 
      logger.info"Adding libs from project ${depProj.name}: [- ${libFilePaths} -]" 
      manifestCp += ' '+libFilePaths 
     } 

     logger.lifecycle("") 
     logger.lifecycle("---Manifest-Class-Path: ${manifestCp}") 
     attributes 'Class-Path': manifestCp 

    } 

} 
+0

您可能希望從最終的類路徑中移除重複項並使用'manifestCp = manifestCp.split('').collect()。unique()進行排序。 ().join('')'...我喜歡groovy – coderatchet 2013-12-17 03:29:23