Maven项目依赖外部jar进行打包的两种方式

    xiaoxiao2022-07-05  162

    项目中除了pom.xml依赖之外,有时还依赖了其他jar包,如图:

    依赖的方式如下:

    点击Project Structure进行项目设置,在Modules中Dependencies标签中点击+号 添加lib下的所有jar,如图:

    然后在Artifacts的Output Layout标签中将依赖放到/WEB-INF/lib目录下,如图: 

    这样的话项目中就可以使用lib中依赖的jar了,但是如果要打包则会报错,须进行相关配置。

    打war包的时候有两种方式:

    第一种方式

    在pom.xml中的build标签内容添加resources标签,如下:

    <resources> <resource> <directory>lib</directory> <targetPath>BOOT-INF/lib/</targetPath> <includes> <include>**/*.jar</include> </includes> </resource> </resources>

    然后在plugin标签的configuration标签内加入compilerArguments标签,如下:

    <compilerArguments> <!-- 打包本地jar包 --> <extdirs>${project.basedir}/lib</extdirs> </compilerArguments>

    整体配置截图如下:

    然后使用maven命令进行打包即可:

    install -Dmaven.test.skip=true

    第二种方式

    在pom.xml中通过以下方式引入lib中的每个依赖

    <dependency> <groupId>com.xxx.www</groupId> <artifactId>out-jar-1</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>${project.basedir}/lib/commons-cxxxx.jar</systemPath> </dependency> <dependency> <groupId>com.xxx.www</groupId> <artifactId>out-jar-2</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>${project.basedir}/lib/commons-httxxxx.jar</systemPath> </dependency> ....

    其中groupId和artifactId可以随便填,注意artifactId不要重复了

    然后使用maven命令进行打包即可:

    install -Dmaven.test.skip=true

    如果是SpringBoot项目还要加如下配置:

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

    主要是

    <includeSystemScope>true</includeSystemScope>

    注意

    1、如果启动tomcat时一直卡在 Deploying web application archive ...

    是因为linux或者windows系统提供随机数设备是/dev/random 和/dev/urandom ,

    urandom安全性没有random高,但random需要时间间隔生成随机数。jdk默认调用random。

    解决步骤如下:

    找到 jdk目录/jre/lib/security/Java.security文件,在文件中找到securerandom.source这个设置项,将

    securerandom.source=file:/dev/random

    改为

    securerandom.source=file:/dev/urandom

    2、如果打包后项目找不到lib依赖,点击Project Structure进行如下设置:

    在Artifacts的Output Layout标签中将依赖放到/WEB-INF/lib目录下

    保存配置即可!

    最新回复(0)