2016-01-18 36 views
0

我正在使用ServiceMix 6.1.0(包含Karaf 3.0.5),並且我有一個Maven項目。我想用karaf-maven-plugin創建卡拉夫檔案(.kar)。檢索每個與Karaf Maven插件的依賴項並創建Karaf存檔

我明白了使用Karaf Archive的目標:檢索每個依賴關係,以便.kar可以部署在脫機環境中。嗯...我想我已經明白了...創建.KAR和部署之後,我得到一個錯誤:

Error executing command: Can't install feature karafMavenPlugin/0.0.0: 
Could not start bundle mvn:org.testng/testng/6.8.8 in feature(s) karafMavenPlugin-0.0.1-SNAPSHOT: 
Unresolved constraint in bundle org.testng [371]: 
Unable to resolve 371.0: 
missing requirement [371.0] osgi.wiring.package; (osgi.wiring.package=org.junit) 

這裏是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>test</groupId> 
    <artifactId>karafMavenPlugin</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>kar</packaging> 
    <name>TestKarafMavenPlugin</name> 

    <build> 
     <plugins> 
      <!-- Plugin to create .kar --> 
      <plugin> 
       <groupId>org.apache.karaf.tooling</groupId> 
       <artifactId>karaf-maven-plugin</artifactId> 
       <version>3.0.5</version> 
       <extensions>true</extensions> 
       <configuration> 
        <aggregateFeatures>true</aggregateFeatures> 
        <includeTransitiveDependency>true</includeTransitiveDependency> 
       </configuration> 
      </plugin> 
     </plugins> 
     <pluginManagement> 
      <plugins> 
       <!--This plugin's configuration is used to store Eclipse m2e settings 
        only. It has no influence on the Maven build itself. --> 
       <plugin> 
        <groupId>org.eclipse.m2e</groupId> 
        <artifactId>lifecycle-mapping</artifactId> 
        <version>1.0.0</version> 
        <configuration> 
         <lifecycleMappingMetadata> 
          <pluginExecutions> 
           <pluginExecution> 
            <pluginExecutionFilter> 
             <groupId> 
              org.apache.karaf.tooling 
             </groupId> 
             <artifactId> 
              karaf-maven-plugin 
             </artifactId> 
             <versionRange> 
              [3.0.5,) 
             </versionRange> 
             <goals> 
              <goal> 
               features-generate-descriptor 
              </goal> 
             </goals> 
            </pluginExecutionFilter> 
            <action> 
             <ignore></ignore> 
            </action> 
           </pluginExecution> 
          </pluginExecutions> 
         </lifecycleMappingMetadata> 
        </configuration> 
       </plugin> 
      </plugins> 
     </pluginManagement> 
    </build> 
    <dependencies> 
     <dependency> 
      <groupId>org.testng</groupId> 
      <artifactId>testng</artifactId> 
      <version>6.8.8</version> 
     </dependency> 
    </dependencies> 
</project> 

這裏是產生feature.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<features xmlns="http://karaf.apache.org/xmlns/features/v1.2.1" name="karafMavenPlugin"> 
    <feature name="karafMavenPlugin" version="0.0.1-SNAPSHOT" description="TestKarafMavenPlugin"> 
     <bundle>mvn:org.testng/testng/6.8.8</bundle> 
     <bundle>wrap:mvn:org.beanshell/bsh/2.0b4</bundle> 
     <bundle>mvn:com.beust/jcommander/1.27</bundle> 
    </feature> 
</features> 

爲什麼JUnit的依賴沒有回收?

我的猜測是,karaf-maven-pluginpom.xml這是<optional><provided>檢索的依賴。事實上,JUnit是一個TestNG的依賴,這裏是TestNG的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/maven-v4_0_0.xsd"> 

    <modelVersion>4.0.0</modelVersion> 
    <groupId>org.testng</groupId> 
    <artifactId>testng</artifactId> 
    <packaging>jar</packaging> 
    <name>TestNG</name> 
    <version>6.8.8</version> 
    <description>TestNG is a testing framework.</description> 
    <url>http://testng.org</url> 

    <dependencies> 
    <dependency> 
     <groupId>org.apache.ant</groupId> 
     <artifactId>ant</artifactId> 
     <version>1.7.0</version> 
     <optional>true</optional> 
    </dependency> 

    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.10</version> 
     <optional>true</optional> 
    </dependency> 

    <dependency> 
     <groupId>org.beanshell</groupId> 
     <artifactId>bsh</artifactId> 
     <version>2.0b4</version> 
<!-- 
     <scope>provided</scope> 
--> 
    </dependency> 

    <dependency> 
     <groupId>com.google.inject</groupId> 
     <artifactId>guice</artifactId> 
     <version>2.0</version> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>com.beust</groupId> 
     <artifactId>jcommander</artifactId> 
     <version>1.27</version> 
    </dependency> 
     <dependency> 
     <groupId>org.yaml</groupId> 
     <artifactId>snakeyaml</artifactId> 
     <version>1.12</version> 
     <optional>true</optional> 
     </dependency> 
    </dependencies>  
</project> 

我們可以看到,每一個誰是依賴不<optional>而不是<provided>karaf-maven-plugin檢索。

所以...我錯了嗎?這是一種預期的行爲嗎?如果是這樣的話,有沒有辦法指示karaf-maven-plugin下載的依賴關係?

回答

1

隨着人們scope Maven的文檔中描述:

提供

這很像編譯,而是指你所期望的JDK或容器,以提供在運行時的依賴。例如, 爲Java Enterprise Edition構建Web應用程序時,您將 將依賴於Servlet API和相關Java EE API 設置爲提供的範圍,因爲Web容器提供了這些類。 此範圍僅在編譯和測試類路徑 上可用,且不可傳遞。

'provided'不是傳遞性的:當通過api詢問maven依賴關係時,不會檢索提供的依賴關係。

所以,爲了與karaf-maven-plugin一起工作,你必須在pom中明確地添加這個依賴。