2017-06-14 132 views
0

我想在我的Maven項目中安裝和使用第三方Jar。
第一罐是:使用Maven安裝第三方Jar

anthill3-client.jar中
這是他的結構:

├───com 
│ └───urbancode 
│  ├───anthill3 
│  │ ├───command 
│  │ ├───custom 
│  │ ├───dashboard 
│  │ ├───domain 
│  │ └───wsviewer 
│  ├───codestation2 
│  │ ├───domain 
│  │ └───server 
│  ├───commons 
│  │ ├───db 
│  │ ├───logfile 
│  │ └───util 
│  ├───license 
│  ├───persistence 
│  │ └───collections 
│  └───scripting 
└───META-INF 

我通過運行該命令安裝它:

call mvn install:install-file -Dfile=anthill3-client.jar -DgroupId=com.urbancode -DartifactId=anthill3-client -Dversion=1.0 -Dpackaging=jar 

配置它在pom.xml中:

<dependency> 
    <groupId>com.urbancode</groupId> 
    <artifactId>anthill3-client</artifactId> 
    <version>1.0</version> 
</dependency> 

,並能夠在我的代碼使用它通過導入一些類:

import com.urbancode.anthill3.domain.persistent.PersistenceException; 
import com.urbancode.anthill3.domain.security.AuthorizationException; 
import com.urbancode.anthill3.main.client.AnthillClient; 
import com.urbancode.anthill3.persistence.UnitOfWork; 
import com.urbancode.anthill3.domain.project.*; 

它沒有相當的工作,雖然,因爲我得到.ClassNotFoundException

Exception in thread "main" java.lang.NoClassDefFoundError: com/urbancode/devilfish/services/method/MethodCall 
    at Valor.CM.Tools.App.main(App.java:26) 
Caused by: java.lang.ClassNotFoundException: com.urbancode.devilfish.services.method.MethodCall 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
    ... 1 more 

我試圖修復它安裝缺少的瓶子:

devilfish.jar
他的結構:

├───com 
│ └───urbancode 
│  ├───command 
│  │ ├───shell 
│  │ └───var 
│  ├───commons 
│  │ └───util 
│  ├───devilfish 
│  │ ├───client 
│  │ ├───common 
│  │ ├───server 
│  │ └───services 
│  └───plugin 
└───META-INF 

運行以下命令安裝它:

call mvn install:install-file -Dfile=devilfish.jar -DgroupId=com.urbancode -DartifactId=devilfish -Dversion=1.0 -Dpackaging=jar 

,並添加到我的POM:

<dependency> 
    <groupId>com.urbancode</groupId> 
    <artifactId>devilfish</artifactId> 
    <version>1.0</version> 
</dependency> 

但是從包導入類根本不工作,我不斷收到同樣的例外。 我想這是因爲我沒有正確安裝罐子。 我根據對我有意義的情況填充了-DgroupId & -DartifactId,但我不確定是否填寫了正確的值。

+0

所以你得到一個運行時異常,而不是編譯時錯誤? 這可能是起訴到沒有在運行時類路徑上的jar – jr593

+0

是的,這是一個運行時異常。它正好在我使用'com.urbancode.anthill3.main.client.AnthillClient'類的類中失敗。 –

回答

0

不知道你是如何運行你的項目,但編譯時間沒問題,並且運行時間不合適,所以我猜你沒有建立你的jar。

爲了你包與依賴你的罐子,按照此:How can I create an executable JAR with dependencies using Maven?

<build> 
<plugins> 
    <plugin> 
    <artifactId>maven-assembly-plugin</artifactId> 
    <configuration> 
     <archive> 
     <manifest> 
      <mainClass>fully.qualified.MainClass</mainClass> 
     </manifest> 
     </archive> 
     <descriptorRefs> 
     <descriptorRef>jar-with-dependencies</descriptorRef> 
     </descriptorRefs> 
    </configuration> 
    </plugin> 
</plugins> 
</build> 
+0

雖然我是否正確安裝了罐子? –

+0

是的,沒關係,如果你可以使用它的API,這意味着你有它。問題出在運行時應用程序尋找第三方jar,但無法找到它。嘗試以不同的方式構建 – Yoniseg

+0

如果您可以在答案本身中添加所有必要的信息會更好。不建議添加鏈接,因爲它們可能被破壞或內容可以稍後更改。 –

0

打開devilfish.jar(用的壓縮和解工具)和移動到META-INF /行家/ [幾款套餐]/pom.xml中。打開它的pom.xml並查找正確的組,工件名稱和版本。然後用適當的設置重新安裝。如果urbancode是您的公司,請爲您的公司安裝Artifactory或Nexus服務器,將文件發佈到那裏並將此服務器添加到您的pom.xml。

+0

META-INF不包含pom.xml。只有MANIFEST.MF與此內容: '清單-版本:1.0' '螞蟻版本:Apache Ant的1.9.7' '創建,通過:pxa6460sr16-20140418_01(SR16)(IBM公司)' 我不t認爲Jars是用Maven構建的 –

+0

它裏面沒有「maven」文件夾嗎?應該在這個文件夾structurue裏面。如果不是它不是Maven神器。 – Stefan

+0

這不是一個Maven神器。但不知何故,我能夠從其中一個調用方法 –