maven 项目打包成可执行jar包的时候 报错 cannot find symbol

MAVEN 工程A 依赖 MAVEN 工程B

工程B 已经install在本地Maven repo 中了,eclipse 没有报错,是能够找到工程B的class 文件,但是打包的时候就报错
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project service-provider: Compilation failure: Compilation failure:
[ERROR] /xxxxx/oservice/service-provider/src/main/java/com/xxx/service/provider/SchoolServiceImpl.java:[15,17] cannot find symbol
[ERROR] symbol: class SchoolMapper
[ERROR] location: class zzz.xxx.service.provider.SchoolServiceImpl
[ERROR] /services/microservice/xxx-service-provider/src/main/java/com/xxx/service/provider/SchoolServiceImpl.java:[18,16] cannot find symbol

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <createDependencyReducedPom>false</createDependencyReducedPom>
                </configuration>
                
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.greatschools.service.provider.App</mainClass>
                                </transformer>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.handlers</resource>
                                </transformer>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.schemas</resource>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

原因分析:
spring-boot-maven-plugin打包出来的jar是不可依赖的

我们现在整合后的maven项目有一个parent工程,打包类型为pom,下面多个spring-boot工程作为它的module,分别为base和moduleA,moduleB。假如moduleA依赖于base。如果你在base中使用了spring-boot-maven-plugin的默认配置build,或者在parent工程中使用spring-boot-maven-plugin的默认配置build。那么在clean package的时候会发现moduleA找不到base中的类。原因就是默认打包出来的jar是不可依赖的。

解决方案:

    官方告诉我们,你如果不想移代码,好吧,我这样来给你解决,给你打两个jar包,一个用来直接执行,一个用来依赖。于是,你需要指定一个属性classifier,这个属性为可执行jar包的名字后缀。比如我设置<classifier>exec</classifier>,原项目名为Vehicle-business。那么会得到两个jar:Vehicle-business.jar和Vehicle-bussiness-exec.jar

     官方文档位置:84.5 Use a Spring Boot application as a dependency

     总结:回到聚合maven上,如果你在parent工程中使用了spring-boot-maven-plugin作为builder,那么你的依赖module一定要用解决方案二来设置。否则你不在parent工程中用spring-boot-maven-plugin作为builder,而在需要打包的module上使用。

一般parent工程的maven插件配置如下:

org.springframework.boot spring-boot-maven-plugin1.8 1.8 repackage 被依赖的maven子模块的maven插件配置如下(其余maven子模块就不需要配置): org.springframework.boot spring-boot-maven-plugin exec

————————————————
版权声明:本文为CSDN博主「DamonREN」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/DamonREN/article/details/85091900

你重启一下eclipse

试过了,不行