Manually attach main artifact if built by maven-assembly-plugin

前端 未结 2 1161
野趣味
野趣味 2021-02-15 13:38

I am having problems building a maven project. I have a requirement to produce deterministic jar files, which must be binary-consistent across different builds and vers

2条回答
  •  深忆病人
    2021-02-15 14:15

    After some more trial and failures I happened to come with a working solution. I am posting it here with the hopes to either be useful, or to have any issues with pointed to me, as I am not really confident if this is a reliable approach.

    So, the error I received

    An attached artifact must have a different ID than its corresponding main artifact.

    meant to me that I cannot manually install "again" the main artifact. Since that artifact is not produced anymore by the maven-jar-plugin, it never gets scheduled for installation even if the file is present (the antrun copy task produces a jar with the same name).

    Surprisingly, it needed a few little tricks to make this work again:

    1. Re-enabled the maven-jar-plugin as it should be:

      
        org.apache.maven.plugins
        maven-jar-plugin
        2.5
        
          
            default-jar
            package
          
        
      
      

      This will produce the standard jar on the package phase, and most importantly, makes maven aware for it to be installed during the install phase.

    2. Tweak the maven-antrun-plugin copy tasks to overwrite the already produced jar with the deterministic zip. The setup is nearly identical as in my question, so I am only adding the differences:

      
        maven-antrun-plugin
        1.7
        
          ...
          
            step-3-rename-assembly-and-sources
            package
            
              
                
              
            
          
            . . .
        
      
      

      The copy operation now has overwrite="true" specified. Originally, the copy operation seemed to ignore files in the destination if they already exist, and what happened is that the maven-jar-plugin had already produced the default jar artifact when the copying occured. With this option set, the maven-antrun-plugin now overrides the former jar with the deterministic one, and the latter becomes a subject of the maven install phase.

    3. Removed the setup from the build-helper-maven-plugin, so that the main jar artifact is not copied a second time:

    
                
                  ${project.build.directory}/${project.build.finalName}.jar
                  jar
                
    
    

    That's it, the correct jar is installed in the .m2 dir.

提交回复
热议问题