Gradle Manifest.MF

Gradle Manifest.MF,gradle,manifest,Gradle,Manifest,我要做的是将项目中预先创建的manifest.mf文件与gradle中jar任务中动态创建的manifest文件结合起来 有没有办法做到这一点?目前,我正在生成清单文件:- jar.doFirst { manifest { def requiredProjects = '' configurations.compile.getAllDependencies().withType(ProjectDependency).each {dep->

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

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

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

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


最后,我使用以下代码实现了我想要的:-

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动态管理的类路径依赖项