2016-07-14 50 views
1

所以我試圖將兩個本地jar文件包含到我有的Maven項目中,但沒有這樣做。我嘗試瞭解這些線程中的解決方案:12但它仍然沒有工作。這裏是我的pom.xml文件的關鍵部分:在maven項目中包含本地jar文件 - 找不到文件

<repository> 
     <id>local-maven-repo</id> 
     <url>file://${basedir}/resources</url> 
    </repository> 

然後依存關係:

<dependency> 
     <groupId>edu.mlab.jar1</groupId> 
     <artifactId>jar1_local</artifactId> 
     <version>1.0</version> 
    </dependency> 

    <dependency> 
     <groupId>edu.mlab.jar2</groupId> 
     <artifactId>jar2_local</artifactId> 
     <version>1.0</version> 
    </dependency> 

兩個jar文件包括包聲明在edu.mlab.jar1edu.mlab.jar2分別,因此,這就是我想要他們。 jar文件位於資源文件夾中,正好位於基本目錄下。

這就是設置。現在,當我嘗試mvn package(後mvn clean)我收到以下錯誤

[ERROR] Failed to execute goal on project PROJECT: Could not resolve dependencies for project edu.mlab.project:PROJECT:war:1.0-SNAPSHOT: The following artifacts could not be resolved: edu.mlab.jar1:jar1_local:jar:1.0, edu.mlab.jar2:jar2_local:jar:1.0: Failure to find edu.mlab.jar1:jar1_local:jar:1.0 in file:///Users/mlab/Desktop/2016/project_web/resources was cached in the local repository, resolution will not be reattempted until the update interval of local-maven-repo has elapsed or updates are forced -> [Help 1]

我真的不知道發生了什麼錯誤看到,因爲我jar1和jar2正是在資源文件夾中。另外,我已經嘗試了將它們與系統範圍一起導入的方法,但這不適合我的目的,因爲我希望它們包含在戰爭工件中。

非常感謝!

+0

你有文件在這裏file:///Users/mlab/Desktop/2016/project_web/resources/edu/mlab/jar1/jar1_local/jar1_local-1.0.jar? – davidxxx

+0

不,我不這樣做,但是不是包裝宣言應該照顧的嗎? –

+0

我重新編輯了路徑。不,Maven search jar在groupId/artifactId/artifactId-version.jar中,其中必須將每個點字符的目錄拆分爲groupid。 試試:) – davidxxx

回答

1
  1. 首先嚐試這兩個jar文件使用命令安裝到Maven的本地倉庫下面

mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=path-to-your-artifact-jar \ 
                       -DgroupId=your.groupId \ 
                       -DartifactId=your-artifactId \ 
                       -Dversion=version \ 
                       -Dpackaging=jar \ 
                       -DlocalRepositoryPath=path-to-specific-local-repo 

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

注:當地Maven存儲庫在安裝$ M2_HOME \ conf \ setting中指定s.xml例如

<localRepository>C:\local_maven_repo</localRepository> 
  • 然後使用常規Maven標籤

    <dependency> 
        <groupId>your.groupId</groupId> 
        <artifactId>your-artifactId</artifactId> 
        <version>your-version</version> 
    </dependency> 
    
  • +0

    看這如果我想創建一個戰爭神器這仍然成立? –

    +0

    這兩個jar文件將被包含在war文件的WEB-INF/lib中。 –

    +0

    好的,在這種情況下,我想設置的groupID/artifactID是什麼? –

    1

    假設:您使用Windows和你jar1 & jar2在C :\ Users \ yourusername folder

    1. 設置您的M2_HOME環境變量,例如,

      C:\Users\yourusername>set M2_HOME=C:\apache-maven-3.3.9 C:\Users\yourusername>echo %M2_HOME% C:\apache-maven-3.3.9

    2. 手動創建一個文件夾C:\ local_maven_repo然後編輯編輯文件

      C:\ Apache的行家-3.3.9 \的conf \設置。XML如下 enter image description here

    3. 運行MVN命令到安裝裝配jar1 & jar2到C:\ local_maven_repo,例如

    C:\Users\yourusername>mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=jar1.jar -DgroupId=edu.mlab -DartifactId=jar1 -Dversion=1.0 -Dpackaging=jar -DlocalRepositoryPath=C:\local_maven_repo

    C:\Users\yourusername>mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=jar2.jar -DgroupId=edu.mlab -DartifactId=jar2 -Dversion=1.0 -Dpackaging=jar -DlocalRepositoryPath=C:\local_maven_repo

  • 驗證jar1 & jar2是在C:\ local_maven_repo,例如
  • C:\Users\yourusername>dir C:\local_maven_repo\edu\mlab\jar1\1.0\

    C:\Users\yourusername>dir C:\local_maven_repo\edu\mlab\jar2\1.0\

  • 打開文件C:\ local_maven_repo \ EDU \ MLAB \ jar1 \ 1.0 \ jar1-1.0.pom,和C:\ local_maven_repo \ EDU \ MLAB \ jar2 \ 1.0 \ jar2-1.0.pom

  • 文件:C:\ local_maven_repo \ EDU \ MLAB \ jar1 \ 1.0 \ jar1-1.0.pom

    <groupId>edu.mlab</groupId> 
        <artifactId>jar1</artifactId> 
        <version>1.0</version> 
    

    文件:C:\ local_maven_repo \ EDU \ MLAB \ jar2 \ 1.0 \ jar2-1.0.pom

    <groupId>edu.mlab</groupId> 
        <artifactId>jar2</artifactId> 
        <version>1.0</version> 
    

    複製以下幷包含在你的pom.xml如下

    <dependency> 
        <groupId>edu.mlab</groupId> 
        <artifactId>jar1</artifactId> 
        <version>1.0</version> 
    <dependency> 
    <dependency> 
        <groupId>edu.mlab</groupId> 
        <artifactId>jar2</artifactId> 
        <version>1.0</version> 
    <dependency> 
    
    相關問題