Maven can't compile java 1.8

后端 未结 8 1381
旧巷少年郎
旧巷少年郎 2021-02-04 03:39

I\'m trying to use maven to build a jar but I keep getting the error

ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile 
(d         


        
相关标签:
8条回答
  • 2021-02-04 04:28

    Because Maven does not know version of jdk you want to build. You can try with

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    
    0 讨论(0)
  • 2021-02-04 04:28

    I noticed that it seems that maven-compiler-plugin ignores the configurations

    <source>1.8</source>
    <target>1.8</target>
    

    In fact, building my project with -X maven's option, I see that in the configuration of the maven-compiler-plugin the source and target were forced to be 1.6:

    [DEBUG] --------------------------------------------------------------
    [DEBUG] Goal:          org.apache.maven.plugins:maven-compiler-plugin:3.3:testCompile (default-testCompile)
    [DEBUG] Style:         Regular
    [DEBUG] Configuration: <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
     <basedir default-value="${basedir}"/>
     <buildDirectory default-value="${project.build.directory}"/>
     <classpathElements default-value="${project.testClasspathElements}"/>
     <compileSourceRoots default-value="${project.testCompileSourceRoots}"/>
     <compilerId default-value="javac">${maven.compiler.compilerId}</compilerId>
     <compilerReuseStrategy default-value="${reuseCreated}">${maven.compiler.compilerReuseStrategy}    </compilerReuseStrategy>
     <compilerVersion>${maven.compiler.compilerVersion}</compilerVersion>
     <debug default-value="true">${maven.compiler.debug}</debug>
     <debuglevel>${maven.compiler.debuglevel}</debuglevel>
     <encoding default-value="${project.build.sourceEncoding}">UTF-8</encoding>
     <executable>${maven.compiler.executable}</executable>
     <failOnError default-value="true">${maven.compiler.failOnError}</failOnError>
     <forceJavacCompilerUse default-value="false">${maven.compiler.forceJavacCompilerUse}</forceJavacCompilerUse>
     <fork default-value="false">${maven.compiler.fork}</fork>
     <generatedTestSourcesDirectory default-value="${project.build.directory}/generated-test-sources/test-annotations"/>
     <maxmem>${maven.compiler.maxmem}</maxmem>
     <meminitial>${maven.compiler.meminitial}</meminitial>
     <mojoExecution default-value="${mojoExecution}"/>
     <optimize default-value="false">${maven.compiler.optimize}</optimize>
     <outputDirectory default-value="${project.build.testOutputDirectory}"/>
     <project default-value="${project}"/>
     <session default-value="${session}"/>
     <showDeprecation default-value="false">${maven.compiler.showDeprecation}</showDeprecation>
     <showWarnings default-value="false">${maven.compiler.showWarnings}</showWarnings>
     <skip>${maven.test.skip}</skip>
     <skipMultiThreadWarning default-value="false">${maven.compiler.skipMultiThreadWarning}</skipMultiThreadWarning>
     <source default-value="1.5">1.6</source>
     <staleMillis default-value="0">${lastModGranularityMs}</staleMillis>
     <target default-value="1.5">1.6</target>
     <testSource>${maven.compiler.testSource}</testSource>
     <testTarget>${maven.compiler.testTarget}</testTarget>
     <useIncrementalCompilation default-value="true">${maven.compiler.useIncrementalCompilation}</useIncrementalCompilation>
      <verbose default-value="false">${maven.compiler.verbose}</verbose>
    

    I solved the problem after much much searches and tries, by forcing the java compiler to use the desired source and target:

    <compilerArguments>
      <source>1.8</source>
      <target>1.8</target>
    </compilerArguments>
    

    This adds to the parameters of javac the two parameters above, overwriting the default.

    In my case, all works well.

    Just a note: I see that with the above configuration, maven calls the javac in this way:

    -d [...] -g -nowarn -target 1.6 -source 1.6 -encoding UTF-8 -source 1.8 -target 1.8
    

    The first two "-source" and "-target" parameters are the default parameters that the maven-compiler-plugin uses; the second couple of parameters are the parameters added by the plugin because of the configuration "compilerArguments" described above. It seems that javac just uses the last couple of parameters (if a parameter is repeated more times, the last one overwrites all the previouses), but I'm not sure that this can be considered a standard and "sure" behavior.

    I used different versions of maven-compiler-plugin, different version of maven. I checked in deep all my configurations (e.g. system variables, every single script used in execution of java or maven and so on) and I'm sure that all is ok.

    It's strange, but my conclusion is that there is a bug in the maven-compiler-plugin with java 8.

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