【问题标题】:Gradle Manifest.MFGradle 清单.MF
【发布时间】:2014-10-29 17:05:17
【问题描述】:

我要做的是将我的项目中预先创建的 manifest.mf 文件与 gradle 中的 jar 任务中动态创建的清单文件结合起来。

有没有办法做到这一点?目前我正在完全生成我的清单文件:-

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
    }
}

我知道我会有一个 /META-INF/MANIFEST.MF 文件。

最初我使用的是用于 Gradle 的 OSGI 插件,但这似乎忽略了清单文件并产生了巨大的混乱。

编辑

我对此进行了很多研究,感谢 carlo,我发现以下代码可以让我读取现有的 MANIFEST.MF:-

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

我还发现了一个名为“wuff”的项目,旨在帮助使用 Gradle 构建 OSGi 项目。

https://github.com/akhikhl/wuff

【问题讨论】:

    标签: gradle manifest


    【解决方案1】:

    最后我设法使用以下代码得到了我想要的东西:-

    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
        }
    }
    

    重要的几行是:-

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

    这允许我继承任何现有的 /META-INF/MANIFEST.MF 文件,还包括由 gradle 动态管理的类路径依赖项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 2021-12-16
      • 2015-12-10
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多