方式一、 将jar放入本地任意目录(不推荐)

 pom.xml 

        <!-- 
            groupId:cn.myCompany.myCommon 规则:公司性质.公司名.项目名或其它
            artifactId:jar包 自定义ID,通常与jar包名同名;pom通过groupId+artifactId+version找到maven仓库中唯一jar包
            version:jar包 版本号
            scope:三方jar 使用 system
            systemPath:直接使用当前系统目录 win系统 D:/mylibs/my-common-1.0.1.jar 或 mac 系统  /Users/xxx/mylibs/my-common-1.0.1.jar
         -->
        <dependency>
            <groupId>cn.myCompany.myCommon</groupId>
            <artifactId>my-common</artifactId>
            <version>1.0.1</version>
            <scope>system</scope>
            <systemPath>D:/mylibs/my-common-1.0.1.jar</systemPath>
        </dependency>

方式二、将jar包放入项目中

 pom.xml

        <!-- 
            groupId:cn.myCompany.myCommon 规则:公司性质.公司名.项目名或其它
            artifactId:jar包 自定义ID,通常与jar包名同名;pom通过groupId+artifactId+version找到maven仓库中唯一jar包
            version:jar包 版本号
            scope:三方jar 使用 system
            systemPath:${project.basedir} 表示当前项目根目录
         -->
        <dependency>
            <groupId>cn.myCompany.myCommon</groupId>
            <artifactId>my-common</artifactId>
            <version>1.0.1</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/lib/my-common-1.0.1.jar</systemPath><!-- 项目 src/main/resources/lib 目录下的 my-common-1.0.1.jar -->
        </dependency>

打包相关:

需要在 pom.xml 的 springboot打包插件 spring-boot-maven-plugin 中配置 configuration.includeSystemScope 为 true:打包时将所有scope为system的三方jar打入jar包

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!-- true表示打包时包含scope为system的第三方的jar包,在在第2步骤自定义依赖时<scope>system</scope> -->
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>
        </plugins>
    </build>

方式三、将jar包打入maven仓库,再通过pom引入jar

命令参数说明

-Dfile=需要导入的jar包所放的位置
-DgroupId=导入到本地仓库jar包的父级目录

-DartifactId=jar包文件夹的名称

-Dversion=版本号
–-settings=是maven的setting.xml配置文件,会把jar包打包到xml文件中配置的本地仓库,未配置会打包到默认的本地仓库中(可以在idea的maven配置中找到该路径,配置文件中指定了maven仓库目录;如果路径中包含空格,则需要将文件全路径用引号引起来)

maven命令执行: 

mac

mvn install:install-file -Dfile=/Users/xxx/mylibs/my-common-1.0.1.jar -DgroupId=cn.myCompany.myCommon -DartifactId=my-common -Dversion=1.0.1 -Dpackaging=jar --settings "/Users/lizhen/apache-maven-3.5.4 idea/conf/settings.xml"

win

mvn install:install-file -Dfile=D:\mylibs\my-common-1.0.1.jar -DgroupId=cn.myCompany.myCommon -DartifactId=my-common -Dversion=1.0.1 -Dpackaging=jar --settings "D:\apache-maven-3.5.4\settings.xml" 

报错:

[ERROR] Error executing Maven.
[ERROR] The specified user settings file does not exist: /Users/lizhen/apache-maven-3.5.4

解决:原因是目录下找不到指定文件 ,或 路径中有空格,需要将全路径用引号引起来。

 pom.xml

        
        <dependency>
            <groupId>cn.myCompany.myCommon</groupId>
            <artifactId>my-common</artifactId>
            <version>1.0.1</version>
        </dependency>

方式四、idea添加

File - Project Structure...

 Libraries - + - Java - 选择jar包 - OK

 

 

相关参考:

https://blog.csdn.net/QWERTY55555/article/details/127014859

https://blog.csdn.net/qq_32486597/article/details/127785075

https://blog.csdn.net/qq_35783715/article/details/128462674

groupId的取名方式 https://www.cnblogs.com/rxysg/p/15692343.html 

https://blog.csdn.net/qq_41026669/article/details/106747204

Logo

新一代开源开发者平台 GitCode,通过集成代码托管服务、代码仓库以及可信赖的开源组件库,让开发者可以在云端进行代码托管和开发。旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐