记一次maven打包报错Compilation failure:[找不到符号] [程序包XXX不存在]的处理过程

问题描述

[INFO] Reactor Summary for Demo 1.0:
[INFO]
[INFO] Demo ............................................... SUCCESS [  0.166 s]
[INFO] core ............................................... SUCCESS [  7.119 s]
[INFO] webAPI ............................................. FAILURE [  0.545 s]
[INFO] adminAPI ........................................... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  8.188 s
[INFO] Finished at: 2019-11-28T15:31:53+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project webAPI: Compilation failure: Compilation failure:
[ERROR] /E:/workspace-git/Demo/WebAPI/src/main/java/com/demo/webapi/controller/CitysController.java:[4,26] 程序包com.demo.core.anno不存在
[ERROR] /E:/workspace-git/Demo/WebAPI/src/main/java/com/demo/webapi/controller/CitysController.java:[5,26] 程序包com.demo.core.anno不存在
[ERROR] /E:/workspace-git/Demo/WebAPI/src/main/java/com/demo/webapi/controller/CitysController.java:[6,26] 程序包com.demo.core.bean不存在
[ERROR] /E:/workspace-git/Demo/WebAPI/src/main/java/com/demo/webapi/controller/CitysController.java:[7,31] 程序包com.demo.core.component不存在
[ERROR] /E:/workspace-git/Demo/WebAPI/src/main/java/com/demo/webapi/controller/CitysController.java:[8,32] 程序包com.demo.core.controller不存在
[ERROR] /E:/workspace-git/Demo/WebAPI/src/main/java/com/demo/webapi/controller/CitysController.java:[9,29] 程序包com.demo.core.service不存在
[ERROR] /E:/workspace-git/Demo/WebAPI/src/main/java/com/demo/webapi/controller/CitysController.java:[29,38] 找不到符号
[ERROR]   符号: 类 BaseController
[ERROR] /E:/workspace-git/Demo/WebAPI/src/main/java/com/demo/webapi/controller/CitysController.java:[29,53] 找不到符号
[ERROR]   符号: 类 CitysService
[ERROR] /E:/workspace-git/Demo/WebAPI/src/main/java/com/demo/webapi/controller/CitysController.java:[27,2] 找不到符号
[ERROR]   符号: 类 RequiredToken
[ERROR] /E:/workspace-git/Demo/WebAPI/src/main/java/com/demo/webapi/controller/CitysController.java:[31,13] 找不到符号
[ERROR]   符号:   类 CitysService
[ERROR]   位置: 类 com.demo.webapi.controller.CitysController
[ERROR] /E:/workspace-git/Demo/WebAPI/src/main/java/com/demo/webapi/controller/CitysController.java:[37,13] 找不到符号
[ERROR]   符号:   类 FanoutProducer
[ERROR]   位置: 类 com.demo.webapi.controller.CitysController
[ERROR] /E:/workspace-git/Demo/WebAPI/src/main/java/com/demo/webapi/controller/CitysController.java:[40,12] 找不到符号
[ERROR]   符号:   类 ResponseBean
[ERROR]   位置: 类 com.demo.webapi.controller.CitysController
[ERROR] /E:/workspace-git/Demo/WebAPI/src/main/java/com/demo/webapi/controller/CitysController.java:[48,12] 找不到符号
[ERROR]   符号:   类 ResponseBean
[ERROR]   位置: 类 com.demo.webapi.controller.CitysController
[ERROR] /E:/workspace-git/Demo/WebAPI/src/main/java/com/demo/webapi/controller/CitysController.java:[54,12] 找不到符号
[ERROR]   符号:   类 ResponseBean
[ERROR]   位置: 类 com.demo.webapi.controller.CitysController
[ERROR] /E:/workspace-git/Demo/WebAPI/src/main/java/com/demo/webapi/controller/CitysController.java:[60,12] 找不到符号
[ERROR]   符号:   类 ResponseBean
[ERROR]   位置: 类 com.demo.webapi.controller.CitysController
[ERROR] /E:/workspace-git/Demo/WebAPI/src/main/java/com/demo/webapi/controller/CitysController.java:[66,12] 找不到符号
[ERROR]   符号:   类 ResponseBean
[ERROR]   位置: 类 com.demo.webapi.controller.CitysController
[ERROR] /E:/workspace-git/Demo/WebAPI/src/main/java/com/demo/webapi/controller/CitysController.java:[76,12] 找不到符号
[ERROR]   符号:   类 ResponseBean
[ERROR]   位置: 类 com.demo.webapi.controller.CitysController
[ERROR] /E:/workspace-git/Demo/WebAPI/src/main/java/com/demo/webapi/controller/CitysController.java:[54,32] 找不到符号
[ERROR]   符号:   类 MultiRequestBody
[ERROR]   位置: 类 com.demo.webapi.controller.CitysController
[ERROR] /E:/workspace-git/Demo/WebAPI/src/main/java/com/demo/webapi/controller/CitysController.java:[54,71] 找不到符号
[ERROR]   符号:   类 MultiRequestBody
[ERROR]   位置: 类 com.demo.webapi.controller.CitysController
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project webAPI: Comp
ilation failure
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
Caused by: org.apache.maven.plugin.compiler.CompilationFailureException: Compilation failure
    at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute (AbstractCompilerMojo.java:1220)
    at org.apache.maven.plugin.compiler.CompilerMojo.execute (CompilerMojo.java:187)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
[ERROR]
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :webAPI

业务背景:项目是使用的spring boot 模块化开发,分为三个模块:即webAPI,core,adminAPI,其中core是其他两个的核心依赖包,本身不提供任何接口服务。

POM文件

这里只展示和打包相关的配置

父POM
<modelVersion>4.0.0</modelVersion>

<groupId>com.demo</groupId>
<artifactId>Demo</artifactId>
<packaging>pom</packaging>
<version>1.0</version>

<modules>
    <module>Core</module>
    <module>WebAPI</module>
    <module>AdminAPI</module>
</modules>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
    <relativePath/> 
    <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>
core POM文件
<modelVersion>4.0.0</modelVersion>

<artifactId>core</artifactId>
<packaging>jar</packaging>
<!--<version>1.0</version>-->
<name>core</name>
<description>Demo project for Spring Boot</description>

<parent>
    <artifactId>Demo</artifactId>
    <groupId>com.demo</groupId>
    <version>1.0</version>
</parent>
<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <profiles.active>dev</profiles.active>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <!-- 生产环境 -->
        <id>pro</id>
        <properties>
            <profiles.active>pro</profiles.active>
        </properties>
    </profile>
</profiles>
<build>
    <finalName>core</finalName>
    <plugins>
    	<!--这里注意下,这是出问题源头-->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>
                    src/main/resources/dev
                </exclude>
                <exclude>
                    src/main/resources/pro
                </exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/resources/${profiles.active}</directory>
        </resource>
    </resources>
</build>
webAPI POM文件
<dependencies>
    <dependency>
        <groupId>com.demo</groupId>
        <artifactId>core</artifactId>
        <version>1.0</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions>
        <type>jar</type>
    </dependency>

    <dependency>
        <groupId>org.springframework.retry</groupId>
        <artifactId>spring-retry</artifactId>
    </dependency>
</dependencies>

<build>
    <finalName>webAPI</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

amdinAPI就不贴了,和webAPI大同小异。
这时候执行打包命令:

mvn clean package  -Dmaven.test.skip=true

结果就是出现上述错误。但是core打包正常,到了打webAPI包才出的问题。一度让我认为是webAPI出现的问题。

问题原因

问题就出在spring-boot-maven-plugin这个打包方式上,就是core POM文件里的这段代码

<plugins>
	<!--这里注意下,这是出问题源头-->
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
  	</plugin>
</plugins>

**上面的这种打包方式是把core打包成可执行jar。无法当做普通依赖包被其他工程使用 **

说明:可执行jar包与依赖jar包中包含的内容是不一样的,大小上看,依赖jar包更小,可执行jar包更大。原因是,依赖jar包中主要包含了生产的代码,而可执行jar包中则包含了相关的底层库。
可执行jar作为依赖,会报出程序包或符号不存在的问题。

看这个core.jar文件70多M,这是工程包

解决方式

解决方法其实有两种:

第一种:

把打包方式更换为maven-compiler-plugin插件。具体操作就是把core pom文件修改为:

<plugins>
  	<!--这里注意下,这是出问题源头-->
	<plugin>
	     <groupId>org.springframework.boot</groupId>
	     <artifactId>spring-boot-maven-plugin</artifactId>
	</plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>8</source>
            <target>8</target>
        </configuration>
    </plugin>
</plugins>

执行打包命令。

[INFO] Reactor Summary for Demo 1.0:
[INFO]
[INFO] Demo ............................................... SUCCESS [  0.308 s]
[INFO] core ............................................... SUCCESS [  6.830 s]
[INFO] webAPI ............................................. SUCCESS [  6.561 s]
[INFO] adminAPI ........................................... SUCCESS [  8.675 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  22.857 s
[INFO] Finished at: 2019-11-29T14:58:00+08:00
[INFO] ------------------------------------------------------------------------

结果没问题。

第二种:

增加classifier属性,具体core pom文件:

<plugins>
	<!--这里注意下,这是出问题源头-->
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <!--新增-->
        <configuration>
            <classifier>exec</classifier>
        </configuration>
    </plugin>
</plugins>

这里为什么要加一个classifier标签:
spring-boot-maven-plugin在打包时会打两个包,一个.jar一个.jar.original,默认第一个.jar是一个可执行jar。classifier是用来区分两个jar包的,这是第一个就是普通的依赖包,第二个就是名字为exec的可执行jar。
在这里插入图片描述

这种方式的使用场景:就是这个模块即要作为普通依赖的jar,也要作为可执行jar使用。不过这种情况很少出现。

总结

这次问题的涉及点就是两种打包方式和两种jar的区别。说明了对于maven及pom配置文件的了解不够。

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值