maven package多模块项目时出现: 程序包XXX.XXX不存在

前提

多模块项目的时候,如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>

这样就是在编译的时候获取对应的路径查找,就可以编译通过了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章