Build multiple artifacts with different classifiers at once

前端 未结 2 808
Happy的楠姐
Happy的楠姐 2020-12-05 19:28

W want my maven project to produce three artifacts with different classifiers at once. I know that I can produce it with modules etc. This is actually a resources project th

2条回答
  •  盖世英雄少女心
    2020-12-05 20:23

    This can be done without profiles if you specify multiple plugin executions and resource filtering.

    Create a properties file for each version in ${basedir}/src/main/filters (e.g. prod.properties, dev.properties) holding appropriate values for each environment.

    Turn on filtering for your resources:

    
      
        src/main/resources
        true
      
    
    

    Now add the resource plugin executions. Note the different filter file and output directory.

    
      org.apache.maven.plugins
      maven-resources-plugin
      
        
          default-resources
          process-resources
          
            resources
          
          
            ${project.build.outputDirectory}/dev
            
              ${basedir}/src/main/filters/dev.properties
            
          
        
        
          prod
          process-resources
          
            resources
          
          
            ${project.build.outputDirectory}/prod
            
              ${basedir}/src/main/filters/prod.properties
            
          
        
      
    
    

    Finally, the jar plugin; note classifier and input directory:

    
      org.apache.maven.plugins
      maven-jar-plugin
      
        
          default-jar
          package
          
            jar
          
          
            dev
            ${project.build.outputDirectory}/dev
          
        
        
          jar-prod
          package
          
            jar
          
          
            prod
            ${project.build.outputDirectory}/prod
          
        
      
    
    

    Running mvn clean install should produce the properly filtered resources in artifacts with dev and prod classifiers like you want.

    In the example, I used execution IDs of default-resources and default-jar for the dev versions. Without this you would also get an unclassified jar artifact when you build.

提交回复
热议问题