Set default java compliance level for Maven projects in Eclipse

前端 未结 4 1714
情歌与酒
情歌与酒 2020-12-31 07:27

The default compliance level for Maven is 1.5 and every time that I update a maven project in Eclipse it sets back the compliance level from 1.6 to 1.5 which is really annoy

相关标签:
4条回答
  • 2020-12-31 07:37

    This worked for me..

    <build>
    <finalName>ProjectName</finalName>
    <plugins>
       <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-compiler-plugin</artifactId>
           <version>2.3.1</version>
           <configuration>
               <source>1.6</source>
               <target>1.6</target>
           </configuration>
       </plugin>
    </plugins>
    </build>
    
    0 讨论(0)
  • 2020-12-31 07:40

    Another way (if you do not have already compiler plugin defined) is to set just these properties in your pom file:

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

    Details: https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html

    0 讨论(0)
  • 2020-12-31 07:44

    Go to the project's pom.xml and insert below in between tags. Then the eclipse plugin (like m2eclipse) should rebuild the workspace.

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
    
        </plugins>
    </build>
    

    Note: this is project-level as only the project's pom.xml is affected.

    0 讨论(0)
  • 2020-12-31 07:59

    I know that I can set the target to 1.6 in pom file but the problem is that I cannot set this in the parent pom and expect the children to inherit it.

    Setting the <source> and <target> version in the parent pom does work.

    For example, in my parent pom, I have:

    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>2.3.2</version>
          <configuration>
            <source>1.6</source>
            <target>1.6</target>
            <encoding>UTF-8</encoding>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
    

    If you are having problems, you might want to check that:

    • the child specifies the correct version of the parent;
    • the parent pom specifies both source and target values;
    • you have run mvn install on the parent; and
    • mvn help:effective-pom on the child project shows the expected source/target values.

    After modifying the poms, you may need to select both projects and use Maven->Update Project.

    0 讨论(0)
提交回复
热议问题