Maven can't compile java 1.8

后端 未结 8 1389
旧巷少年郎
旧巷少年郎 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

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

    1.8
    1.8
    

    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: 
    
     
     
     
     
     ${maven.compiler.compilerId}
     ${maven.compiler.compilerReuseStrategy}    
     ${maven.compiler.compilerVersion}
     ${maven.compiler.debug}
     ${maven.compiler.debuglevel}
     UTF-8
     ${maven.compiler.executable}
     ${maven.compiler.failOnError}
     ${maven.compiler.forceJavacCompilerUse}
     ${maven.compiler.fork}
     
     ${maven.compiler.maxmem}
     ${maven.compiler.meminitial}
     
     ${maven.compiler.optimize}
     
     
     
     ${maven.compiler.showDeprecation}
     ${maven.compiler.showWarnings}
     ${maven.test.skip}
     ${maven.compiler.skipMultiThreadWarning}
     1.6
     ${lastModGranularityMs}
     1.6
     ${maven.compiler.testSource}
     ${maven.compiler.testTarget}
     ${maven.compiler.useIncrementalCompilation}
      ${maven.compiler.verbose}
    

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

    
      1.8
      1.8
    
    

    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.

提交回复
热议问题