2013-10-04 63 views
2

我有一個maven項目,除了使用正常的回購也使用本地jar。該罐在清單中定義是這樣的:Maven不會添加本地依賴項到目標jar

<dependency> 
     <groupId>com.mirrorworlds</groupId> 
     <artifactId>lstnef</artifactId> 
     <version>1.0.0</version> 
     <optional>false</optional> 
     <scope>system</scope> 
     <systemPath>${basedir}/lib/lstnef-1.0.0.jar</systemPath> 
    </dependency> 

安裝腳本的工作成功,但應用程序啓動後,我得到這樣的:

Exception in thread "main" java.lang.NoClassDefFoundError: 
com/mirrorworlds/lifestreams/mail/tnef/internet/TnefMultipart 
at ...processMails(MailProcessor.java:57) 
at ...main(MailReader.java:42) 

當我看到目標罐子裏面我可以」 t找到這些類,以及它們,但它們應該在裏面lstnef-1.0.0.jar

我會感謝任何有關解決這個謎團的建議。

+2

使用本地存儲庫管理器和安裝這個罐子把它比你可以使用它作爲一個USAL依賴。讓生活更輕鬆。 – khmarbaise

+0

@khmarbaise,我對maven完全陌生,並且得到了這個項目來介紹一些更新。它曾經以某種方式工作,但現在我無法讓它們都恢復原狀。 – svz

回答

1

我使用的可能的解決方案是在編譯階段之前將此係統JAR安裝到本地Maven存儲庫中,然後將此JAR作爲Maven工件引用。即

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-install-plugin</artifactId> 
    <version>2.2</version> 
    <executions> 
     <execution> 
      <id>your-file</id> 
      <inherited>false</inherited> 
      <phase>validate</phase> 
      <configuration> 
       <file>${pom.basedir}/lib/your-file-4.8.jar</file> 
       <repositoryLayout>default</repositoryLayout> 
       <groupId>your-file</groupId> 
       <artifactId>your-file</artifactId> 
       <version>4.8</version> 
       <packaging>jar</packaging> 
       <generatePom>true</generatePom> 
      </configuration> 
      <goals> 
       <goal>install-file</goal> 
      </goals> 
     </execution> 
    </executions> 
</plugin> 

,然後引用它:

<dependency> 
    <groupId>your-file</groupId> 
    <artifactId>your-file</artifactId> 
    <version>4.8</version>  
</dependency> 
+0

將標誌這一個,因爲它提供了即插即用解決方案。 – svz

0

你需要使用遮陽插件 http://maven.apache.org/plugins/maven-shade-plugin/

<project> 
    ... 
    <build> 
    <plugins> 
     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-shade-plugin</artifactId> 
     <version>2.1</version> 
     <executions> 
      <execution> 
      <phase>package</phase> 
      <goals> 
       <goal>shade</goal> 
      </goals> 
      <configuration> 
       <transformers> 
       <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> 
        <manifestEntries> 
        <Main-Class>org.sonatype.haven.ExodusCli</Main-Class> 
        <Build-Number>123</Build-Number> 
        </manifestEntries> 
       </transformer> 
       </transformers> 
      </configuration> 
      </execution> 
     </executions> 
     </plugin> 
    </plugins> 
    </build> 
    ... 
</project> 
+0

那麼,我實際上並不需要將庫作爲一個罐子放在罐子裏。如果它是解壓縮的,對我來說也很好。 – svz

7

檢查Maven文檔:http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope

system 
This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository. 

您需要手動提供JAR運行時環境自己。

或者,我會reccommend這種做法,建立自己的資源庫,你可以在正常的行家方式

+0

你能否解釋一下這個部分:'你必須提供明確包含它的JAR'?當目標jar被編譯並且編譯成功時,我確實在'$ {basedir}/lib/lstnef-1.0.0.jar'中提供了該jar。我也試圖提供目標jar,但我一直得到這個異常。我在這裏錯過了什麼? – svz

+0

您正在提供的JAR正在構建過程中使用。但是由於它的範圍與「提供」相同,因此它不包含在最終版本中,所以它期望運行時提供它。 – cowls

+0

罐子的範圍是*系統*,而不是*提供* – svz

1

使用系統範圍中添加的jar和管理他們告訴Maven的依賴是行家期間可用「工作小時「在您提供的系統位置(這是與使用正常的依賴關係解析代替的提供的範圍的區別)。 之後,您必須自己「提供」文件 - 例如將其放入CLASSPATH(因此與提供的範圍相似)。要將文件安裝到你的本地庫高速緩存,你可以參考這篇文章:

http://maven.apache.org/plugins/maven-install-plugin/examples/specific-local-repo.html

你可以只ommit的localrepository路徑和Maven將在他的本地「緩存」安裝在那裏纔去查找任何相關性到遠程存儲庫。

當您使用Class-Path條目創建manifest.mf時(例如,當您的應用程序在本地主機上運行時),Maven也將爲您提供支持:要了解其工作原理,請參閱here

相關問題