前提
多模塊項目的時候,如B依賴A:B->A,在A項目中依賴了一個本地jar包:
A模塊的POM文件部分:
<dependency> <groupId>org.csource</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.29</version> <scope>system</scope> <systemPath>${project.basedir}/lib/XXX.jar</systemPath> </dependency>
注:如果要打包本地jar到項目環境
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>xxxx\lib</outputDirectory> <excludeTransitive>false</excludeTransitive> <stripVersion>false</stripVersion> <includeScope>system</includeScope> </configuration> </execution> <!--打包 scope 為 compile 的 jar 包--> <execution> <id>copy-dependencies2</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>xxxx\lib</outputDirectory> <includeScope>runtime</includeScope> </configuration> </execution> </executions> </plugin>
如果需要現在編譯B,並且在B中使用了XXX.jar里面的類,在打包的的時候會提示
java: 程序包XXX.XXX.XXX不存在
在日志中發現了一個警告:
[WARNING] The POM for xxx:jar:1.0 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
說是一個jar包的pom無效,傳遞依賴項(如果有的話)將不可用。也就是說,Maven的模塊(Module)B依賴了A,而A又依賴了一個jar,A的pom無效,所以B編譯時就報錯了,各種找不到類、找不到包。
解決方法
方法一
A層Module的引用中,去掉第三方的jar,尋找一個Maven中央倉庫可以引用到的jar代替;在項目根目錄,mvn clean deploy,把B層引用到的jar包傳到私服上面;這次再運行mvn clean install -Dmaven.test.skip=true,發現通過,問題解決。
方法二
設置模塊B的編譯參數
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.0</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> <compilerArguments> <extdirs>${project.basedir}/../XXX/lib</extdirs> </compilerArguments> </configuration> </plugin>
這樣就是在編譯的時候獲取對應的路徑查找,就可以編譯通過了