maven中指定jdk的版本的两种方式解答

问题背景

解决方案

临时方案

一劳永逸方案

1.全局配置

2. 局部配置


 

问题背景

在maven项目中,使用到了JDK1.8的新特性

但是每次修改maven配置,模块的java语言版本就会自动切换为1.5版本的

这样导致了使用JDK1.8新特性的代码报错

解决方案

临时方案

idea使用java8新特性,报红提示不支持方法引用

一劳永逸方案

1.全局配置

全局配置是指在mavend的settings.xml文件中,在<profiles> </profiles>之间添加如下代码

<profile>
    <id>jdk18</id>
    <activation>
        <activeByDefault>true</activeByDefault>
        <jdk>1.8</jdk>
    </activation>
    <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></activeProfiles>里激活

<activeProfiles>
    <activeProfile>jdk18</activeProfile>
  </activeProfiles>

2. 局部配置

 局部配置就是只针对具体某个项目进行配置的。具体就是,在项目的pom.xml文件中添加如下代码:

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

猜你喜欢

转载自blog.csdn.net/lgl782519197/article/details/109029203
今日推荐