【问题标题】:Impossible to compile using maven-compiler-plugin with Java higher than 1.8无法使用 Java 高于 1.8 的 maven-compiler-plugin 进行编译
【发布时间】:2019-08-20 10:00:03
【问题描述】:

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

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

嗯,我试过了,也编译不了这个简单的项目。

谁能解释我做错了什么?

序言

NetBeans:10.0

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

马文:

项目创建

  1. 使用窗口创建项目(第一个 TopComponent)

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

  1. 这是自动生成的带有 javax.annotation-api 依赖关系的 POM

    <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

这会将相关的插件添加到 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 > FAILED

    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\""
    

    Building Project-source 2.9

    --- maven-clean-plugin:2.5:clean (default-clean) @ Project-source ---

删除 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,但这改变了 Project 的结构,我不知道如何重新包含此模块

【问题讨论】:

  • nbm-maven-plugin 失败,不是编译。暂时从您的构建中删除nbm-maven-plugin,看看它是否有效。

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


【解决方案1】:

在您的示例中,nbm-maven-plugin 失败,但编译过程很好。

暂时从您的构建中删除nbm-maven-plugin,看看Maven 是否可以在没有它的情况下构建。也许您可以完全跳过这个插件,因为它似乎只针对 NetBeans IDE。

【讨论】:

  • 是的,删除插件构建工作;新问题是“nbm”行不再被识别并删除它,项目将无法工作
  • nbm-maven-plugin 似乎不再根据Github 维护,除非有人在维护它,否则它可能不适用于最近的 Java 版本。
【解决方案2】:

请像这样使用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老版本插件无法解析。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-04
    • 2015-03-14
    • 2020-08-13
    • 2021-02-07
    • 2020-12-22
    • 2021-04-18
    • 2018-11-13
    • 2018-11-25
    相关资源
    最近更新 更多