java - 无法使用高于 1.8 的 Java 的 maven-compiler-plugin 进行编译

标签 java maven java-8 maven-plugin maven-compiler-plugin

我有一个基于 NetBeans maven 的项目,我想从 Java 1.8 切换到 12,但我无法编译它。

经过多次尝试,我决定从头开始,创建一个非常简单的项目来了解如何进行此更改。

好吧,我试过了,我也无法编译简单的项目。

谁能解释一下我做错了什么?

序言

NetBeans:10.0

JAVA_HOME: C:\Program Files\Java\jdk-12(我也试过9、10、11)

专家: Console output of Maven

项目创建

  1. 创建带有窗口的项目(第一个 TopComponent)

Netbeans display window

  1. 我将 JDK 版本从 11(NetBeans 10.0 中的默认值)更改为 12

Project properties in NetBeans

  1. 这是自动生成的 POM 和 javax.annotation-api 依赖项

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>it.prj</groupId>
        <artifactId>Project-parent</artifactId>
        <version>2.9</version>
    </parent>
    <artifactId>Project-source</artifactId>
    <packaging>nbm</packaging>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>nbm-maven-plugin</artifactId>
                <extensions>true</extensions>
                <configuration>
                    <useOSGiDependencies>true</useOSGiDependencies>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <useDefaultManifestFile>true</useDefaultManifestFile>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.netbeans.api</groupId>
            <artifactId>org-netbeans-api-annotations-common</artifactId>
            <version>${netbeans.version}</version>
        </dependency>
        <dependency>
            <groupId>org.netbeans.api</groupId>
            <artifactId>org-openide-windows</artifactId>
            <version>${netbeans.version}</version>
        </dependency>
        <dependency>
            <groupId>org.netbeans.api</groupId>
            <artifactId>org-openide-util</artifactId>
            <version>${netbeans.version}</version>
        </dependency>
        <dependency>
            <groupId>org.netbeans.api</groupId>
            <artifactId>org-openide-util-ui</artifactId>
            <version>${netbeans.version}</version>
        </dependency>
        <dependency>
            <groupId>org.netbeans.api</groupId>
            <artifactId>org-openide-util-lookup</artifactId>
            <version>${netbeans.version}</version>
        </dependency>
        <dependency>
            <groupId>org.netbeans.api</groupId>
            <artifactId>org-openide-awt</artifactId>
            <version>${netbeans.version}</version>
        </dependency>
        <dependency>
            <groupId>org.netbeans.api</groupId>
            <artifactId>org-netbeans-modules-settings</artifactId>
            <version>${netbeans.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.3.2</version>
            <type>jar</type>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    

  2. 然后我开始 Clean & Build > SUCCESS

  3. 我把 Java 版本从 1.7 改成了 12

Changing java version in properties

这将相关的插件添加到 POM

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>12</source>
                <target>12</target>
            </configuration>
        </plugin>
  1. 更新版本字段,使其变为:

       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <source>12</source>
                <target>12</target>
            </configuration>
        </plugin>
    
  2. 然后我重新启动 Clean & Build > 失败

    cd D:\Project\Project-source; "JAVA_HOME=C:\\Program Files\\Java\\jdk-12" "M2_HOME=C:\\Program Files\\apache-maven-3.6.0" cmd /c "\"\"C:\\Program Files\\apache-maven-3.6.0\\bin\\mvn.cmd\" -Dmaven.ext.class.path=\"C:\\Program Files\\Netbeans 10.0\\java\\maven-nblib\\netbeans-eventspy.jar\" -Dfile.encoding=UTF-8 clean install\""
    

    构建项目源代码 2.9

    --- maven-clean-plugin:2.5:clean (default-clean) @项目源---

删除 D:\Project\Project-source\target

--- maven-resources-plugin:3.1.0:resources (default-resources) @ Project-source ---

Using 'UTF-8' encoding to copy filtered resources.
Copying 1 resource

--- maven-compiler-plugin:3.8.0:compile (default-compile) @ Project-source ---
Changes detected - recompiling the module!
Compiling 1 source file to D:\Project\Project-source\target\classes

--- nbm-maven-plugin:4.1:manifest (default-manifest) @ Project-source ---
NBM Plugin generates manifest
Adding OSGi bundle dependency - javax.annotation:javax.annotation-api
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Failed to execute goal org.codehaus.mojo:nbm-maven-plugin:4.1:manifest (default-manifest) on project Project-source: 
Execution default-manifest of goal org.codehaus.mojo:nbm-maven-plugin:4.1:manifest failed.: IllegalArgumentException 

编辑

  1. 我已尝试按照建议删除 nbm-maven-plugin 但这会改变项目的结构,我不知道如何重新包含此模块

enter image description here

最佳答案

请像这样使用4.2版本:

<groupId>org.apache.netbeans.utilities</groupId>
<artifactId>nbm-maven-plugin</artifactId>
<version>4.2</version>

替换对以下内容的任何引用:

 <groupId>org.codehaus.mojo</groupId>
 <artifactId>nbm-maven-plugin</artifactId>

jdk生成的jar文件>8老版本插件无法解析

关于java - 无法使用高于 1.8 的 Java 的 maven-compiler-plugin 进行编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55417803/

相关文章:

java - 在 Intellij CE 中,如何显示 Maven 选项卡?

maven - 复制资源时完全搜索和替换源文件中的字符串

java - 有没有办法将 spring boot maven pom 项目转换为 gradle?

java - 从具有相似属性的 List<Object> 创建 List<Set>

java - 使用 Java 登录函数式编程

java - 如何在 spring Controller 中将 JSON 转换为 java 对象,反之亦然?

java - 向下舍入时间至最后 5 分钟

java - Java中不同返回类型的重载?

java - 放弃连接警告 - tomcat 7 抛出 PooledConnection 异常

java - 增强 Java 8 的库,同时保持向后兼容性