項目環境:
- spring-boot
- maven多模塊項目
- 需要引入外部jar包
目錄結構圖如下:
方式一, 打jar包,待寫中。。。
方式二, 打war包:
1. 在對應子模塊的pom.xml
文件中引入jar
包,本例因爲在common
和web
模塊都引入了,因此則都需要分分別引入,common
的pom.xml
配置如下:
groupId、artifactId、version都是可以自己隨意填的,當然最好還是按照一定得規律填寫,方便區分,上圖中紅框中的部分就是需要特別注意的地方,scope
只能填寫 system
,systemPath
則填寫被引入jar包在項目中的位置。
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>
注意:
${project.basedir}
和${basedir}
都表示項目根目錄,即包含pom.xml
文件的目錄,這兩個都是maven
預定義的內置屬性,用戶可以直接使用。
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相關的jar包打進去 -->
<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包資源路徑就行了