如何创建支持 Spring 的 Eclipse 插件?

发布于 2024-10-25 22:14:28 字数 3114 浏览 3 评论 0原文

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

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

现在我有一个非常简化的子目标:为Eclipse插件创建简单的Maven项目(如HelloWorld),它可以通过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

No available Bundle Exports package 'org.springframework.context' 之类的错误 (如果我尝试强制将此包添加到 Import-Package 中)。

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

我对OSGi技术经验不是很丰富,所以我什至不明白这是Maven的问题还是OSGi的问题。

有人有创建支持 Spring 的 Eclipse 插件的经验吗?欢迎任何建议和意见。另外,如果能看到一些支持 Spring 的开源 Eclipse 插件那就太好了。

I have a problem when creating even simple Eclipse plugin with Spring support.

My main goal is to develop multi-module Eclipse plugin project using Apache Camel framework. That's why I'm trying to use Spring as IoC container(Camel has good Spring DSL) and Apache Maven as a build tool.

Now I have very simplified sub-goal: create simple Maven project for Eclipse plugin (like HelloWorld), which can create Spring's ApplicationContext by bundle-context.xml file, get some simple dependency from there, and, for instance, print it to console.

I started with spring-osgi-bundle-archetype archetype. I'm trying to use maven-bundle-plugin but without success. Currently, I have following configuration in 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>

This configuration couse MANIFEST.MF generation with a lot of imported packages, all dependencies from pom.xml are embedded into target/dependency and declared in MANIFEST's Bundle-Classpath.

But plugin still doesn't work: there are errors like

NoClassDefFound: org.springframework.context.ApplicationContext

or

No available bundle exports package 'org.springframework.context'
(If I try to add this package to Import-Package forcingly).

But archive with this dependency (spring-context-3.0.5-RELEASE.jar) exists in target/dependency and in Bundle-Classpath.

I'm not very experienced in OSGi technology, so I even cannot understand whether this is a issue with Maven or with OSGi.

Does anyone has experience on creation Eclipse plugins with Spring support? Any advice and comments are welcome. Also it would be great to see some OpenSource Eclipse plugin with Spring support.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪推晚风 2024-11-01 22:14:29

解决方法是在 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 中的运行/调试插件之前调用 mvn package

Workaround was to add in pom.xml following code:

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

and call mvn package before Run/Debug plugin in Eclipse PDE.

怎言笑 2024-11-01 22:14:28

我建议您为 Eclipse 项目使用清单首先构建,这样您就可以使用 Eclipse 中的所有工具来实现插件功能,请参阅 tycho 以及页面末尾的示例。

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

I would suggest you go with a manifest first build for your eclipse project, so you can use all the tools from eclipse for plugin stuff, see tycho and at the end of the page the examples.

EDIT: The links on the tycho page are broken, get the examples via github insteat, its the demo folder

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文