Compile Maven Module with Different Java Version

后端 未结 3 653
夕颜
夕颜 2020-12-01 04:49

My maven project has a few modules: server, web, etc.

I would like to build all but my server module on Java 6. For the server module, I\'d like to compile it with J

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-01 05:03

    This is a very common question, so this answer is based on this article I wrote on my blog.

    Maven Toolchains

    To use multiple Java versions, you need to use Maven Toolchains, which require you to create a toolchains.xml file in your ~/.m2 Maven folder, containing all Java versions installed on your machine:

    
      
        jdk
        
          Java13
          13
        
        
          ${env.JAVA_HOME_13}
        
      
      
        jdk
        
          Java9
          9
        
        
          ${env.JAVA_HOME_9}
        
      
      
        jdk
        
          Java8
          8
        
        
          ${env.JAVA_HOME_8}
        
      
      
        jdk
        
          Java7
          7
        
        
          ${env.JAVA_HOME_7}
        
      
      
        jdk
        
          Java6
          6
        
        
          ${env.JAVA_HOME_6}
        
      
    
    

    The JAVA_HOME_13, JAVA_HOME_9, JAVA_HOME_8, JAVA_HOME_7, JAVA_HOME_6 environment variables are configured so that they reference the path where the associated Java version is installed.

    The FlexyPool parent pom.xml configuration file

    The parent pom.xml Maven configuration file of the FlexyPool project defines the global Java version settings

    
        8
        ...
    
    

    Now, we need to instruct both the compiler and the test plugins to use the configured java version.

    
        
            
                org.apache.maven.plugins
                maven-toolchains-plugin
                1.1
                
                    
                        
                            toolchain
                        
                    
                
                
                    
                        
                            ${jdk.version}
                        
                    
                
            
    
            
                org.apache.maven.plugins
                maven-compiler-plugin
                ${maven-compiler-plugin.version}
                
                    ${jdk.version}
                    ${jdk.version}
                    true
                    true
                
            
    
            
                org.apache.maven.plugins
                maven-surefire-plugin
                ${maven-surefire-plugin.version}
            
        
    
    

    The FlexyPool child Maven module pom.xml using a different Java version

    The flexy-pool-core-java9 child Maven module that requires a different Java version only needs to override the default jdk.version Maven property:

    
        9
    
    

    And that's it, we can now build each module using its own minimum viable Java version.

提交回复
热议问题