java - 如何在 NetBeans 8 上使用旧版本的 Java(不是默认版本)和 Maven 项目

标签 java maven netbeans

我需要使用 Java 6,但即使在我进行了所有配置之后,NetBeans 也没有使用它。它一直在为我的 IDE 使用默认的 Java 8 运行 Maven。相同的配置在 Eclipse 上运行良好,所以我不知道我做错了什么。

我不想在 nb-configuration.xml 中定义编译器,因为我必须提交该文件。我希望 NetBeans 能够从 POM 中正确获取它。

一般信息:

  • 我正在使用在 Java 8 上运行的 NetBeans 8.0.1(完全更新)
  • NetBeans 8 需要 JDK 7+ 才能工作
  • 使用 Maven 3.2.3,但嵌入式(Maven 3.0.5 不能正常工作)
  • 所有 Maven 插件都是最新的

要重现,只需在 NetBeans 中创建任何类型的 Maven 项目。就我而言,我尝试使用 Java、Web 和 EJB,但都没有用。

下图表示JDK已正确添加到IDE中。

工具 > Java 平台

添加了 JDK 1.6。

enter image description here

我尝试过的 POM 配置:

属性

<properties>
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
</properties>

编译器插件

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.2</version>
        <configuration>
            <source>1.6</source>
            <target>1.6</target>
        </configuration>
    </plugin>

执行器插件

强制执行 Java 6 会出现以下错误:

Detected JDK Version: 1.8.0-25 is not in the allowed range [1.6,1.7).
------------------------------------------------------------------------
BUILD FAILURE

执行者的配置:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>1.3.1</version>
        <executions>
            <execution>
                <id>enforce-versions</id>
                <goals>
                    <goal>enforce</goal>
                </goals>
                <configuration>
                    <rules>
                        <requireJavaVersion>
                            <version>[1.6,1.7)</version>
                        </requireJavaVersion>
                    </rules>
                </configuration>
            </execution>
        </executions>
    </plugin>

Java 6 测试代码

下面的代码应该编译:

import java.nio.file.Files;
import java.time.LocalTime;

public class JavaVersionChecker {

    public static void main(String[] args) {

        System.out.println(Files.class); // Java 7 API
        System.out.println("Time: " + LocalTime.now()); // Java 8 API

        // Print Java version
        System.out.println(System.getProperty("java.version"));
    }

}

测试输出

测试代码使用 Java 7 和 8 API 编译,但只接受 Java 6。

------------------------------------------------------------------------
Building java6_project 1.0-SNAPSHOT
------------------------------------------------------------------------

--- exec-maven-plugin:1.2.1:exec (default-cli) @ java6_project ---
class java.nio.file.Files
Time: 19:24:05.997
1.8.0_25
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 0.610 s
Finished at: 2014-11-21T19:24:06-02:00
Final Memory: 6M/123M
------------------------------------------------------------------------

IDE 应检测到 JDK 6

enter image description here

最佳答案

正如评论所建议的那样,我使用 hint 属性进行配置,生成的 nb-configuration.xml 中也有很好的记录。

添加到 pom.xml 的配置:

<properties>
    <netbeans.hint.jdkPlatform>JDK_1.6</netbeans.hint.jdkPlatform>
</properties>

据我了解,NetBeans 的编译版本需要通过专有参数进行设置,设置起来非常简单。

有用的 nb-configuration.xml 详细信息:

Properties that influence various parts of the IDE, especially code formatting and the like. You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up. That way multiple projects can share the same settings (useful for formatting rules for example). Any value defined here will override the pom.xml file value but is only applicable to the current project.

关于java - 如何在 NetBeans 8 上使用旧版本的 Java(不是默认版本)和 Maven 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27070750/

相关文章:

java - 如何在action类和jsp页面之间传递对象数据?

java - Faces Servlet 的 doGet 方法

netbeans 之外的 java hashmap 空指针

html - Netbeans:在 HTML 中围绕一个词包装代码?

java - 是否有使用编码/解码的替代方法?

java - 如何在正在运行的 JVM 中查找类实例

java - 如何创建具有构建路径使用的依赖项的 Maven 项目 jar?

windows - Jenkins : Selenium GUI tests are not visible on Windows

java - 使用 Thymeleaf 从 Spring Boot 中的 Jar/war 文件访问 HTML/JavaScript 文件

java - 您好,需要有关 jTable 的帮助