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

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

enter image description here

依赖的方式如下:

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

enter image description here

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

这样的话项目中就可以使用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>

整体配置截图如下:

enter image description here

然后使用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进行如下设置:

enter image description here

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

保存配置即可!

  • 10
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

坏菠萝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值