1. 程式人生 > >maven編譯引入本地jar包-解決Maven中使用很多本地jar包的編譯問題

maven編譯引入本地jar包-解決Maven中使用很多本地jar包的編譯問題

Maven依賴本地非repository中的jar包,依賴jar包放在WEB-INF/lib等目錄下的情況客戶端編譯出錯的處理。

Maven提供了scope為system的依賴,文件的原文如下:

system

This scope is similar to provided except thatyou have to provide the JAR which contains it explicitly.

The artifact is always available and is notlooked up in a repository.

這樣就可以新增dependency而不需要再將WEB-INF/lib目錄下的jar包安裝到本地庫中了。

具體配置錄下:

<dependency>

<groupId>org.apache</groupId>

<artifactId>test</artifactId>

<version>1.0</version>

<scope>system</scope>

<systemPath>${basedir}/src/main/webapp/WEB-INF/lib/test.jar</systemPath>

</dependency>

如果只新增一個兩個jar包,還比較方便,但是如果WEB-INF/lib/下面有幾十個jar包,逐個新增就會顯得很繁瑣。最好是能方便配置WEB-INF/lib/目錄,讓該目錄下所有jar包都參與編譯。這個配置在maven-compiler-plugin中。配置編譯引數<compilerArguments>,新增extdirs將jar包相對路徑新增到配置中,如下:

<plugin>

<artifactId>maven-compiler-plugin</artifactId>

  <configuration>

    <source>1.6</source>

    <target>1.6</target>

    <encoding>UTF-8</encoding>

    <compilerArguments>

      <extdirs>src\main\webapp\WEB-INF\lib</extdirs>

    </compilerArguments>

  </configuration>

</plugin>

通過配置maven-compiler-plugin 的compilerArguments可以方便在使用Maven的時候,還大量使用以前使用Ant的大批WEB-INF/lib下面的歷史遺留jar包。