在開發過程中有時會用到maven倉庫里沒有的jar包或者本地的jar包,這時沒辦法通過pom直接引入,那么該怎么解決呢
一般有兩種方法
第一種是將本地jar包安裝在本地maven庫
第二種是將本地jar包放入項目目錄中
這篇文章主要講第二種方式,這又分兩種情況,一種是打包jar包,第二種是打包war包

1. pom文件中引入jar
<dependency>
<groupId>javacsv</groupId>
<artifactId>javacsv</artifactId>
<scope>system</scope>
<version>1.0</version>
<systemPath>${project.basedir}/src/main/resources/lib/javacsv.jar</systemPath>
</dependency>
<!-- groupId,artifactId,version可隨便寫 -->
這時候在項目中運行是沒問題了,但是使用命令mvn clean package打包之后BOOT-INF\lib里面並沒有javacsv.jar包,還需要pom文件中做如下配置:
2. 打war包
2.1 定義打包類型為war
<groupId>com.xx</groupId>
<artifactId>xx-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
2.2 定義war包插件
<build>
<resources>
<resource>
<directory>lib</directory>
<targetPath>/lib/</targetPath>
<includes>
<include>${project.basedir}/src/main/resources/lib/</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<!-- 定義war包插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<!-- 原路徑 -->
<directory>src/main/resources/lib</directory>
<targetPath>WEB-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
<!-- 定義war包插件 -->
<!--<plugin>-->
<!-- <artifactId>maven-compiler-plugin</artifactId>-->
<!-- <configuration>-->
<!-- <source>1.8</source>-->
<!-- <target>1.8</target>-->
<!-- <encoding>UTF-8</encoding>-->
<!-- <compilerArguments>-->
<!-- <extdirs>${project.basedir}/src/main/resources/lib</extdirs>-->
<!-- </compilerArguments>-->
<!-- </configuration>-->
<!--</plugin>-->
<!-- 打可執行jar包時 定義jar包插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
3. 打jar包
3.1 定義打包類型為jar
<groupId>com.xx</groupId>
<artifactId>xx-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
3.2 定義jar包插件(同上面文件一樣,通用都包含進去了)
<build>
<resources>
<resource>
<directory>lib</directory>
<targetPath>/lib/</targetPath>
<includes>
<include>${project.basedir}/src/main/resources/lib/</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<!-- 定義war包插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<!-- 原路徑 -->
<directory>src/main/resources/lib</directory>
<targetPath>WEB-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
<!-- 定義war包插件 -->
<!--<plugin>-->
<!-- <artifactId>maven-compiler-plugin</artifactId>-->
<!-- <configuration>-->
<!-- <source>1.8</source>-->
<!-- <target>1.8</target>-->
<!-- <encoding>UTF-8</encoding>-->
<!-- <compilerArguments>-->
<!-- <extdirs>${project.basedir}/src/main/resources/lib</extdirs>-->
<!-- </compilerArguments>-->
<!-- </configuration>-->
<!--</plugin>-->
<!-- 打可執行jar包時 定義jar包插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
4. 如果不加會出現的錯誤或者告警
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.new3s:zghWindPowerManage:jar:0.0.1-SNAPSHOT
[WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.apache.commons:commons-lang3:jar -> version 3.8.1 vs 3.6 @ line 85, column 21
[WARNING] 'dependencies.dependency.systemPath' for javacsv:javacsv:jar should not point at files within the project directory, ${project.basedir}/src/main/resources/lib/javacsv.jar will be unresolvable by dependent projects @ line 231, column 25
[WARNING] 'build.plugins.plugin.(groupId:artifactId)' must be unique but found duplicate declaration of plugin org.springframework.boot:spring-boot-maven-plugin @ line 275, column 21
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[WARNING] 'dependencies.dependency.systemPath' for javacsv:javacsv:jar should not point at files within the project directory, ${project.basedir}/src/main/resources/lib/javacsv.jar will be unresolvable by dependent projects @ line 231, column 25
打包后不存在(BOOT-INF\lib目錄下)

