What causes imported Maven project in Eclipse to use Java 1.5 instead of Java 1.6 by default and how can I ensure it doesn't?

前端 未结 13 2090
情深已故
情深已故 2020-11-27 09:51

I imported a Maven project and it used Java 1.5 even though I have 1.6 configured as my Eclipse default Preferences->Java->Installed JREs.

When I

相关标签:
13条回答
  • 2020-11-27 10:01

    If you want to make sure that newly created projects or imported projects in Eclipse use another default java version than Java 1.5, you can change the configuration in the maven-compiler-plugin.

    • Go to the folder .m2/repository/org/apache/maven/plugins/maven-compiler-plugin/3.1
    • Open maven-compiler-plugin-3.1.jar with a zip program.
    • Go to META-INF/maven and open the plugin.xml
    • In the following lines:
      <source implementation="java.lang.String" default-value="1.5">${maven.compiler.source}</source>
      <target implementation="java.lang.String" default-value="1.5">${maven.compiler.target}</target>

    • change the default-value to 1.6 or 1.8 or whatever you like.

    • Save the file and make sure it is written back to the zip file.

    From now on all new Maven projects use the java version you specified.

    Information is from the following blog post: https://sandocean.wordpress.com/2019/03/22/directly-generating-maven-projects-in-eclipse-with-java-version-newer-than-1-5/

    0 讨论(0)
  • 2020-11-27 10:03
    <project>
    
        <!-- ... -->
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.2</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    0 讨论(0)
  • 2020-11-27 10:06

    I wanted to add something to the answer already provided. maven-compiler-plugin by default will compile your project using Java 1.5 which is where m2e get's its information.

    That's why you have to explicitly declare the maven-compiler-plugin in your project with something other then 1.5. Your effective pom.xml will implicitly use the default set in the maven-compiler-plugin pom.xml.

    0 讨论(0)
  • 2020-11-27 10:07

    Here is the root cause of java 1.5:

    Also note that at present the default source setting is 1.5 and the default target setting is 1.5, independently of the JDK you run Maven with. If you want to change these defaults, you should set source and target.

    Reference : Apache Mavem Compiler Plugin

    Following are the details:

    Plain pom.xml

        <?xml version="1.0" encoding="UTF-8"?>
        <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>
    
        <groupId>com.pluralsight</groupId>
        <artifactId>spring_sample</artifactId>
        <version>1.0-SNAPSHOT</version>
    
    </project>
    

    Following plugin is taken from an expanded POM version(Effective POM),

    This can be get by this command from the command line C:\mvn help:effective-pom I just put here a small snippet instead of an entire pom.

        <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <executions>
          <execution>
            <id>default-compile</id>
            <phase>compile</phase>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
          <execution>
            <id>default-testCompile</id>
            <phase>test-compile</phase>
            <goals>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    

    Even here you don't see where is the java version defined, lets dig more...

    Download the plugin, Apache Maven Compiler Plugin » 3.1 as its available in jar and open it in any file compression tool like 7-zip

    Traverse the jar and findout

    plugin.xml

    file inside folder

    maven-compiler-plugin-3.1.jar\META-INF\maven\

    Now you will see the following section in the file,

          <configuration>
        <basedir implementation="java.io.File" default-value="${basedir}"/>
        <buildDirectory implementation="java.io.File" default-value="${project.build.directory}"/>
        <classpathElements implementation="java.util.List" default-value="${project.testClasspathElements}"/>
        <compileSourceRoots implementation="java.util.List" default-value="${project.testCompileSourceRoots}"/>
        <compilerId implementation="java.lang.String" default-value="javac">${maven.compiler.compilerId}</compilerId>
        <compilerReuseStrategy implementation="java.lang.String" default-value="${reuseCreated}">${maven.compiler.compilerReuseStrategy}</compilerReuseStrategy>
        <compilerVersion implementation="java.lang.String">${maven.compiler.compilerVersion}</compilerVersion>
        <debug implementation="boolean" default-value="true">${maven.compiler.debug}</debug>
        <debuglevel implementation="java.lang.String">${maven.compiler.debuglevel}</debuglevel>
        <encoding implementation="java.lang.String" default-value="${project.build.sourceEncoding}">${encoding}</encoding>
        <executable implementation="java.lang.String">${maven.compiler.executable}</executable>
        <failOnError implementation="boolean" default-value="true">${maven.compiler.failOnError}</failOnError>
        <forceJavacCompilerUse implementation="boolean" default-value="false">${maven.compiler.forceJavacCompilerUse}</forceJavacCompilerUse>
        <fork implementation="boolean" default-value="false">${maven.compiler.fork}</fork>
        <generatedTestSourcesDirectory implementation="java.io.File" default-value="${project.build.directory}/generated-test-sources/test-annotations"/>
        <maxmem implementation="java.lang.String">${maven.compiler.maxmem}</maxmem>
        <meminitial implementation="java.lang.String">${maven.compiler.meminitial}</meminitial>
        <mojoExecution implementation="org.apache.maven.plugin.MojoExecution">${mojoExecution}</mojoExecution>
        <optimize implementation="boolean" default-value="false">${maven.compiler.optimize}</optimize>
        <outputDirectory implementation="java.io.File" default-value="${project.build.testOutputDirectory}"/>
        <showDeprecation implementation="boolean" default-value="false">${maven.compiler.showDeprecation}</showDeprecation>
        <showWarnings implementation="boolean" default-value="false">${maven.compiler.showWarnings}</showWarnings>
        <skip implementation="boolean">${maven.test.skip}</skip>
        <skipMultiThreadWarning implementation="boolean" default-value="false">${maven.compiler.skipMultiThreadWarning}</skipMultiThreadWarning>
        <source implementation="java.lang.String" default-value="1.5">${maven.compiler.source}</source>
        <staleMillis implementation="int" default-value="0">${lastModGranularityMs}</staleMillis>
        <target implementation="java.lang.String" default-value="1.5">${maven.compiler.target}</target>
        <testSource implementation="java.lang.String">${maven.compiler.testSource}</testSource>
        <testTarget implementation="java.lang.String">${maven.compiler.testTarget}</testTarget>
        <useIncrementalCompilation implementation="boolean" default-value="true">${maven.compiler.useIncrementalCompilation}</useIncrementalCompilation>
        <verbose implementation="boolean" default-value="false">${maven.compiler.verbose}</verbose>
        <mavenSession implementation="org.apache.maven.execution.MavenSession" default-value="${session}"/>
        <session implementation="org.apache.maven.execution.MavenSession" default-value="${session}"/>
      </configuration>
    

    Look at the above code and find out the following 2 lines

        <source implementation="java.lang.String" default-value="1.5">${maven.compiler.source}</source>
        <target implementation="java.lang.String" default-value="1.5">${maven.compiler.target}</target>
    

    Good luck.

    0 讨论(0)
  • 2020-11-27 10:09

    I added this to my pom.xml below the project description and it worked:

    <properties>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
    </properties>
    
    0 讨论(0)
  • Your JRE was probably defined in run configuration. Follow these steps in Eclipse to change the build JRE.

    1) Right click on the project and select Run As > Run Configurations

    2) From Run Configurations window, select your project build configuration on the left panel. On the right, you will see various tabs: Main, JRE, Refresh, Source,...

    3) Click on JRE tab, you should see something like this

    enter image description here

    4) By default, Work Default JRE (The JRE you select as default under Preferences->Java->Installed JREs) will be used. If you want to use another installed JRE, tick the Alternate JRE checkbox and select your preferred JRE from the dropdown.

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