1. 程式人生 > >1111_如何新增本地JAR檔案到Maven專案中

1111_如何新增本地JAR檔案到Maven專案中

<dependency>
  <groupId>com.czh.smessage</groupId>
  <artifactId>CCP_SMS</artifactId>
  <version>2.6.3</version>
  <scope>system</scope>
  <systemPath>${project.basedir}/src/lib/CCP_REST_SMS_SDK_JAVA_v2.6.3r.jar</systemPath>
</dependency>

如何新增本地JAR檔案到Maven專案中

2017年11月16日 13:20:04 ShuSheng007 閱讀數:31263 標籤: maven java idea 更多

個人分類: Java

有時我們需要將本地的jar檔案新增到maven專案中,如果直接通過IDEA新增本地庫引用的話,打包時候會丟失。網上中文教程的解決方法非常複雜,自己調查了一下,希望有此問題的同行不用再走彎路。

第一種方案:將jar安裝到本地Maven倉庫

第一步

首先確定你的電腦已經安裝了Maven。在命令列中鍵入mvn -v命令,如果出現類似如下圖所示,說明你的電腦已經安裝了Maven

,可進行第二步,如果沒有請安裝Maven

檢查是否安裝maven

第二步

安裝jar到本地倉庫

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
  • 1
  • 2

<path-to-file>: 要安裝的JAR的本地路徑<group-id>:要安裝的JAR的Group Id<artifact-id>

: 要安裝的JAR的 Artificial Id<version>: JAR 版本<packaging>: 打包型別,例如JAR

NOTE:最好在pom.xml檔案所在的目錄執行上述命令,個人經驗不在根目錄執行有時會安裝不成功

第三步

使用,例如我安裝了百度推送JAR到本地,我就可以在pom.xml檔案中這樣引用它了

<dependency>
    <groupId>com.baidu.app</groupId>
    <artifactId>bdpush</artifactId>
    <version>3.0.1</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

總結:這種方法弊端較大,程式的可維護性以及移植性較低。例如當你改變本地Maven倉庫時需要重新安裝。如果引用此JAR的專案是多人協調工作的專案,則每個人都要將其安裝在自己的本地倉庫。

解決辦法

可以將此JAR檔案放在工程的根目錄下,讓其隨著專案走,然後在pom.xml檔案中使用maven-install-pluginMaven初始化階段完成安裝。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <version>2.5</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>install-file</goal>
            </goals>
            <configuration>
                <groupId>com.baidu.app</groupId>
                <artifactId>bdpush</artifactId>
                <version>3.0.1</version>
                <packaging>jar</packaging>
                <file>${basedir}/lib/bdpush-3.0.1.ja</file>
            </configuration>
        </execution>
    </executions>
</plugin>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

我使用IDEA上面的程式碼是可以工作的,如果你們使用Eclipse報錯的話,加入如下程式碼

<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.codehaus.mojo</groupId>
                                <artifactId>aspectj-maven-plugin</artifactId>
                                <versionRange>[1.0,)</versionRange>
                                <goals>
                                    <goal>test-compile</goal>
                                    <goal>compile</goal>
                                </goals>
                            </pluginExecutionFilter>
                            <action>
                                <execute />
                            </action>
                        </pluginExecution>
                        <pluginExecution>
                            <pluginExecutionFilter>
                                <groupId>
                                    org.apache.maven.plugins
                                </groupId>
                                <artifactId>
                                    maven-install-plugin
                                </artifactId>
                                <versionRange>
                                    [2.5,)
                                </versionRange>
                                <goals>
                                    <goal>install-file</goal>
                                </goals>
                            </pluginExecutionFilter>
                            <action>
                                <execute>
                                    <runOnIncremental>false</runOnIncremental>
                                </execute>
                            </action>
                        </pluginExecution>
                    </pluginExecutions>
                </lifecycleMappingMetadata>
            </configuration>
        </plugin>
    </plugins>
</pluginManagement>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

下面是我在IDEA中使用SpringBoot時候的配置

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>install-file</goal>
            </goals>
            <configuration>
                <groupId>com.baidu.app</groupId>
                <artifactId>bdpush</artifactId>
                <version>3.0.1</version>
                <packaging>jar</packaging>
                <file>${basedir}/lib/bdpush-3.0.1.jar</file>
            </configuration>
        </execution>
    </executions>
</plugin>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

${basedir}表示pom.xml檔案所在的目錄

第二種方案

第二種方法比較粗暴簡單,具體為將依賴設定為系統域,通過完全路徑引用。例如要引用的JAR檔案在 <PROJECT_ROOT_FOLDER>/lib下,那麼使用如下方法新增依賴

<dependency>
    <groupId>com.baidu.app</groupId>
    <artifactId>bdpush</artifactId>
    <version>3.0.1</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/bdpush-3.0.1.ja</systemPath>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

${basedir}表示pom.xml檔案所在的目錄,例如你的JAR檔案在D盤下的jarLibs裡面,就將${basedir}替換為“D:/jarLibs”即可。

note: 這種方法我自己在SpringBoot專案中打包成war檔案時,沒有成功打包到裡面

第三種方案

第三種方案與第一種差不多,不同的是JAR檔案被安裝在一個單獨的倉庫裡。這個本地倉庫建在你專案的根目錄下,隨著專案走。

例如 1:我們在${basedir}pom.xml檔案所在路徑)目錄下建立一個叫“maven-repository”的本地倉庫。

2:使用如下命令安裝我們要引用的JAR到此倉庫中

mvn deploy:deploy-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=jar -Durl=file:./maven-repository/ -DrepositoryId=maven-repository -DupdateReleaseInfo=true    
  • 1

3:在pom.xml中如下使用

申明倉庫

<repositories>
    <repository>
        <id>maven-repository</id>
        <url>file:///${project.basedir}/maven-repository</url>
    </repository>
</repositories>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

然後新增引用

<dependency>
    <groupId>com.baidu.app</groupId>
    <artifactId>bdpush</artifactId>
    <version>3.0.1</version>
</dependency>