首页 文章

Gradle,OSGI和依赖管理

提问于
浏览
2

我试图通过Intellij Idea Gradle构建一个OSGI Web应用程序 . 我发现Gradle有OSGI插件,如下所述:https://docs.gradle.org/current/userguide/osgi_plugin.html

但我不知道如何添加依赖,例如,org.apache.felix.dependencymanager是OSGI bundle . 所以,我在编译时需要这个jar,而我在生成的jar中不需要它 . 我认为,我需要类似于maven'提供'范围的东西,或类似的东西 .

附:有谁知道,“TBD”在Gradle文档中意味着什么?这是否意味着它必须在未来实施,或者是否已实施某种机制,但尚未在文档中进行描述?

2 回答

  • 1

    请查看我编写的插件,osgi-run,它旨在使用OSGi非常容易,而无需使用Eclipse之类的任何外部工具(尽管osgi-run可以为您生成一个Manifest文件,您可以从IDE中指出它获得IDE OSGi支持 - 这就是我使用IntelliJ做的事情,只是Gradle .

    使用 osgi-run ,您只需将依赖项添加到任何您想要的任何Java项目中......无论是否应该由环境提供在编译时都无关紧要,这是一个部署时间问题 .

    例如,添加到build.gradle文件:

    apply plugin: 'osgi' // or other OSGi plugin if you prefer
    
    repositories {
        mavenCentral() // add repos to get your dependencies from
    }
    
    dependencies {
        compile "org.apache.felix:org.apache.felix.dependencymanager:4.3.0"
    }
    

    Note: 只需要 osgi 插件就可以将你的jar变成一个包 . osgi-run 不这样做 .

    如果您有任何运行时依赖项应该存在于OSGi环境中但不存在于编译类路径中,请执行以下操作:

    dependencies {
        ...
        osgiRuntime 'org.apache.felix:org.apache.felix.configadmin:1.8.8'
    }
    

    现在编写一些代码,一旦你准备好运行一个带有你的东西的OSGi容器,把这些行添加到build.gradle文件中:

    // this should be the first line
    plugins {
        id "com.athaydes.osgi-run" version "1.4.3"
    }
    
    ...
    
    // deployment to OSGi container config
    runOsgi {
        // which bundles do you want to add?
        // transitive deps will be automatically added
        bundles += project
    
        // do not deploy jars matching these regexes (not needed, this is the default)
        excludedBundles = ['org\\.osgi\\..*']
    
        // make the manifest visible to the IDE for OSGi support
        copyManifestTo file( 'auto-generated/MANIFEST.MF' )
    }
    

    跑:

    gradle createOsgiRuntime
    

    然后在 build/osgi 目录中找到准备运行的完整OSGi环境 .

    运行它:

    build/osgi/run.sh # or run.bat in Windows
    

    您甚至可以在构建期间运行它:

    gradle runOsgi
    
  • 0

    所以你可能想制作自己的 provided 配置 .

    configurations {
        // define new scope
        provided
    }
    sourceSets {
        // add the configurations to the compile classpath but not runtime
        main.compileClasspath += configurations.provided
        // be sure to add the provided configs to your tests too if needed
        test.compileClasspath += configurations.provided
    }
    dependencies {
        // declare your provided dependencies
        provided 'org.apache.felix:org.apache.felix.dependencymanager:4.3.0'
    }
    

    另外上面关于直接使用 bndtool 而不是gradle提供 osgi 插件的建议是一个很好的建议 . gradle插件有许多不足之处,实际上只是 bndtool 的包装器 . 此外,gradle团队已声明他们没有足够的带宽或专业知识来修复 osgi 插件[1] .

    [1] https://discuss.gradle.org/t/the-osgi-plugin-has-several-flaws/2546/5

相关问题