方式一 通过dependency引入
在项目根路径新建lib文件夹,把jar包扔进去
<dependency>
<groupId>com.test</groupId> <!--自定义-->
<artifactId>test</artifactId> <!--自定义-->
<version>1.0</version> <!--自定义-->
<scope>system</scope> <!--system,类似provided,需要显式提供依赖的jar以后,Maven就不会在Repository中查找它-->
<systemPath>${project.basedir}/lib/test.jar</systemPath> <!--项目根目录下的lib文件夹下-->
</dependency>
scope参数说明:
- compile
编译范围,默认scope,在工程环境的classpath(编译环境)和打包(如果是WAR包,会包含在WAR包中)时候都有效。
- provided
容器或JDK已提供范围,表示该依赖包已经由目标容器(如tomcat)和JDK提供,只在编译的classpath中加载和使用,打包的时候不会包含在目标包中。最常见的是j2ee规范相关的servlet-api和jsp-api等jar包,一般由servlet容器提供,无需在打包到war包中,如果不配置为provided,把这些包打包到工程war包中,在tomcat6以上版本会出现冲突无法正常运行程序(版本不符的情况)。
- runtime
一般是运行和测试环境使用,编译时候不用加入classpath,打包时候会打包到目标包中。一般是通过动态加载或接口反射加载的情况比较多。也就是说程序只使用了接口,具体的时候可能有多个,运行时通过配置文件或jar包扫描动态加载的情况。典型的包括:JDBC驱动等。
- test
测试范围,一般是单元测试场景使用,在编译环境加入classpath,但打包时不会加入,如junit等。
- system
系统范围,与provided类似,只是标记为该scope的依赖包需要明确指定基于文件系统的jar包路径。
因此光是上面的配置,打包是进不去的。可以通过以下两方式实现
1.在spring-boot-maven-plugin 添加<includeSystemScope>true</includeSystemScope>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
<executions>
<execution>
<phase>none</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
2. 也可以在resources中指定,如下
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<phase>none</phase>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources/lib</directory>
<targetPath>BOOT-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</resources>
</build>
方式二 将外部jar打入本地maven仓库
mvn install:install-file -Dfile=test.jar -DgroupId=com.test -DartifactId=test -Dversion=1.0 -Dpackaging=jar
引入依赖
<dependency>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
</dependency>
方式三 编译阶段指定外部lib
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<extdirs>lib</extdirs><!--指定外部lib-->
</compilerArguments>
</configuration>
</plugin>
方式四、启动项目时 通过java -Dloader.path="lib/"加载
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
</configuration>
</plugin>
</plugins>
项目启动 将 项目的 jar 和 刚创建的 lib 放在同级目录下(不是必须的)。
启动项目: java -Dloader.path="lib/" -jar xx.jar