maven打包 复制本地第三方jar包到指定目录的几种方式

背景

我的项目打包配成了项目代码单独jar包,依赖的jar包使用copy-dependencies复制到指定libs目录。但是本地jar包没法复制,研究了下,使用下面方法即可,亲测可用。

开发配置

在需要使用三方jar包的模块,pom文件中配置如下:

<dependency>
   <groupId>com.my.abc/groupId>  <!--自定义-->
     <artifactId>sdk</artifactId>    <!--自定义-->
     <version>1.0</version> <!--自定义-->
     <scope>system</scope> <!--system,类似provided,需要显式提供依赖的jar以后,Maven就不会在Repository中查找它-->
     <systemPath>${pom.basedir}/libs/my-customer1.jar</systemPath> <!--不能使用父项目的路径-->
 </dependency>

打包配置

具体打包模块的pom的< build >中配置:

方式一:

<resources>
   <!-- 复制本地扩展jar包到libs目录 -->
     <resource>
         <directory>../moduleA/libs</directory><!-- 要复制的moduleA模块libs目录下的jar包 -->
         <targetPath>${project.build.directory}/libs</targetPath><!-- 复制到本模块打包目录的libs目录 -->
         <includes>
             <include>**/*.jar</include>
         </includes>
     </resource>
     <!--定义了上面的resource, 要想默认的src/main/resources目录生效, 必须显式额外增加下面的配置-->
     <resource>
          <directory>src/main/resources</directory>
          <!-- 表示是否对配置文件中的${}占位符进行解析替换-->
          <filtering>false</filtering>
      </resource>
 </resources>

方式二:

 <plugin>
	 <artifactId>maven-antrun-plugin</artifactId>
	 <executions>
	     <execution>
	         <id>copy</id>
	         <phase>package</phase>
	         <configuration>
	             <target name="copy">	             	
	                 <copy todir="${project.build.directory}/libs"> <!-- 复制到本模块打包目录的libs目录 -->	                 
	                     <fileset dir="../moduleA/libs"> <!-- 要复制的moduleA模块libs目录下的jar包 -->
	                         <include name="*.jar"/>
	                     </fileset>
	                 </copy>
	             </target>
	         </configuration>
	         <goals>
	             <goal>run</goal>
	         </goals>
	     </execution>
	 </executions>
</plugin>
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Maven项目中,我们可以通过以下几种方式第三方本地JAR文件引入到war包中: 1. 通过 system scope 引入:在项目的pom.xml文件中,添加如下配置: ```xml <dependency> <groupId>com.example</groupId> <artifactId>example</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${project.basedir}/libs/example.jar</systemPath> </dependency> ``` 这种方式直接指定了本地JAR文件的路径,并将其作为系统路径引入。 2. 通过 systemPath 引入:在系统的pom.xml文件中,添加如下配置: ```xml <dependencies> ... <dependency> <groupId>com.example</groupId> <artifactId>example</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>/path/to/example.jar</systemPath> </dependency> ... </dependencies> ``` 与第一种方式类似,也是通过指定本地JAR文件的路径进行引入。 3. 通过 install 到本地仓库引入:首先需要使用以下命令将本地JAR文件安装到本地Maven仓库中: ``` mvn install:install-file -Dfile=/path/to/example.jar -DgroupId=com.example -DartifactId=example -Dversion=1.0 -Dpackaging=jar ``` 在项目的pom.xml文件中,添加如下配置: ```xml <dependencies> ... <dependency> <groupId>com.example</groupId> <artifactId>example</artifactId> <version>1.0</version> </dependency> ... </dependencies> ``` 这种方式会将本地JAR文件安装到Maven本地仓库中,然后从本地仓库中引入。 无论使用哪种方式,都需要在pom.xml文件中添加相应的依赖配置,以确保Maven能够正确地将第三方本地JAR文件打包进入war包中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

福&缘

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

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

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

打赏作者

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

抵扣说明:

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

余额充值