在Maven項目中使用本地JAR包有兩種方法。
1. 使用system scope
<dependencies> <dependency> <groupId>org.richard</groupId> <artifactId>my-jar</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${project.basedir}/lib/my-jar.jar</systemPath> </dependency> </dependencies>
system scope引入的包,在使用jar-with-dependencies打包時將不會被包含,可以使用resources將本地包打進jar-with-dependencies
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <finalName>xxx-jar-with-dependencies</finalName> </configuration> </execution> </executions> </plugin> </plugins> <resources> <resource> <targetPath>lib/</targetPath> <directory>lib/</directory> <includes> <include>**/my-jar.jar</include> </includes> </resource> </resources> </build>
生成的xxx-jar-with-dependencies.jar中,將會包含lib目錄以及my-jar.jar,並且能夠被在執行的時候被找到。
有的時候這種方法會實效,比如JDBCDriver在聲明的時候Class.forName("xxx.Driver")就會說找不到類,用下面兩種方法就可以。
2. 將jar包安裝到本地repository中
mvn install:install-file -Dfile=my-jar.jar -DgroupId=org.richard -DartifactId=my-jar -Dversion=1.0 -Dpackaging=jar
引用:
<dependency> <groupId>org.richard</groupId> <artifactId>my-jar</artifactId> <version>1.0</version> </dependency>