在使用maven进行打包项目中,想要将第三方的jar包放入,有两种方法:一种将jar包上传到maven库中,第二种再pom.xml中进行配置,第三种 情况是需要打包成war包放入tomcat中;具体如下:

第一种:安装第三方jar包到maven库


1. 安装maven
1、下载mvn到本地,解压。

2、新建系统变量MAVEN_HOME,值指向安装目录如D:\apache-maven-3.3.6

3、path变量中增加:%MAVEN_HOME%\bin;

4、在MAVEN_HOME/conf/setting.xml中的<localRepository>标签可修改仓库位置(默认位置在C盘的用户下),如<localRepository>D:\software\repository</localRepository>

5、验证,cmd下输入命令查看安装是否成功(需要先安装好JDK):mvn -version

6、安装本地jar到仓库


安装命令:

mvn install:install-file -Dfile=jar包的位置 -DgroupId=上面的groupId -DartifactId=上面的artifactId -Dversion=上面的version -Dpackaging=jar

例如:

mvn install:install-file -Dfile=D:\lib\test-1.0.5.jar.jar -DgroupId=test -DartifactId=test -Dversion=1.0.5 -Dpackaging=jar

将文件打到本地仓中(D:\software\repository路径下多出test文件夹,同时多出上面那个jar包)

在pom.xml文件中增加依赖项
<dependency>
    <groupId>test</groupId>
    <artifactId>test</artifactId>
    <version>1.0.5.</version>
</dependency>


第二种:修改pom.xml文件


 对于jar包

maven插件spring-boot-maven-plugin中添加配置<includeSystemScope>true</includeSystemScope>配置,如下:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <includeSystemScope>true</includeSystemScope>
            </configuration>
        </plugin>
    </plugins>
</build>


 然后,添加依赖项:

        <dependency>
            <groupId>com.gis</groupId>
            <artifactId>test</artifactId>
            <version>1.0.5</version>
            <scope>system</scope>
            <systemPath>${pom.basedir}/src/main/resources/lib/test-1.0.5.jar</systemPath>
        </dependency>

 打包后,在lib文件夹下,就会多出一个test-1.0.5.jar文件。

第三种:我们需要打成war包,直接放到tomcat中运行(正在用)

    war包打包后会生成一个WEB-INF/lib-provided的文件夹,访问时,会报错,因为tomcat是读取WEB-INF/lib里的jar包

需要添加maven-war-plugin插件,

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <webResources>
                        <resource>
                            <directory>src/main/resources/lib</directory>
                            <targetPath>WEB-INF/lib/</targetPath>
                            <includes>
                                <include>**.jar</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>


打war包后,仍然会有lib-provided文件夹,而我们需要在lib下存在第三方jar包,这个时候我们就需要在pom.xml 新增上面代码。

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐