簡體   English   中英

僅在Eclipse中將帶有捆綁的外部jar添加到項目中

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

有沒有一種方法可以添加帶有捆綁包的外部jar,而無需編輯目標平台,而只需為當前項目定義這些jar。 我在全局定義了錯誤的插件時遇到了一些錯誤。

作為參考,我使用了以下答案: MANIFEST.MF中未將外部jar解析為包

我曾經嘗試使用目標平台來做到這一點。 但是,Eclipse PDE非常混亂且陌生。

今天,我使用Maven Bundle插件來管理我的OSGi項目。 除非您真的需要使用eclipse插件/平台,並且別無選擇,否則我建議您嘗試使用felix maven捆綁插件( http://felix.apache.org/site/apache-felix-maven- bundle-plugin-bnd.html )。

我的方法是通過創建一個內部項目捆綁在一起的主項目。 這是從頭開始創建此類項目的方法:

在此處輸入圖片說明

首先,在Eclipse Kepler(或更高版本)中,通過轉到File-> New-> Maven Project創建一個新的maven項目。 在顯示的新窗口中,選擇要將項目保存到的文件夾,它可以是您的工作區或任何其他文件夾。

在此處輸入圖片說明

選擇選項“創建簡單項目(跳過achetype選擇)”,然后按Next。 填寫組ID名稱和工件ID名稱。 這些是用於在Maven存儲庫中標識您的項目的參數,其中每個項目都由該對表示(不要問我為什么他們選擇這種方式來命名項目)。 您可以根據需要命名。 我只是在工件ID后面附加了“ .master”,以突出表明該項目只是父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>

我們需要讓Maven理解我們將使用felix 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一樣困擾着人們,除非在日食環境之外使用,但這是另外一回事了……)。 默認情況下,maven將在maven的中央存儲庫中搜索這些依賴關系,該中央存儲庫是一個龐大的在線存儲庫,其中包含大量Java庫供我們使用。 如果現在在項目瀏覽器窗口中選擇項目,然后右鍵單擊它,然后轉到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] ------------------------------------------------------------------------

請注意BUILD SUCCESS部分,這非常重要,這意味着到目前為止一切正常! 現在,如果嘗試運行方式-> Maven Build…,然后在顯示的窗口的“目標”字段中鍵入-Pcreate-osgi-bundles-from-dependencies bundle:wrap,然后單擊運行,則將進行maven下載這3個依賴項(junit和org.orgi。*),並從它們中創建捆綁包(此操作稱為打包為捆綁包)。 它將這些捆綁包放入項目的bundles /目錄中:

在此處輸入圖片說明

別擔心,在您的項目中,除了我在這里看到的以外,您還會看到Maven依賴項或其他文件夾。 有時蝕把它們放在那兒,有時卻沒有,就像魔術一樣。 如果遇到任何奇怪的問題,只需選擇項目,然后轉到Maven-> Update Maven Project,選擇Force Update of Snapshots / Releases,確保在上面的列表中選擇了您的項目,然后單擊Ok。

在此處輸入圖片說明

現在該創建捆綁包了。 為了簡單起見,我們將只創建一個簡單的包,因此最簡單的方法是在項目內創建一個新文件夾,並為其命名,例如:

在此處輸入圖片說明

現在,在該文件夾中創建具有以下內容的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自動檢測到新項目。

在此處輸入圖片說明

點擊完成,尋找新導入的項目。 您會看到您的POM文件包含錯誤,但是它們是誤報,現在就將其忽略。 Maven試圖告訴您的是,您的項目中沒有任何源文件(即捆綁激活器)。 因此,在這個名為“ 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 install! 每次進行Maven安裝時,它將在您的target /文件夾中生成一個.jar。 該.jar是您項目的捆綁包,您可以將其部署到OSGi框架。 但是現在您也希望將所有相關的依賴項生成為捆綁包,以便它們也可以進入OSGi Framework運行時。 對於我們的示例,我已在該捆綁包POM文件中放置了一個依賴關系,它與我們的實際代碼無關,它不是必需的,但我將其放在此處以顯示如果您對此捆綁包具有依賴關系,maven的行為(其他java.lang中已經包含在所有Java運行時環境中的其他對象)。 選擇主項目,然后轉到運行方式-> Maven Build…,然后在顯示的窗口的“目標”字段中鍵入-Pcreate-osgi-bundles-from-dependencies bundle:wrap,然后單擊運行,您將maven從父/主項目及其捆綁軟件(在我們的情況下,我們只有一個捆綁軟件)下載所有依賴項,並從它們創建捆綁軟件。 現在,您應該將每個程序包放在每個項目(主程序和helloworldbundle)中的bundles /文件夾中。 現在下載felix OSGi框架,並將所有這些jar以及捆綁包的target /目錄中的jar文件復制到felix框架bundle /文件夾中。

在此處輸入圖片說明

注意:每次運行felix時,都可以刪除高速緩存文件夾以完全重置框架,以防萬一您更新了某些捆綁軟件並進行了更新而損壞了運行時。 現在打開cmd,轉到您的felix根路徑並鍵入:java –jar bin \\ felix.jar您應該看到一條巨大的錯誤消息。 這是因為我們在捆綁軟件的pom文件中添加了示例依賴項。 因此,請刪除它,因為這只是一個示例。 因此,您需要刪除以下內容:

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

在Eclipse中運行方式-> Maven安裝,將target /文件夾中新生成的.jar再次復制到felix框架中。 從此處刪除org.zeromq.scala-binding_2.10_0.0.7.jar文件,然后再次運行felix框架。 您現在應該看到世界問候消息了!

在此處輸入圖片說明

希望這可以幫助!

在此鏈接中,您可以找到我為該示例創建的項目以及已部署此項目的felix框架,可以按照本迷你教程結尾處的說明運行。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM