一、引入本地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

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐