简体   繁体   English

仅在Eclipse中将带有捆绑的外部jar添加到项目中

[英]adding external jars with bundles to project only in Eclipse

Is there a way to add external jars with bundles without editting the target platform and just defining these jars for the current project. 有没有一种方法可以添加带有捆绑包的外部jar,而无需编辑目标平台,而只需为当前项目定义这些jar。 I run into some errors having wrong plugins defined globally. 我在全局定义了错误的插件时遇到了一些错误。

As reference I used this answer: External jars not resolved as bundles in MANIFEST.MF 作为参考,我使用了以下答案: MANIFEST.MF中未将外部jar解析为包

I once tried to use the target platform to do that. 我曾经尝试使用目标平台来做到这一点。 But eclipse PDE is very messy and strange. 但是,Eclipse PDE非常混乱且陌生。

Today I use Maven Bundle Plugin to manage my OSGi projects. 今天,我使用Maven Bundle插件来管理我的OSGi项目。 Unless you really need to work with eclipse plugins/platform and you have no other choice, I would suggest you to try maven with the felix maven bundle plugin ( http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html ). 除非您真的需要使用eclipse插件/平台,并且别无选择,否则我建议您尝试使用felix maven捆绑插件( http://felix.apache.org/site/apache-felix-maven- bundle-plugin-bnd.html )。

The way I do it is by creating one master project with bundles inside. 我的方法是通过创建一个内部项目捆绑在一起的主项目。 Here's how to create such project from scratch: 这是从头开始创建此类项目的方法:

在此处输入图片说明

First, in Eclipse Kepler (or later), create a new maven project, by going File->New->Maven Project. 首先,在Eclipse Kepler(或更高版本)中,通过转到File-> New-> Maven Project创建一个新的maven项目。 In the new window that shows up, choose the folder to save the project into, it can be your workspace or any other folder. 在显示的新窗口中,选择要将项目保存到的文件夹,它可以是您的工作区或任何其他文件夹。

在此处输入图片说明

Select the option “Create simple project (skip achetype selection)”, and press Next. 选择选项“创建简单项目(跳过achetype选择)”,然后按Next。 Fill the group id name and artifact id name. 填写组ID名称和工件ID名称。 These are parameters used to identify your project in maven repositories, where each project is represented by the pair (don't ask me why did they choose this way to name projects). 这些是用于在Maven存储库中标识您的项目的参数,其中每个项目都由该对表示(不要问我为什么他们选择这种方式来命名项目)。 You can name them anything you want. 您可以根据需要命名。 I just appended “.master” to my artifact id to highlight that this project is just a parent POM. 我只是在工件ID后面附加了“ .master”,以突出表明该项目只是父POM。 The parent POM is a POM.XML file containing all the information that is common to all our bundles we want to develop. 父POM是一个POM.XML文件,其中包含我们要开发的所有捆绑软件所共有的所有信息。

在此处输入图片说明

Press Finish. 按完成。 Now if you look at your POM.XML you'll see very few lines with some useless information and our group and artifact ids. 现在,如果您查看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>

We need to make maven understand that we are going to create bundles, using felix apache maven bundle plugin. 我们需要让Maven理解我们将使用felix apache maven捆绑包插件创建捆绑包。 More specifically your POM.XML must look like this: 更具体地说,您的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>

If you look at the dependencies part you'll see that all our bundles will need junit, org.osgi.core and org.osgi.compendium (I choose felix implementations because equinox is as much as a pain in the ass as Eclipse PDE, unless used outside the eclipse environment, but that's a different story…). 如果您查看依赖项部分,您会发现我们所有的捆绑软件都将需要junit,org.osgi.core和org.osgi.compendium(我选择felix实现,因为Equinox就像Eclipse PDE一样困扰着人们,除非在日食环境之外使用,但这是另外一回事了……)。 By default maven will search these dependencies in maven's central repository, a huge repository online that has tons of java libraries at our disposal. 默认情况下,maven将在maven的中央存储库中搜索这些依赖关系,该中央存储库是一个庞大的在线存储库,其中包含大量Java库供我们使用。 If you now select the project in the project explorer window and right-click on it and go to Run As->Maven Install you'll see the console outputting this: 如果现在在项目浏览器窗口中选择项目,然后右键单击它,然后转到Run As-> Maven Install,您将看到控制台输出以下内容:

                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] ------------------------------------------------------------------------

Notice the BUILD SUCCESS part, that's very important, it means that everything is ok so far! 请注意BUILD SUCCESS部分,这非常重要,这意味着到目前为止一切正常! Now, If you try Run As -> Maven Build… and type -Pcreate-osgi-bundles-from-dependencies bundle:wrap in the field “Goals” in the window that shows up, and hit Run, you'll make maven download those 3 dependencies (junit and org.orgi.*) and create bundles from them (this action is called wrapping as a bundle). 现在,如果尝试运行方式-> Maven Build…,然后在显示的窗口的“目标”字段中键入-Pcreate-osgi-bundles-from-dependencies bundle:wrap,然后单击运行,则将进行maven下载这3个依赖项(junit和org.orgi。*),并从它们中创建捆绑包(此操作称为打包为捆绑包)。 It will put those bundles in the bundles/ directory of your project: 它将这些捆绑包放入项目的bundles /目录中:

在此处输入图片说明

Don't worry if in your project you also see maven dependencies or other folders besides the ones I have here. 别担心,在您的项目中,除了我在这里看到的以外,您还会看到Maven依赖项或其他文件夹。 Sometimes eclipse puts them there, sometimes it doesn't, it's like magic. 有时蚀把它们放在那儿,有时却没有,就像魔术一样。 If you ran into any strange problem just select the project and go to Maven -> Update Maven Project, select Force Update of Snapshots/Releases, ensure your project is selected in the list above and hit Ok. 如果遇到任何奇怪的问题,只需选择项目,然后转到Maven-> Update Maven Project,选择Force Update of Snapshots / Releases,确保在上面的列表中选择了您的项目,然后单击Ok。

在此处输入图片说明

Now its time to create our bundles. 现在该创建捆绑包了。 For the sake of simplicity we'll only create one simple bundle, so the easiest way to do it is to create a new folder inside our project and give it the name of our bundle, for example: 为了简单起见,我们将只创建一个简单的包,因此最简单的方法是在项目内创建一个新文件夹,并为其命名,例如:

在此处输入图片说明

Now create a POM.XML file inside that folder with the following content: 现在,在该文件夹中创建具有以下内容的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>

In the very.example.master's POM.XML look for the tags and replace them with this: 在very.example.master的POM.XML中查找标签,然后将其替换为:

    <modules>
        <module>very.example.mybundles.helloworldbundle</module>
    </modules>

Now in Eclipse you do File->Import->Existing Maven Projects and choose the folder of your master project, you'll see eclipse detecting the new project automatically. 现在在Eclipse中,执行File-> Import-> Existing Maven Projects并选择主项目的文件夹,您会看到eclipse自动检测到新项目。

在此处输入图片说明

Hit Finish and look for the new imported project. 点击完成,寻找新导入的项目。 You'll see that your POM file contains errors, but they are false positives, just ignore them for now. 您会看到您的POM文件包含错误,但是它们是误报,现在就将其忽略。 What maven is trying to tell you is that you don't have any source files (namely a bundle activator) in you project. Maven试图告诉您的是,您的项目中没有任何源文件(即捆绑激活器)。 So create a new folder inside this new project called “src” and put your Activator.java inside. 因此,在这个名为“ src”的新项目中创建一个新文件夹,并将Activator.java放入其中。 Its code is as follows: 其代码如下:

    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!");
        }

    }

Your project should have no errors by now, and it should run maven install perfectly! 您的项目现在应该没有错误,并且应该可以完美地运行maven install! Each time you make maven install it will generate a .jar in your target/ folder. 每次进行Maven安装时,它将在您的target /文件夹中生成一个.jar。 That .jar is your project's bundle that you can deploy to the OSGi Framework. 该.jar是您项目的捆绑包,您可以将其部署到OSGi框架。 But now you want to generate all the related dependencies as bundles too so that they can also go into the OSGi Framework runtime. 但是现在您也希望将所有相关的依赖项生成为捆绑包,以便它们也可以进入OSGi Framework运行时。 For our example I've put a dependency in this bundle POM file, that has nothing to do with our actual code, it isn't needed, but I put it there to show you how maven would behave if you had dependencies for this bundle (others that java.lang, that are already included in any java runtime environment). 对于我们的示例,我已在该捆绑包POM文件中放置了一个依赖关系,它与我们的实际代码无关,它不是必需的,但我将其放在此处以显示如果您对此捆绑包具有依赖关系,maven的行为(其他java.lang中已经包含在所有Java运行时环境中的其他对象)。 Select the master project and go to Run As -> Maven Build… and type -Pcreate-osgi-bundles-from-dependencies bundle:wrap in the field “Goals” in the window that shows up, and hit Run, you'll make maven download those all dependencies from the parent/master project and its bundles (in our situation we just have one bundle) and create bundles from them. 选择主项目,然后转到运行方式-> Maven Build…,然后在显示的窗口的“目标”字段中键入-Pcreate-osgi-bundles-from-dependencies bundle:wrap,然后单击运行,您将maven从父/主项目及其捆绑软件(在我们的情况下,我们只有一个捆绑软件)下载所有依赖项,并从它们创建捆绑软件。 You now should have your bundles in the bundles/ folder inside each project (master and the helloworldbundle). 现在,您应该将每个程序包放在每个项目(主程序和helloworldbundle)中的bundles /文件夹中。 Now download the felix OSGi Framework and copy all those jars, plus the jar file in your bundle's target/ directory to the felix framework bundle/ folder. 现在下载felix OSGi框架,并将所有这些jar以及捆绑包的target /目录中的jar文件复制到felix框架bundle /文件夹中。

在此处输入图片说明

Note: Each time you run felix you can delete the cache folder to fully reset the framework, in case you update some bundles and updates corrupt the runtime. 注意:每次运行felix时,都可以删除高速缓存文件夹以完全重置框架,以防万一您更新了某些捆绑软件并进行了更新而损坏了运行时。 Now open cmd, go to your felix root path and type: java –jar bin\\felix.jar You should see a huge error message. 现在打开cmd,转到您的felix根路径并键入:java –jar bin \\ felix.jar您应该看到一条巨大的错误消息。 This is because that example dependency we added in the bundle's pom file. 这是因为我们在捆绑软件的pom文件中添加了示例依赖项。 So lets delete it since it was just an example. 因此,请删除它,因为这只是一个示例。 So you need to delete this: 因此,您需要删除以下内容:

    <dependencies>
            <dependency>
                <groupId>org.zeromq</groupId>
                <artifactId>zeromq-scala-binding_2.10</artifactId>
                <version>0.0.7</version>
            </dependency>
        </dependencies>

Do Run As -> Maven Install in eclipse, copy the new generated .jar in the target/ folder into felix framework again. 在Eclipse中运行方式-> Maven安装,将target /文件夹中新生成的.jar再次复制到felix框架中。 Delete the org.zeromq.scala-binding_2.10_0.0.7.jar file from there and run felix framework again. 从此处删除org.zeromq.scala-binding_2.10_0.0.7.jar文件,然后再次运行felix框架。 You should now see the hello world message! 您现在应该看到世界问候消息了!

在此处输入图片说明

Hope this helps! 希望这可以帮助!

In this link you can find the project I've made for this example and the felix framework with this project deployed, ready to run as you see at the end of this mini tutorial. 在此链接中,您可以找到我为该示例创建的项目以及已部署此项目的felix框架,可以按照本迷你教程结尾处的说明运行。

https://www.dropbox.com/s/pazhtrjmu50zsv9/example-source.zip https://www.dropbox.com/s/pazhtrjmu50zsv9/example-source.zip

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM