Maven引入本地Jar包並打包進War包中
1.概述
在平時的開發中,有一些Jar包因為種種原因,在Maven的中央倉庫中沒有收錄,所以就要使用本地引入的方式加入進來。
2. 拷貝至項目根目錄
項目根目錄即pom.xml文件所在的同級目錄,可以在項目根目錄下創建文件夾lib,如下圖所示:
這4個Jar包是識別網頁編碼所需的包。
3. 配置pom.xml,依賴本地Jar
配置Jar的dependency,包括groupId,artifactId,version三個屬性,同時還要包含scope和systemPath屬性,分別指定Jar包來源於本地文件,和本地文件的所在路徑。
<!-- ################################# cpdetector #################################### --> <dependency> <groupId>cpdetector</groupId> <artifactId>cpdetector</artifactId> <version>1.0.10</version> <scope>system</scope> <systemPath>${basedir}/lib/cpdetector_1.0.10.jar</systemPath> </dependency> <dependency> <groupId>antlr</groupId> <artifactId>antlr</artifactId> <version>2.7.4</version> <scope>system</scope> <systemPath>${basedir}/lib/antlr-2.7.4.jar</systemPath> </dependency> <dependency> <groupId>chardet</groupId> <artifactId>chardet</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${basedir}/lib/chardet-1.0.jar</systemPath> </dependency> <dependency> <groupId>jargs</groupId> <artifactId>jargs</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${basedir}/lib/jargs-1.0.jar</systemPath> </dependency>
其中,${basedir}是指項目根路徑
4. 配置Maven插件將本地Jar打包進War中
在進行以上配置以后,編寫代碼時已經可以引入Jar包中的class了,但是在打包時,由於scope=system,默認並不會將Jar包打進war包中,所有需要通過插件進行打包。
修改pom.xml文件,在plugins標簽下加入下面的代碼
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.10</version> <executions> <execution> <id>copy-dependencies</id> <phase>compile</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/lib</outputDirectory> <includeScope>system</includeScope> </configuration> </execution> </executions> </plugin>
這樣,打出來的war包中,就會包含本地引入的jar依賴了。