java8 maven 编译失败

从 java7迁移到 java8, 使用了 java8 的lambdas语法,执行 mvn -X package 异常

[ERROR] /Users/workspace/Repositories/work/architecture/baymax/shared/src/main/test.java:[40,68] diamond operator is not supported in -source 1.6
[ERROR] (use -source 7 or higher to enable diamond operator)
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project shared: Compilation failure
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
    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:858)
    at org.apache.maven.plugin.compiler.CompilerMojo.execute(CompilerMojo.java:129)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
    ... 20 more

解决方式:

  1. 检查java 版本,java -version 确定使用 java1.8,设置$JAVA_HOME,java 版本可以使用 jenv
  2. 检查mvn 中 java 版本信息,mvn -v 确定 mvn 用的 java 版本是1.8
  3. 设置编译 source, target版本号为1.8,有两种方式:
    (1)设置项目中的 pom 文件
    </build>
         <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
         </plugins>
    </build>

(2)设置系统maven setting.xml

       <profile>
          <id>nexus</id>
          <properties>
              <maven.compiler.source>1.8</maven.compiler.source>
              <maven.compiler.target>1.8</maven.compiler.target>                      <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
          </properties>
       </profile>
 <activeProfiles>
     <activeProfile>nexus</activeProfile>
 </activeProfiles>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Maven 是一个流行的 Java 构建工具,用于自动化软件项目的构建、测试和部署过程。在开发过程中,可能会遇到各种编译错误,如未找到的依赖、代码语法错误或配置问题。当你想忽略某个特定的编译错误时,可以通过调整Maven的配置、使用插件或者临时处理错误来进行。 1. **忽略单个错误:** 在`pom.xml`中,`<build>`标签下的`<plugins>`标签内,你可以为`maven-compiler-plugin`配置一个`<compilerArgument>`元素来添加特定的命令行参数。例如,忽略某个特定警告`-Werror:unchecked`可以写成: ```xml <compilerArgument>-Xlint:unchecked</compilerArgument> <compilerArgument>-Wno-error=unchecked</compilerArgument> ``` 2. **使用 profiles:** 创建一个 profile,当需要时启用,可以暂时忽略错误: ```xml <profiles> <profile> <id>ignore-build-errors</id> <activation> <activeByDefault>false</activeByDefault> </activation> <build> <plugins> <plugin> <!-- 插件配置,可能包括忽略错误的设置 --> </plugin> </plugins> </build> </profile> </profiles> ``` 激活 profile 时,这些编译错误会被忽略。 3. **插件管理:** 使用插件如`maven-no-fail-plugin`,它可以在编译失败时继续执行后续步骤,而不是中断整个构建过程。 4. **临时注释或修复:** 如果错误是由于临时的代码修改引起的,可以在代码中添加注释(如`@ SuppressWarnings`)或者直接修复错误,等到下一次提交时再解决。 5. **忽略特定的依赖问题:** 如果是依赖冲突或缺失的问题,检查`pom.xml`中的`dependencies`部分,确保正确配置了所有依赖,并使用`exclusions`来排除已知的问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值