Maven String Replace of Text Web Resources

后端 未结 5 1111
萌比男神i
萌比男神i 2021-02-07 07:38

I have a Maven web application with text files in

src/main/webapp/textfilesdir

As I understand it, during the package phase this te

5条回答
  •  遥遥无期
    2021-02-07 08:24

    I had the same problem and I was fiddling a lot with this issue, so I will answer although this question is pretty old. As stated by leandro and Phil, one can use the maven-replacer-plugin. But their solution didn't work for me. Enabling useCache caused an error which made it impossible to build the project. Additionally, I can't make the auto-clean thing to work properly. As I am not allowed yet to comment on the post, I will provide my full solution here:

    First of all, configure the maven-replacer-plugin:

     
      com.google.code.maven-replacer-plugin
      maven-replacer-plugin
      1.3.7
      
        
          prepare-package
          
            replace
          
        
      
      
        
          target/${project.build.finalName}/static/**/*.css
        
        false
        someString
        replaceString
      
    
    

    Before the actual war is built, we create an exploded war. This means, the whole content of the war file is stored in a sub directory (which is target/${project.build.finalName} by default). After that, the maven-replacer-plugin will change the contents of the files as we have specified. Finally, the .war will be packed in the package phase by the default-war job. To avoid the content of the exploded war folder being overridden, one has to set warSourceDirectory to the directory where the exploded war stuff is stored. The following configuration snippet will do this job:

    
      maven-war-plugin
      2.1.1
      
        
          prepare
          prepare-package
          
            exploded
      
        
         
          default-war
          package
          
            war
          
          
            ${project.build.directory}/${project.build.finalName}
          
        
      
    
    

    The package with the replaced content can be built using mvn clean package

提交回复
热议问题