首页 文章

使用gradle显示第一个OSGi构建 - 从ant迁移到Gradle

提问于
浏览
26

是否有OSGi的第一个http://wiki.osgi.org/wiki/Tooling_Approaches gradle插件?或者如何用gradle做到这一点?

OSGi容器有一个很大的旧项目,许多项目在MANIFEST.MF中声明了复杂的关系 . 构建很长 . 现在我们想要简化并采用Gradle . 但首先不要破坏事物并保持 Ant 和gradle并行构建一段时间 . 但是我看到的是gradle建议在 build.gradle 中定义MANIFEST . https://docs.gradle.org/current/userguide/osgi_plugin.html
这将使很多复制工作 .

更新有近100个模块,模块之间有很多依赖关系信息,嵌套jar . 平均MANIFEST.MF长度约为50行(从20到300行不等) . 如何捆绑嵌套jar是other question . 这个问题是关于使用现有的MANIFEST.MF文件 . 我看到的所有插件都使用 bnd ,这与第一种方法完全相反 .

2 回答

  • 9

    Gradle有一个 OsgiManifest 类,它是一个扩展的jar清单:

    https://docs.gradle.org/current/javadoc/org/gradle/api/plugins/osgi/OsgiManifest.html

    StackOverflow上有一篇文章显示了类似的用法:

    How to add Import-Package instructions for runtime dependencies?

    相关的gradle块看起来像这样:

    apply plugin: 'java'
    apply plugin: 'osgi'
    
    jar {
        baseName = 'awesome'
        manifest {
            name = 'An Awesome Application'
            symbolicName = 'com.example.awesome'
            instruction 'Import-Package', 'org.springframework.orm', '*'
        }
    }
    

    如果您有现有清单并希望使用自己的自定义文件,则可以通过在jar闭包中设置清单文件位置来执行此操作:

    jar {
        manifest {
            def manif = "${resourcesDir}/MANIFEST.MF"
            if (new File(manif).exists()) {
                from file(manif)
            }
            else{
                name = 'overwrittenSpecialOsgiName'
                instruction 'Private-Package', 'org.mycomp.somepackage'
                instruction 'Bundle-Vendor', 'MyCompany'
                instruction 'Bundle-Description', 'Platform2: Metrics' 
            }
        }
    }
    

    可以在此处找到Gradle清单的文档:https://docs.gradle.org/current/javadoc/org/gradle/api/java/archives/Manifest.html

    For your 'other question':

    在某些情况下,还有其他用于构建OSGI包的Gradle插件,包括来自其他OSGI包的依赖项 .

    例如,有一个Gradle Bundle Plugin,它使用bnd工具,允许您指定包含传递依赖,甚至排除不需要的依赖 . 举个例子:

    jar {
        manifest {
            attributes 'Implementation-Title': 'Bundle Quickstart',     // Will be added to manifest
                         'Import-Package': '*'  // Will be overwritten by the instuctions below
        }
    }
    
    bundle {
        includeTransitiveDependencies = true
    
        instructions << [
            'Bundle-Activator': 'foo.bar.MyBundleActivator',
            'Import-Package': 'foo.*',
            '-sources': true
        ]
    
        instruction 'Export-Package', '*' // Specify an individual instruction
        instruction '-wab', ''
    }
    

    还有Gradle osgi-run plugin,默认情况下包含传递依赖:

    dependencies {
        // all your usual dependencies
        ...
    
        osgiRuntime( "your:dependency:1.0" ) {
            transitive = false // transitive dependencies not included in OSGi runtime
        }
    }
    

    希望这足以让你前进 .

  • 3

    截至2016年4月,在OSGi的Maven或Gradle构建工具中没有Manifest-first方法 .

    虽然对于Eclipse插件(也是有效的OSGi包),有一个maven / tycho build,这是Eclipse Foundation中的标准,但对于一般的OSGi项目来说它并没有什么帮助 .

    Oposite to Manifest-first是Manifest代,只有一个工具 bnd ,最初用于清单创建,然后发展为完整的bundle jar构建器,现在有BndTools Eclipse integration,看起来类似于Maven / Gradle集成管理依赖项 .

    我建议在外部标准 bnd.bnd 文件中保留 bnd 指令,而不是将其放在构建脚本中 . *.bnd 文件类似于通常的Java .properties 文件,因此在Eclipse IDE中右键单击,打开 - >其他...选择 Properties File Editor 检查"Use this editor for.."并检查"Use this editor for all '*.nbd' files"

    对于Gradle

    对于maven

    所有基于bnd的工具现在collected http://bnd.bndtools.org/chapters/700-tools.html

    https://github.com/paulvi/OSGiBuildExamples中有一些例子

    注意:链接http://wiki.osgi.org/wiki/Tooling_Approaches已处于"The OSGi Community Wiki was unfortunately hacked and is currently unavailable. "状态超过一周,而此问题已打开 .

    可悲的是@Richard过早地放弃了以获得一些感谢(提到maven)

相关问题