2013-07-02 196 views
1

我想使用maven-bundle-plugin創建一個包。該項目的pom.xml是:未解決的引用[org.osgi.service.component]

<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/maven-v4_0_0.xsd"> 

    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.adobe.support.examples</groupId> 
    <artifactId>com.adobe.support.examples.osgi.service</artifactId> 
    <version>1.0.8</version> 
    <packaging>bundle</packaging> 
    <name>OSGi Service Example</name> 
    <url>http://blogs.adobe.com/kmossman</url> 
    <description>Adobe Developer Support OSGi Service Example</description> 
    <!-- 
    Credit: This example is based on Moritz Havelock's http://in-the-sling.blogspot.com/ 

    I suggest you use the most current versions of the plugin's. 
    Search the Central Repository for the most current versions http://search.maven.org/ 

    Use the search strings below and update the appropriate versions 

    // Felix 
    g:"org.apache.felix" a:"org.apache.felix.framework" 
    g:"org.apache.felix" a:"maven-bundle-plugin" 
    g:"org.apache.felix" a:"maven-scr-plugin" 
    g:"org.apache.felix" a:"org.osgi.core" 
    // Maven 
    g:"org.apache.maven.plugins" a:"maven-compiler-plugin" 
    // Sling 
    g:"org.apache.sling" a:"maven-sling-plugin" 
    --> 
    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.build.java.version>1.6</project.build.java.version> 
     <!-- Felix --> 
     <org.apache.felix.framework.version>4.0.2</org.apache.felix.framework.version> 
     <org.apache.felix.maven-bundle-plugin.version>2.3.7</org.apache.felix.maven-bundle-plugin.version>  
     <org.apache.felix.maven-src-plugin.version>1.7.4</org.apache.felix.maven-src-plugin.version> 
     <org.apache.felix.org.osgi.core>1.4.0</org.apache.felix.org.osgi.core> 
     <!-- Maven --> 
     <org.apache.maven.plugins.maven-compiler-plugin.version>2.3.2</org.apache.maven.plugins.maven-compiler-plugin.version> 
     <org.apache.maven.plugins.maven-surefire-plugin.version>2.12</org.apache.maven.plugins.maven-surefire-plugin.version> 
     <!-- Sling --> 
     <org.apache.sling.maven-sling-plugin.version>2.1.0</org.apache.sling.maven-sling-plugin.version> 
    </properties> 
    <build> 

     <plugins> 
      <!-- Maven will compile our source java classes using 
      the "project.build.java.version" specified --> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>${org.apache.maven.plugins.maven-compiler-plugin.version}</version> 
       <configuration> 
        <source>${project.build.java.version}</source> 
        <target>${project.build.java.version}</target> 
       </configuration> 
      </plugin> 
      <!-- this will install the OSGi bundle into Sling for us 
      we now upload the jar file automatically when we build with this plug-in --> 
      <plugin> 
       <groupId>org.apache.sling</groupId> 
       <artifactId>maven-sling-plugin</artifactId> 
       <version>${org.apache.sling.maven-sling-plugin.version}</version> 
       <executions> 
        <execution> 
         <id>install-bundle</id> 
         <goals> 
          <goal>install</goal> 
         </goals> 
         <configuration> 
          <slingUrl>http://localhost:4502/system/console/install</slingUrl> 
          <user>admin</user> 
          <password>admin</password> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 
      <plugin> 
       <!-- This will create the OSGI-INF for us that handles the Activator Class for us 
       we now auto-generate the details in our bundle with this plug-in--> 
       <groupId>org.apache.felix</groupId> 
       <artifactId>maven-scr-plugin</artifactId> 
       <extensions>true</extensions> 
       <version>${org.apache.felix.maven-src-plugin.version}</version> 
       <executions> 
        <execution> 
         <id>generate-scr-scrdescriptor</id> 
         <goals> 
          <goal>scr</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin>   
      <plugin> 
       <!-- This will create the OSGi /META-INF/MANIFEST.MF for us 
       we now auto-generated the filea for us with this plug-in --> 
       <groupId>org.apache.felix</groupId> 
       <artifactId>maven-bundle-plugin</artifactId> 
       <extensions>true</extensions> 
       <version>${org.apache.felix.maven-bundle-plugin.version}</version> 
       <configuration> 
        <instructions> 
         <Export-Package>com.adobe.support.examples.osgi.service</Export-Package> 
         <Import-Package>org.osgi.framework;version="1.3.0"</Import-Package> 
         <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName> 
         <Bundle-Name>${project.name}</Bundle-Name> 
         <Bundle-Vendor>Kurt Mossman</Bundle-Vendor> 
         <!-- 
          Inserting content into the JCR and installing some files on the server with your bundle. 

          Sling-Initial-Content 

           The first line will overwrite the contents of the node at content/osgitest with test.json 
           NOTE: uninstall:=false says that it will not remove the content when I remove the package. 
            This could be set to true to also remove the content when the package is removed the choice is yours. 

           The second line will overwrite the path will install the files and overwrite them if you re-install. 
         --> 
         <Sling-Initial-Content> 
          SLING-INF/initial-content/content/osgitest;path:=/content/osgitest;overwrite:=true;uninstall:=false, 
          SLING-INF/initial-content/apps/samples/osgitest;path:=/apps/samples/osgitest;overwrite:=true;uninstall:=true 
         </Sling-Initial-Content> 
        </instructions> 
       </configuration> 
      </plugin> 
      <!-- use the surefire plugin to run the test cases 
      we use a thread count of 4 to increase the performance of the test time. --> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <version>${org.apache.maven.plugins.maven-surefire-plugin.version}</version> 
       <configuration> 
        <parallel>methods</parallel> 
        <threadCount>4</threadCount> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
    <dependencies> 
     <dependency> 
      <groupId>org.apache.felix</groupId> 
      <artifactId>org.osgi.core</artifactId> 
      <version>${org.apache.felix.org.osgi.core}</version> 
     </dependency> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.8.2</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.felix</groupId> 
      <artifactId>org.apache.felix.scr.annotations</artifactId> 
      <version>1.9.0</version> 
     </dependency> 
     <dependency> 
    <groupId>org.apache.felix</groupId> 
    <artifactId>org.osgi.compendium</artifactId> 
    <version>1.4.0</version> 
</dependency> 
    </dependencies> 
</project> 

當我運行mvn install命令時出現以下錯誤。

"Unresolved references to [org.osgi.service.component] by class(es) on the Bundle-Classpath[Jar:dot]: [com/adobe/support/examples/com/adobe/support/examples/osgi/service/SampleServiceImpl.class]" 

你能幫我解決嗎?

問候, 安德森

回答

5

的問題是,你有沒有什麼你的應用做了心理模型以及應該如何OSGi的處理......所以讓我嘗試解釋。

該消息表明您的SampleServiceImpl類使用包org.osgi.service.component中的類。當bnd,使得OSGi清單的maven插件分析你的代碼時,它創建了對該包的引用,這是爲導入做準備。然後bnd尋找指令Import-Package。這是默認的*(通配符),意思是它應該導入所有的引用。但是,在您的POM中,您可以用org.osgi.framework;version="1.3.0覆蓋此指令。由於此包與org.osgi.service.component包不匹配,因此bnd將忽略此引用。即Import-Package指令旨在裝飾進口(版本,可選,屬性,指令等),但通常不應該被觸及,因爲bnd真的很擅長根據類路徑進行裝飾。

但是,在bnd構建了jar之後,它運行一個獨立的驗證器來檢查它做了什麼。然後,驗證者會看到對未導入類的引用,並將其報告爲錯誤,這是正確的。一個包含引用另一個類的類的包不是導入的也不是包含的在運行時綁定會導致類未找到異常。

因此,在bnd中有很多事情,解決方案很簡單:只需刪除Import-Package標題並讓bnd執行其工作,其缺省值通常是最佳解決方案,尤其是如果您還沒有線索的話。只有在清單中導入不需要的內容時,才應使用此標題。

現在,你的生活會SOOOOO簡單得多,如果你會使用BND(工具)......

+0

它開始在我的pom.xml除去進口包裝元素 謝謝:)後,開始工作 – user2532663