有没有一种方法来添加外部罐子捆绑而不编辑目标平台,并只为当前项目定义这些罐子。我遇到了一些全球定义错误插件的错误。添加外部罐子捆绑项目只在Eclipse中
正如参考我用这样的回答:External jars not resolved as bundles in MANIFEST.MF
有没有一种方法来添加外部罐子捆绑而不编辑目标平台,并只为当前项目定义这些罐子。我遇到了一些全球定义错误插件的错误。添加外部罐子捆绑项目只在Eclipse中
正如参考我用这样的回答:External jars not resolved as bundles in MANIFEST.MF
我曾经试图用目标平台做到这一点。但是Eclipse PDE非常混乱和奇怪。
今天我用Maven Bundle Plugin来管理我的OSGi项目。除非你真的需要使用eclipse插件/平台,并且你别无选择,否则我会建议你用felix maven bundle插件(http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html)尝试maven。
我这样做的方式是通过创建一个内部包的主项目。 下面是如何从头开始创建这样的项目:
首先,在Eclipse开普勒(或更高版本),创建一个新的Maven项目,转到文件 - >新建 - > Maven项目。 在出现的新窗口中,选择要保存项目的文件夹,它可以是您的工作空间或任何其他文件夹。
选择选项“创建简单的项目(跳过achetype选择)”,按下一步。 填写组ID名称和工件ID名称。这些参数用于在Maven仓库中识别您的项目,其中每个项目都由对代表(不要问我为什么选择这种方式来命名项目)。你可以给他们任何你想要的名字。我只是将“.master”添加到我的工件ID中,以突出显示此项目只是一个父POM。父POM是一个POM.XML文件,其中包含我们想要开发的所有捆绑包所共有的所有信息。
按完成。 现在,如果你看看你的POM.XML,你将会看到很少有一些无用的信息,我们的组和工件ID。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>such.osgi.example</groupId>
<artifactId>very.example.master</artifactId>
<version>0.0.1-SNAPSHOT</version>
</project>
我们需要让行家明白,我们要创建包,使用Apache的菲利克斯捆绑的Maven插件。更具体地讲你的pom.xml必须是这样的:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>such.osgi.example</groupId>
<artifactId>very.example.master</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>${project.artifactId}</name>
<properties>
<project.build.sourceEncoding>ISO-8859-1</project.build.sourceEncoding>
</properties>
<packaging>pom</packaging>
<modules>
</modules>
<profiles>
<!-- http://www.lucamasini.net/Home/osgi-with-felix/creating-osgi-bundles-of-your-maven-dependencies -->
<!-- -Pcreate-osgi-bundles-from-dependencies bundle:wrap -->
<profile>
<id>create-osgi-bundles-from-dependencies</id>
<build>
<directory>${basedir}/bundles</directory>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.0.1</version>
<extensions>true</extensions>
<executions>
<execution>
<id>wrap-my-dependency</id>
<goals>
<goal>wrap</goal>
</goals>
<configuration>
<wrapImportPackage>;</wrapImportPackage>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<build>
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.5</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.osgi.core</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.osgi.compendium</artifactId>
<version>1.4.0</version>
</dependency>
</dependencies>
</project>
如果你看一下依赖性来,你会看到,我们所有的包都需要的JUnit,org.osgi.core和org.osgi.compendium(我选择felix的实现,因为equinox和Eclipse PDE一样痛苦,除非在eclipse环境以外使用,但这是一个不同的故事......)。 默认情况下,maven会在maven的中央仓库中搜索这些依赖项,这是一个庞大的在线仓库,拥有大量的Java库供我们使用。 如果你现在在项目资源管理器窗口中选择该项目,并在其上单击鼠标右键,然后转到运行AS-> Maven的安装,你会看到控制台输出这样的:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building very.example.master 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ very.example.master ---
[debug] execute contextualize
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is p
latform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ very.example.master ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ very.example.mast
er ---
[debug] execute contextualize
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is p
latform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ very.example.master
---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ very.example.master ---
[INFO] Surefire report directory: C:\Users\Pedro\workspace\very.example.master\target\surefire-
reports
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit3/2.10
/surefire-junit3-2.10.pom
Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit3/2.10/
surefire-junit3-2.10.pom (2 KB at 4.5 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit3/2.10
/surefire-junit3-2.10.jar
Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit3/2.10/
surefire-junit3-2.10.jar (26 KB at 143.4 KB/sec)
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ very.example.master ---
[INFO] Building jar: C:\Users\Pedro\workspace\very.example.master\target\very.example.master-0.
0.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ very.example.master ---
[INFO] Installing C:\Users\Pedro\workspace\very.example.master\target\very.example.master-0.0.1
-SNAPSHOT.jar to C:\Users\Pedro\.m2\repository\such\osgi\example\very.example.master\0.0.1-SNAP
SHOT\very.example.master-0.0.1-SNAPSHOT.jar
[INFO] Installing C:\Users\Pedro\workspace\very.example.master\pom.xml to C:\Users\Pedro\.m2\re
pository\such\osgi\example\very.example.master\0.0.1-SNAPSHOT\very.example.master-0.0.1-SNAPSHO
T.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.711s
[INFO] Finished at: Mon May 19 14:47:00 BST 2014
[INFO] Final Memory: 5M/15M
[INFO] ------------------------------------------------------------------------
通知的BUILD成功的一部分,这是非常重要,这意味着一切都很好! 现在,如果您尝试运行方式 - > Maven Build ...并键入-Pcreate-osgi-bundles-from-dependencies包:在显示的窗口中打包字段“目标”,然后点击Run,您将使Maven下载这3个依赖项(junit和org.orgi。*)并从它们中创建包(这个操作被称为包装包)。它将把这些包在你的项目的包/目录:
不要担心,如果在你的项目中,也可以看到Maven的依赖关系或其他文件夹之外的那些我这里。有时候eclipse会把它们放在那里,有时候不会,它就像魔术一样。 如果遇到任何奇怪的问题,只需选择项目并转到Maven - >更新Maven项目,选择强制更新快照/释放,确保您的项目在上面的列表中选择并点击确定。现在
它的时间来建立我们的包。为了简便起见,我们将只创建一个简单的包,所以要做到这一点最简单的方法是创建一个新的文件夹,我们的项目中,并给它我们包的名称,例如:
现在创建该文件夹内的pom.xml文件,内容如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<relativePath>../pom.xml</relativePath>
<groupId>such.osgi.example</groupId>
<artifactId>very.example.master</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>very.example.mybundles.helloworldbundle</artifactId>
<packaging>bundle</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId};singleton:=true</Bundle-SymbolicName>
<Bundle-Version>${project.version}</Bundle-Version>
<Export-Package>very.example.mybundles.helloworldbundle</Export-Package>
<Bundle-Activator>very.example.mybundles.helloworldbundle.Activator</Bundle-Activator>
</instructions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
<name>${project.artifactid}</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.zeromq</groupId>
<artifactId>zeromq-scala-binding_2.10</artifactId>
<version>0.0.7</version>
</dependency>
</dependencies>
</project>
在very.example.master的pom.xml认准标签以及与此替换它们:
<modules>
<module>very.example.mybundles.helloworldbundle</module>
</modules>
现在在Eclipse中执行File-> Import-> Existing Maven Projects并选择主项目的文件夹,您将看到eclipse自动检测到新项目。
点击Finish(完成),并寻找新的进口项目。 你会看到你的POM文件包含错误,但是它们是误报,现在就忽略它们。 Maven试图告诉你的是,你的项目中没有任何源文件(即bundle激活器)。因此,在这个新项目中创建一个名为“src”的新文件夹,并将Activator.java放入其中。 其代码如下:
package very.example.mybundles.helloworldbundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class Activator implements BundleActivator {
@Override
public void start(BundleContext arg0) throws Exception {
System.out.println("hello world!");
}
@Override
public void stop(BundleContext arg0) throws Exception {
System.out.println("bye!");
}
}
您的项目现在应该不会出错,而且它应该运行Maven安装完美! 每次你安装Maven时,它都会在你的目标/文件夹中生成一个.jar文件。 .jar是您可以部署到OSGi Framework的项目捆绑包。但是现在您想要将所有相关的依赖项也作为捆绑包生成,以便它们也可以进入OSGi Framework运行时。对于我们的例子,我已经在这个bundle POM文件中放置了一个依赖项,这与我们的实际代码无关,这是不需要的,但是我把它放在那里来向你展示如果你有这个bundle的依赖项,maven会如何表现(其他的java.lang,已经包含在任何Java运行时环境中)。 选择主项目并转到Run As - > Maven Build ...并键入-Pcreate-osgi-bundles-from-dependencies bundle:在显示的窗口中的“Goals”字段中换行,然后点击Run,让Maven从父/主项目及其捆绑包(在我们的情况下,我们只有一个捆绑包)下载所有依赖关系,并从它们创建捆绑包。 现在,您应该在每个项目(master和helloworldbundle)中的捆绑包/文件夹中包含捆绑包。现在下载felix OSGi框架并将所有这些jar文件,以及你的bundle目标/目录中的jar文件复制到felix框架bundle /文件夹中。
注意:每次运行菲利克斯您可以删除缓存文件夹后再重新设置的框架,如果你更新了一些包和更新损坏的运行时间。 现在打开cmd,转到您的felix根路径并键入:java -jar bin \ felix.jar 您应该会看到一条巨大的错误消息。这是因为我们在bundle的pom文件中添加了这个示例依赖项。所以让我们删除它,因为它只是一个例子。 所以,你需要删除:
<dependencies>
<dependency>
<groupId>org.zeromq</groupId>
<artifactId>zeromq-scala-binding_2.10</artifactId>
<version>0.0.7</version>
</dependency>
</dependencies>
待办事项运行方式 - > Maven的Eclipse中的新目标/文件夹到菲利克斯框架生成的.jar重新安装,复制。从那里删除org.zeromq.scala-binding_2.10_0.0.7.jar文件并再次运行felix框架。 您现在应该看到hello world消息!
希望这有助于!
在这个链接中,您可以找到我为这个示例所做的项目,以及部署了此项目的felix框架,随时准备运行,就像您在本迷你教程结束时看到的一样。
https://www.dropbox.com/s/pazhtrjmu50zsv9/example-source.zip