2014-10-29 15 views
6

Những gì tôi đang tìm cách làm là kết hợp cả tệp manifest.mf được tạo trước từ dự án của tôi với tệp kê khai được tạo động từ nhiệm vụ jar trong gradle.Gradle Manifest.MF

Có cách nào để thực hiện việc này không? Hiện tại tôi đang tạo tệp kê khai hoàn toàn: -

jar.doFirst { 
    manifest { 
     def requiredProjects = '' 
     configurations.compile.getAllDependencies().withType(ProjectDependency).each {dep-> 
      def dependantProjects = dep.getDependencyProject() 
      def projects = project(dependantProjects.path).libsDir.list().findAll{it.endsWith('.jar')} 
      projects.removeAll(projects.findAll{it.endsWith('test.jar')}) 
      def requiredProject = projects.join(' ') 
      requiredProjects += requiredProject.replaceAll(/ /,'%20') + ' ' 
      logger.info 'Required Project: ' + requiredProject 
     } 
     logger.info 'Required requiredProjects: ' + requiredProjects 

     def compileFiles = configurations.compile.files{ it instanceof ExternalDependency }.collect { 
      File file = it 
      "lib/${file.name}" 
     }.join(' ') 

     def manifestPath = requiredProjects + compileFiles 
     logger.info 'Manifest: '+ manifestPath 
     attributes 'Class-Path': manifestPath 
     attributes 'Build-date': new Date(); 
     attributes 'Application-Version': project.version 
    } 
} 

Tôi biết rằng tôi sẽ có tệp /META-INF/MANIFEST.MF.

Ban đầu tôi đã sử dụng plugin OSGI cho Gradle, nhưng điều này dường như bỏ qua tệp kê khai và tạo ra một mớ hỗn độn lớn.

Sửa

Tôi đã nhìn vào trong này khá một chút, nhờ Carlo tôi đã tìm thấy đoạn mã sau sẽ cho phép tôi đọc một MANIFEST.MF hiện có: -

jar { 
     onlyIf { !compileJava.source.empty } 
     manifest { 
      // benutze das im Projekt vorliegende File, falls vorhanden: 
      def manif = "${projectDir}/META-INF/MANIFEST.MF" 
      if (new File(manif).exists()) {     
       from (manif) { 
        eachEntry { details ->       
         if (details.key == 'Bundle-Vendor') { 
                details.value = 'xyz GmbH' 
         } 
        } 
       }          
      } 
      else { 
       logger.info(project.name + " doesn't have a META-INF/MANIFEST.MF.") 
       manifest.attributes provider: xyz GmbH' 
       manifest.attributes project: project.name 
       manifest.attributes Build: new Date() 
      } 
     } 
     // copy if we have these:   
     from file ('plugin.xml')    
     from file ('plugin.properties')  
     into ('icons') { // if any ... 
      from fileTree('icons') 
     } 
    } 

http://forums.gradle.org/gradle/topics/how_to_deal_with_eclipse_projects_and_manifest_files

Tôi cũng đã tìm thấy một dự án có tên 'wuff' nhằm giúp xây dựng các dự án OSGi với Gradle.

https://github.com/akhikhl/wuff

Trả lời

3

Cuối cùng tôi đã quản lý để có được những gì tôi muốn sử dụng đoạn mã sau: -

jar.doFirst { 
    manifest { 
     def manifestFile = "${projectDir}/META-INF/MANIFEST.MF" 
     if (new File(manifestFile).exists()) 
      from (manifestFile) 
     def requiredProjects = '' 
     configurations.compile.getAllDependencies().withType(ProjectDependency).each {dep-> 
      def dependantProjects = dep.getDependencyProject() 
      def projects = project(dependantProjects.path).libsDir.list().findAll{it.endsWith('.jar')} 
      projects.removeAll(projects.findAll{it.endsWith('test.jar')}) 
      def requiredProject = projects.join(' ') 
      requiredProjects += requiredProject.replaceAll(/ /,'%20') + ' ' 
      logger.info 'Required Project: ' + requiredProject 
     } 
     logger.info 'Required requiredProjects: ' + requiredProjects 

     def compileFiles = configurations.compile.files{ it instanceof ExternalDependency }.collect { 
      File file = it 
      "lib/${file.name}" 
     }.join(' ') 

     def manifestPath = requiredProjects + compileFiles 
     logger.info 'Manifest: '+ manifestPath 
     attributes 'Class-Path': manifestPath 
     attributes 'Build-date': new Date(); 
     attributes 'Application-Version': project.version 
    } 
} 

Các dòng quan trọng là: -

def manifestFile = "${projectDir}/META-INF/MANIFEST.MF" 
if (new File(manifestFile).exists()) 
    from (manifestFile) 

này cho phép tôi kế thừa bất kỳ tệp /META-INF/MANIFEST.MF hiện có nào và cũng bao gồm các phụ thuộc classpath được quản lý động bởi gradle.

Các vấn đề liên quan