Different maven compiler versions for test and main

前端 未结 3 1976
-上瘾入骨i
-上瘾入骨i 2020-12-13 13:49

How can I configure the maven compiler to use java 5 for my test code and java 1.4 for my main code?

3条回答
  •  一生所求
    2020-12-13 14:30

    If you want to set compliance to the relevant Java version, you can configure the compiler plugin for each execution. Assuming Maven is using a JDK at least as current as the highest version you specify. By using properties you can override that configuration on the commandline or in a child if needed:

    
      org.apache.maven.plugins
      maven-compiler-plugin
      
        ${compileSource}
        ${compileSource}
      
      
        
          test-compile
          process-test-sources
          
            testCompile
          
          
            ${testCompileSource}
            ${testCompileSource}
          
        
      
    
    ...
    
      1.4
      1.5
    
    

    If you mean using different compilers, that's a bit more involved. as you need to specify the path to the JDK and what compiler version you're using. Again these can be defined in properties. Though you may want to define them in your settings.xml

    
      org.apache.maven.plugins
      maven-compiler-plugin
      
        ${compileSource}
        ${compileSource}
        ${compileJdkPath}/bin/javac
        ${compileSource}
      
      
        
          test-compile
          process-test-sources
          
            testCompile
          
          
            ${testCompileSource}
            ${testCompileSource}
            ${testCompileJdkPath}/bin/javac
            ${testCompileSource}
          
        
      
    
    ...
    
      1.4
      1.5
      path/to/jdk
      path/to/test/jdk
    
    

    Note it might make sense to define the compiler configurations in profiles, one for each JDK you support, so that your normal builds don't rely on properties being set.

    Also, in Maven 3.x, you need to include the fork parameter when specifying the executable, e.g.:

      
        maven-compiler-plugin
        3.1
        
          
            default-testCompile
            test-compile
            
              testCompile
            
            
              true
              ${testCompileJdkPath}/bin/javac
              1.8
              1.8
                        
          
        
      
    

提交回复
热议问题