springBoot(maven)项目引入本地jar并打包

一、引入本地jar并通过编译

1. 给项目添加外部依赖

在这里插入图片描述选自己要引入的jar并添加,依赖列表中会显示新添加的jar
注意 \color{#FF0000}{注意} 注意:若是多个模块,注意模块之间的依赖关系,不要在模块A中添加的外部依赖,却在模块B中使用,即使模块B引用了模块A,也要在B中再次引用这个外部的jar,原因下边讲…

上面的方法可以使项目在IDEA中正常运行,但是打包依然会找不到刚才引入的程序包,进而打包失败(无论 jar包还是war包都会失败)。

二. 项目打包

2.1 如果要使用maven打包,需要在pom文件中引入依赖
<dependency>
     <groupId>com.sylis</groupId>
     <artifactId>syLis-api</artifactId>
     <version>1.0</version>
     <scope>system</scope>
     <systemPath>${pom.basedir}/src/main/resources/lib/syLis-api-1.0.jar</systemPath>
 </dependency>

引入后可以在项目的依赖列表中看到
在这里插入图片描述
到这里,实现的效果跟1中的一样,项目可以在IDEA中运行,但是不能依赖传递和打包。

2.2 打包配置
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>2.1.1.RELEASE</version>
    <configuration>
    
 	     <!-- 如果没有该配置,devtools不会生效 -->
        <!-- <fork>true</fork>   -->
        
        <!-- 加入此配置,才会把本地的jar包也引进去 -->
        <includeSystemScope> true </includeSystemScope>  
        
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>

加入上面的配置 可以将本地依赖打进jar包

  • 打war包时如果报错
    Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project xxxxxx: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)
    导致的原因:
    maven的web项目默认的webroot是在src\main\webapp。如果在此目录下找不到web.xml就抛出以上的异常。
    解决:
    方法1:如果有用到web.xml,文件结构应该是这样
    在这里插入图片描述
    再次打包,maven找到了想要的web.xml 就不会抛异常了
    方法2:更改maven的配置,在pom文件中添加如下配置:
<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-war-plugin</artifactId>
 <version>3.1.0</version>
 <configuration>
     <!-- 缺少web.xml文件时候报错 false:不报错 -->
     <failOnMissingWebXml>false</failOnMissingWebXml>

     <warName>${project.artifactId}</warName>
 </configuration>
</plugin>

将引用的jar包代码打包

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-assembly-plugin</artifactId>
   <version>3.0.0</version>
   <configuration>
       <descriptorRefs>jar-with-dependencies</descriptorRefs>
   </configuration>
   <executions>
       <execution>
           <id>make-assembly</id>
           <phase>package</phase>
           <goals>
               <goal>single</goal>
           </goals>
       </execution>
   </executions>
</plugin>

至此,springBoot项目引入本地jar并打包发布结束


方法二:

(将jar包安装至本地仓库后使用pom文件直接引入)
mvn install:install-file -Dfile=jar包的位置 -DgroupId=上面的groupId -DartifactId=上面的artifactId -Dversion=上面的version -Dpackaging=jar

eg.:

mvn install:install-file  -Dfile=E:\work\workspace\IDEA\lkp-report-factory\src\main\resources\libs\aspose-words-14.9.0-jdk16.jar -DgroupId=com.aspose -DartifactId=aspose-words -Dversion=14.9.0-jdk16 -Dpackaging=jar

pom引入:

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-words</artifactId>
    <version>14.9.0-jdk16</version>
</dependency>

拓展

  • pom文件 dependency 中的 scope标签
    scope主要是为了管理依赖的部署,确定依赖的使用范围。使用scope标签,可以很方便、直观的进行依赖的总体管理。

scope的值

解释
compile默认的scope,表示 dependency 都可以在生命周期中使用。而且,这些dependencies 会传递到依赖的项目中。适用于所有阶段,会随着项目一起发布。
provided跟compile相似,但是表明了dependency 由JDK或者容器提供,例如Servlet AP和一些Java EE APIs。这个scope 只能作用在编译和测试时,同时没有传递性。
runtime表示dependency不作用在编译时,但会作用在运行和测试时,如JDBC驱动,适用运行和测试阶段。
test表示dependency作用在测试时,不作用在运行时。 只在测试时使用,用于编译和运行测试代码。不会随项目发布。
system 与provided类似,但是它不会去maven仓库寻找依赖,而是在本地找;而systemPath标签将提供本地路径
import这个标签就是 引入该dependency的pom中定义的所有dependency定义
  • pom文件中的 ${basedir}
    项目的根目录,即pom文件的同级目录,还有其他的写法${project.basedir}${pom.basedir}等 。
    一般用 project.basedir或pom.basedir指向会更明确

参考:
pom.xml使用scope为system

  • 4
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将外部Jar包引入Spring Boot项目中并打包Jar包中,可以按照以下步骤进行操作: 1. 在pom.xml文件中添加依赖项。例如,要引入一个名为example.jar的外部Jar包,可以通过以下方式添加依赖项: ``` <dependency> <groupId>com.example</groupId> <artifactId>example</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>${project.basedir}/lib/example.jar</systemPath> </dependency> ``` 其中,systemPath指定了外部Jar包的路径,scope设置为system,表示使用系统路径下的Jar包。 2. 将外部Jar包复制到项目的lib目录下,例如,将example.jar复制到项目目录下的lib文件夹中。 3. 在pom.xml文件中添加Maven插件,以将外部Jar包打包到生成的Jar包中。例如,可以添加以下插件: ``` <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.0</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.example.Application</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> ``` 其中,classpathPrefix指定了Jar包中lib文件夹下的依赖项路径前缀,mainClass指定了Spring Boot应用程序的主类。 4. 使用Maven命令进行打包,生成的Jar包中将包含外部Jar包。例如,使用以下命令进行打包: ``` mvn clean package ``` 这样,生成的Jar包中就包含了外部Jar包,并可以在运行时使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值