Subtle differences in declared order of Maven plug-ins

Source: Internet
Author: User

Subtle differences in declared order of Maven plug-ins
Look at this very interesting question:

A project needs to perform the package operation as follows:

Maven-jar-plugin is the first to obtain the jar package of this project. The reason why the jar plug-in is explicitly configured is that you need to exclude unnecessary files and then use maven-shade-plugin, compress the jar package of the project and the jar package it depends on into a big jar package of all-in-one. This is not an elegant way to handle it, but you may have to make the final choice if you are limited to the special needs of some environments, use maven-assembly-plugin to organize the final all-in-one jar package, some shell files, and configuration files (such as bin, lib, conf, and other folders) as follows) package into a distribution package

Someone may be afraid of using the jar, shade, and assembly package plug-ins in the project. In fact, from my practical experience, these three plug-ins have their respective focuses and are good at problem domains. There is no problem of who replaces them. Instead, they need to be combined based on project needs. Jar is a narrow jar file packaging tool. The final delivery is a single jar package containing the project classes, shade focuses on combining dependent jar packages to generate an all-in-one jar package, while assembly has the ability to merge dependent jar packages, however, it is not as powerful as shade in this aspect. Its main strength is to build a distribution package with a specific directory structure and resource files.

The initial configuration is as follows:


      
   
    org.apache.maven.plugins
       maven-jar-plugin    
   
    ${jar.version}
       
           
                
     
      bin/**
                 
     
      deploy.bat
             
        
   
  
      
   
    org.apache.maven.plugins
       maven-assembly-plugin    
   
    ${assembly.version}
       
   
    false
       
           
                
     
      package
                 
                     
      
       single
                  
             
        
       
           
                
     
      src/main/assembly/bin.xml
             
        
   
  
      
   
    org.apache.maven.plugins
       maven-shade-plugin    
   
    ${shade.version}
       
           
                
     
      package
                 
                     
      
       shade
                  
             
        
   
  

In the generated target directory, obtain several important files similar to the following:

Xxx. jar (about 15 M)
Original-xxx.jar (about 10 K)
Xxx-bin.zip ~ 10 K (about 10 K)

Explain the origins of these three files:
The original-xxx.jar was originally a jar package generated by the jar plug-in that only contains the project's own classes, so it is very small, it was created at the beginning of the name is xxx. jar, which is later renamed by the shade plug-in during work. jar and its dependent jar are packaged together. In order to avoid duplicate names, the original xxx. jar changed to original-xxx.jar, new xxx. jar is a collection of all dependent jars, so it is very large. Xxx-bin.zip is a final release package that contains xxx. jar.

However, this configuration has a major bug:

We can see that the xxx-bin.zip file is very small and contains xxx. jar is not a big jar package of all-in-one after shade processing, but the original small xxx generated in the jar package phase. jar.Although the above three plug-ins are bound to the phase package without exception, there is a "first come and then" order between the three plug-ins.This bug implies that, according to the above configuration, the jar plug-in that maven was the first to execute, and then executed the assembly, this is why the zip package contains the original xxx generated by the jar plug-in. instead of the all-in-one jar package generated by the shadee plug-in, the shade plug-in is executed at the end, therefore, it is the final generated all-in-one big jar package, which misses the zip package process. So,Maven executes multiple plug-ins bound to the same phase in the order they are declared in pom. xml..

So the solution for modifying this bug is clear. We only need to adjust the Declaration Order of the three plug-ins,Modify the previous maven-jar-plugin-> maven-assembly-plugin-> maven-shade-plugin to maven-jar-plugin-> maven-shade-plugin-> maven- assembly-plugin.

The modified version is as follows:


      
   
    org.apache.maven.plugins
       maven-jar-plugin    
   
    ${jar.version}
       
           
                
     
      bin/**
                 
     
      deploy.bat
             
        
   
  
      
   
    org.apache.maven.plugins
       maven-shade-plugin    
   
    ${shade.version}
       
           
                
     
      package
                 
                     
      
       shade
                  
             
        
   
  
      
   
    org.apache.maven.plugins
       maven-assembly-plugin    
   
    ${assembly.version}
       
   
    false
       
           
                
     
      package
                 
                     
      
       single
                  
             
        
       
           
                
     
      src/main/assembly/bin.xml
             
        
   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.