首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Maven 打包專案時包含本地 JAR:system scope 與 maven-install-plugin 兩大方案

     編輯:Java 相關知識

專案依賴中央倉庫沒有的第三方 JAR(如廠商私有 SDK、舊版專屬驅動),Maven 打包時預設不會包含。本文介紹兩種標準解法:system scope(舊專案相容)與 maven-install-plugin 安裝至本地倉庫(現代推薦),並說明打包插件設定細節。

方案一:system Scope(不推薦新專案)

<dependency>
    <groupId>com.example</groupId>
    <artifactId>legacy-lib</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/legacy-lib-1.0.jar</systemPath>
</dependency>

缺點:

  • 不會傳遞給依賴此專案的下游模組(傳遞性中斷)。
  • 路徑硬編碼,跨機器建構易失敗。
  • Maven 3.9+ 已棄用警告。

方案二:maven-install-plugin 安裝至本地倉庫(推薦)

1. 專案內綁定 install 階段自動安裝

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <version>3.1.2</version>
            <executions>
                <execution>
                    <id>install-external-jars</id>
                    <phase>initialize</phase>
                    <goals><goal>install-file</goal></goals>
                    <configuration>
                        <file>${project.basedir}/lib/legacy-lib-1.0.jar</file>
                        <groupId>com.example</groupId>
                        <artifactId>legacy-lib</artifactId>
                        <version>1.0</version>
                        <packaging>jar</packaging>
                        <generatePom>true</generatePom>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

執行 mvn clean install 即會自動安裝,後續依賴可用普通 compile scope 引用。

2. 手動安裝指令(一次性)

mvn install:install-file \
    -Dfile=lib/legacy-lib-1.0.jar \
    -DgroupId=com.example \
    -DartifactId=legacy-lib \
    -Dversion=1.0 \
    -Dpackaging=jar \
    -DgeneratePom=true

打包插件確保 JAR 進入最終產物

若為 Spring Boot Fat Jar:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <includeSystemScope>true</includeSystemScope>  <!-- 僅 system scope 需要 -->
    </configuration>
</plugin>

若為普通 maven-shade-plugin 或 maven-assembly-plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <configuration>
        <createDependencyReducedPom>false</createDependencyReducedPom>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals><goal>shade</goal></goals>
        </execution>
    </executions>
</plugin>

安裝至本地倉庫後,systemPath 依賴會被視為普通依賴正確打包。

進階:建立私有 Nexus/Artifactory 代理本地 JAR

  • 團隊專案請部署至私有倉庫,mvn deploy:deploy-file 上傳。
  • 所有成員共享,避免各自手動 install、版本不一致。
  • CI/CD 流程自動發布,版本管理完整。

常見問題

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
現象原因解決
打包後缺 Classsystem scope 未傳遞、shade 插件未配置改用 install-file + 普通依賴
版本衝突多模組各自 install 同 artifactId 不同版本統一私有倉庫、強制版本管理
IDE 無法識別本地倉庫更新後 IDE 未重新導入Maven Reload Project / Reimport