【问题标题】:How to create Eclipse plugin with Spring support?如何创建具有 Spring 支持的 Eclipse 插件?
【发布时间】:2011-07-23 00:18:32
【问题描述】:

在创建带有 Spring 支持的简单 Eclipse 插件时,我遇到了问题。

我的主要目标是使用 Apache Camel 框架开发多模块 Eclipse 插件项目。这就是为什么我尝试使用 Spring 作为 IoC 容器(Camel 有很好的 Spring DSL)和 Apache Maven 作为构建工具。

现在我有了非常简化的子目标:为 Eclipse 插件(如 HelloWorld)创建简单的 Maven 项目,它可以通过 bundle-context.xml 文件创建 Spring 的 ApplicationContext,从那里获取一些简单的依赖关系,例如,打印它来控制台。

我从 spring-osgi-bundle-archetype 原型开始。我正在尝试使用 maven-bundle-plugin 但没有成功。目前,我在 pom.xml 中有以下配置:

        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <version>2.3.4</version>
            <extensions>true</extensions>
            <configuration>
                <manifestLocation>META-INF</manifestLocation>
                <ignoreMissingArtifacts>true</ignoreMissingArtifacts>
                <instructions>
                    <Bundle-SymbolicName>${bundle.symbolicName}; singleton:=true</Bundle-SymbolicName>
                    <Bundle-Version>${pom.version}</Bundle-Version>
                    <!-- | assume public classes are in the top package, and private classes 
                        are under ".internal" -->

                    <Export-Package>!${bundle.namespace}.internal.*,${bundle.namespace}.*;version="${pom.version}"</Export-Package>
                    <Private-Package>${bundle.namespace}.internal.*</Private-Package>

                    <Import-Package>.,*;resolution:=optional</Import-Package>
                    <Bundle-Activator>${bundle.namespace}.Activator</Bundle-Activator>
                    <Bundle-ActivationPolicy>lazy</Bundle-ActivationPolicy>
                    <Require-Bundle>org.eclipse.ui,org.eclipse.core.runtime</Require-Bundle>
                    <Bundle-RequiredExecutionEnvironment>JavaSE-1.6</Bundle-RequiredExecutionEnvironment>
                    <Embed-Dependency>*;scope=compile|runtime;inline=false</Embed-Dependency>
                    <Embed-Directory>target/dependency</Embed-Directory>
                    <Embed-StripGroup>true</Embed-StripGroup>
                    <Embed-Transitive>true</Embed-Transitive>
                </instructions>
            </configuration>
        </plugin>

这个配置导致 MANIFEST.MF 生成大量导入的包,所有来自 pom.xml 的依赖都嵌入到 target/dependency 并在 MANIFEST 的 Bundle-Classpath 中声明。

但插件仍然无法正常工作:出现类似

的错误

NoClassDefFound: org.springframework.context.ApplicationContext

没有可用的捆绑导出包“org.springframework.context” (如果我尝试将此包强制添加到 Import-Package)。

但是在目标/依赖项和 Bundle-Classpath 中存在具有此依赖项 (spring-context-3.0.5-RELEASE.jar) 的存档。

我对 OSGi 技术不是很有经验,所以我什至无法理解这是 Maven 还是 OSGi 的问题。

有没有人在创建带有 Spring 支持的 Eclipse 插件方面有经验?欢迎任何建议和cmets。也很高兴看到一些支持 Spring 的 OpenSource Eclipse 插件。

【问题讨论】:

  • 对不起,这是我的错。我用Eclipse PDE测试过插件,没有使用maven打包的bundle。
  • 此外,嵌入 Spring 模块以满足我的主要目标 - Apache Camel 支持是个坏主意。该框架在具有常规 Spring 库的 OSGi 环境中不起作用。根据文档,应该使用 Spring Dynamic Modules 或 Eclipse Gemini Blueprint。

标签: java eclipse spring maven osgi


【解决方案1】:

我建议你为你的 eclipse 项目使用 manifest 首次构建,这样你就可以使用 eclipse 中的所有工具来获取插件内容,请参阅tycho 和页面末尾的示例。

编辑:tycho 页面上的链接已损坏,请通过github inseat 获取示例,它是演示文件夹

【讨论】:

    【解决方案2】:

    解决方法是在 pom.xml 中添加以下代码:

            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
    

    并在 Eclipse PDE 中的 Run/Debug 插件之前调用 mvn package

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      • 2014-06-06
      • 2015-12-15
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多