2012-07-13 79 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