我有一个基于 NetBeans maven 的项目,我想从 Java 1.8 切换到 12,但我无法编译它。
经过多次尝试,我决定从头开始,创建一个非常简单的项目来了解如何进行此更改。
好吧,我试过了,我也无法编译简单的项目。
谁能解释一下我做错了什么?
序言
NetBeans:10.0
JAVA_HOME: C:\Program Files\Java\jdk-12(我也试过9、10、11)
项目创建
- 创建带有窗口的项目(第一个 TopComponent)
- 我将 JDK 版本从 11(NetBeans 10.0 中的默认值)更改为 12
这是自动生成的 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>
然后我开始 Clean & Build > SUCCESS
我把 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>
更新版本字段,使其变为:
<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>
然后我重新启动 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
编辑
- 我已尝试按照建议删除 nbm-maven-plugin 但这会改变项目的结构,我不知道如何重新包含此模块
最佳答案
请像这样使用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/