项目环境:
- spring-boot
- maven多模块项目
- 需要引入的外部jar包
目录结构图如下:
方式一, 打war包:
1. 在对应子模块的pom.xml
文件中引入jar包,本例因为在common和web 模块都引入了,因此则都需要分分别引入,common的pom.xml配置如下:
groupId、artifactId、version都是可以自己随意填的,当然最好还是按照一定得规律填写,方便区分,上图中红框中的部分就是需要特别注意的地方,scope
只能填写 system
,systemPath
则填写被引入jar包在项目中的位置。
注意:
${project.basedir}
和${basedir}
都表示项目根目录,即包含pom.xml文件的目录,这两个都是maven预定义的内置属性,用户可以直接使用。
common
层pom.xml
的全部内容:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>libii-identity</artifactId>
<groupId>com.libii.sso</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>common</artifactId>
<dependencies>
<dependency>
<groupId>com.libii.sso</groupId>
<artifactId>bcprov-jdk16-1.46.jar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${basedir}/lib/bcprov-jdk16-1.46.jar</systemPath>
</dependency>
<dependency>
<groupId>com.libii.sso</groupId>
<artifactId>commons-beanutils-1.8.0.jar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${basedir}/lib/commons-beanutils-1.8.0.jar</systemPath>
</dependency>
</dependencies>
</project>
2. 配置web
模块pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>libii-identity</artifactId>
<groupId>com.libii.sso</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>web</artifactId>
<!--1. 将打包方式变为 war包-->
<packaging>war</packaging>
<!--编译跳过test-->
<properties>
<skipTests>true</skipTests>
</properties>
<dependencies>
<!--2. 把内置的tomcat给注释掉 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.libii.sso</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.libii.sso</groupId>
<artifactId>backend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<!--3. 引入外部jar包 -->
<dependency>
<groupId>com.libii.sso</groupId>
<artifactId>junit-4.12.jar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${basedir}/lib/junit-4.12.jar</systemPath>
</dependency>
</dependencies>
<build>
<finalName>identity</finalName>
<plugins>
<plugin>
<!--4. 配置maven的war包插件 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<!--5. 配置各模块的jar包资源目录,一个模块则只需配置一个resource -->
<!-- 把web模块的jar包打进去 -->
<resource>
<directory>lib</directory>
<targetPath>WEB-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
<!-- 把common模块的jar包打进去 -->
<resource>
<!-- 特别声明: 此处需要使用相对路径,找到common模块 -->
<directory>../common/lib</directory>
<targetPath>WEB-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
<!--6. 下面的plugin为Springboot常规的打包插件,打war包时,加不加都没有关系,加上之后,会改变包结构,会让war包变大,所以推荐不加 -->
<!--<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>-->
</plugins>
</build>
</project>
使用误区:
1. 将jar包只放在web层,如果是其他子模块需要使用时候,则编译会出问题。
2. 将jar包只放在使用的模块,很多人会出现打包打不进去,因为你配置打包插件时没有使用子模块的相对路径,默认会使用当前模块的地址,
3. web模块和使用的模块都引入一次,打包能成功,使用也没问题,但是你不觉得很冗余吗,不够优雅。
最佳的方式:只在使用的模块引入jar包,在web层的pom.xml中配置子模块的jar包资源路径就行了
方式二, 打jar包:
1. 在用到此jar包的模块引入,同方式一的第一点类似。
2. 修改web
模块的打包方式为 jar
3. 为打包插件配置configuration
属性
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
web
模块完整的pom.xml
文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>libii-identity</artifactId>
<groupId>com.libii.sso</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>web</artifactId>
<packaging>jar</packaging>
<dependencies>
<!-- SpringBoot 项目热部署 依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.libii.sso</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.libii.sso</groupId>
<artifactId>backend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<!--3. 引入外部jar包 -->
<dependency>
<groupId>com.libii.sso</groupId>
<artifactId>junit-4.12.jar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${basedir}/lib/junit-4.12.jar</systemPath>
</dependency>
</dependencies>
<build>
<finalName>identity</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
</project>
参考博客:https://blog.csdn.net/J080624/article/details/81505937