我们需要在eclipse上搭建一个OSGI的集成开发环境,实际上就是构建一个eclipse的目标平台(target),这个目标平台里面包括了一些我们要勇于OSGI开发的所必须用到的插件(Bundle),此外,剔除掉了一些我们不需要的插件来达到一个最干净,精简的OSGI开发环境的搭建,这样就有利于我们在eclipse上执行(run)或者调试(debug)一个方便的统一的OSGI开发环境来进行我们的OSGI相关开发工作。要完成这个目的,当然第一步工作是按照我们自己的需要去定制一个插件集,然后在利用eclipse的构建目标平台的功能(target definition)去关联到我们定义的这个插件集上,来完成一个自定义的OSGI开发平台的构建。
这里我们还是利用一个名叫 Maven Dependency Plugin的maven插件来实施我们的插件集合的构建。关于这个插件的配置方法,相信官方网站上已经做了非常详尽的说明,这里由于我们只需要这个插件的 Copying Project Dependencies功能,所以我们这里只简要的讲解一下这个功能的配置方法。
利用如下的一个模板配置你可以用来将配置在你的pom文件中的那些dependencies以及这些dependencies所依赖的dependencies拷贝到指定的目录:
- <project>
- [...]
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-dependency-plugin</artifactId>
- <executions>
- <execution>
- <id>copy-dependencies</id>
- <phase>package</phase>
- <goals>
- <goal>copy-dependencies</goal>
- </goals>
- <configuration>
- <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
- <overWriteReleases>false</overWriteReleases>
- <overWriteSnapshots>false</overWriteSnapshots>
- <overWriteIfNewer>true</overWriteIfNewer>
- </configuration>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- [...]
- </project>
当然你也可以通过设置 excludeTransitive 属性为 true 来显示的将 pom 中配置的 dependencies 所依赖的 dependencies 进行拷贝。
- [...]
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-dependency-plugin</artifactId>
- <executions>
- <execution>
- <id>copy-dependencies</id>
- <phase>package</phase>
- <goals>
- <goal>copy-dependencies</goal>
- </goals>
- <configuration>
- <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
- <overWriteReleases>false</overWriteReleases>
- <overWriteSnapshots>true</overWriteSnapshots>
- <excludeTransitive>true</excludeTransitive>
- </configuration>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- [...]
- project>
实际中,我们会一般的在pom中设置好我们这个OSGI环境所需要的一些bundles(插件),然后设置好outputDirectory(输出目录)假设是${project.build.directory},这个就是指代的这个工程下的target目录,然后用maven的命令:maven package来进行构建。构建完毕后一个符合我们自己需要的OSGI taget platform就设置好了。当然在一些最基础的OSGI核心bundle(比如org.eclipse.osgi)你就没必要再去以这种方式构建到target下了,因为你下下来一个eclipse的IDE后,它里面就包含了一些基本的OSGI核心包(eclipse就是一个OSGI产品),至于在POM中配置dependecies的方式来配置那些需要的bundles,那这些bundle来至哪里呢,这个可以参考我以前的博客中写过得利用pax来进行这项OSGI bundle的构建工作。
<o:p> </o:p>
剩下来的步骤就是要把我们eclipse IDE的工作环境设到我们之前做好的这个OSGI bundles集合上来了。
首先在我们已经构建dependences集合的工程里面创建一个Target definition
先选择New一个Target definition
<o:p> </o:p>
父文件夹选择我们的放了dependences的工程目录,设置好target配置文件的文件名后,点击完成,这样一个target definition就建好了。然后对其做些配置
<o:p>
</o:p>
如果你已经有一个基础的eclipse环境(实际上就是个基础的OSGI环境),那么就在Target Location中将这个Location设置进来,此外再将我们刚才构建的我们自需要的OSGI环境设置在Additional Locations里面,最后保存下这个文件,再点击右上角的Set as Target Platform,那么我们的eclipse就可以立刻切换到我们自定义的OSGI开发环境中去了。
总结:利用 maven 插件以及 eclipse 的 target platform 的结合,来构建一个我们自需要的 OSGI 环境,然后实现 IDE 的 target platform 在这个 target 上的自由切换,有利于统一管理我们的 OSGI 开发平台环境,对开发中要用到的 bundles 进行了有效的积累和管理。