title: Rename directory while building war-file with Maven
body: <h1>Question</h1>
  
  <h2>Overall Goal</h2>
  
  <p>I want to build a Java WebApp (war file) where folders containing static content have a version number append to it for cachability. Renaming should happen automatically during build. </p>
  
  <h2>Details</h2>
  
  <p>The WebApp is already structured in a way that all static content is grouped together inside folders: </p>
  
  <ul>
  <li>/lib -> All JavaScript libraries, eg /lib/angular-1.0.3 (source at src/main/webapp) </li>
  <li>/app -> My Own Code, at the moment /app/myApp (source at src/main/webapp) </li>
  <li>/api -> all dynamic servlets (source at src/main/java) </li>
  <li>index.jsp -> host page that bootstraps the WebApp</li>
  </ul>
  
  <p>Everything starting with /api and index.jsp is dynamic and not cachable at all.</p>
  
  <p>Everything starting with /lib is static and versioned, thus we can set an expires header of 'access + 12 month'. Content changes require a new version number. This is easy with libraries, as they don't change much an the version number is hardcoded.</p>
  
  <p>Since my own app (built with AngularJS) is also static, I want to set its expires header to 'access + 12 month', too. To do so, I need to add the current version number from Maven to the end of the folder name, thus turning /app/myApp from the source directory to /app/myApp-1.2.3 in the target directory / final war file. </p>
  
  <h2>POM File</h2>
  
  <pre><code>&lt;plugins&gt;
  &lt;plugin&gt;
   &lt;groupId&gt;net.alchim31.maven&lt;/groupId&gt;
   &lt;artifactId&gt;yuicompressor-maven-plugin&lt;/artifactId&gt;
   &lt;version&gt;1.3.2&lt;/version&gt;
   &lt;executions&gt;
     &lt;execution&gt;
       &lt;goals&gt;
         &lt;goal&gt;jslint&lt;/goal&gt;
         &lt;goal&gt;compress&lt;/goal&gt;
       &lt;/goals&gt;
     &lt;/execution&gt;
   &lt;/executions&gt;
  
   &lt;configuration&gt;
     &lt;excludeResources&gt;true&lt;/excludeResources&gt;
     &lt;removeIncluded&gt;true&lt;/removeIncluded&gt;
     &lt;suffix&gt;.min&lt;/suffix&gt;
     &lt;excludes&gt;
       &lt;exclude&gt;**/*.xml&lt;/exclude&gt;
       &lt;exclude&gt;**/*.properties&lt;/exclude&gt;
       &lt;exclude&gt;**/lib/**/*.js&lt;/exclude&gt;
       &lt;exclude&gt;**/lib/**/*.css&lt;/exclude&gt;
     &lt;/excludes&gt;
  
     &lt;aggregations&gt;
       &lt;aggregation&gt;
         &lt;insertNewLine&gt;true&lt;/insertNewLine&gt;
         &lt;output&gt;${project.build.directory}/${project.build.finalName}/app/myApp/js/all.min.js&lt;/output&gt;
         &lt;includes&gt;
           &lt;include&gt;${project.basedir}/src/main/webapp/app/myApp/js/header.txt&lt;/include&gt;
           &lt;include&gt;**/*.min.js&lt;/include&gt;
         &lt;/includes&gt;
       &lt;/aggregation&gt;
     &lt;/aggregations&gt;
   &lt;/configuration&gt;
  &lt;/plugin&gt;
  &lt;plugin&gt;
   &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
   &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
   &lt;version&gt;2.0.1&lt;/version&gt;
   &lt;executions&gt;
     &lt;execution&gt;
       &lt;id&gt;prepare-war&lt;/id&gt;
       &lt;phase&gt;prepare-package&lt;/phase&gt;
       &lt;goals&gt;
         &lt;goal&gt;exploded&lt;/goal&gt;
       &lt;/goals&gt;
     &lt;/execution&gt;
   &lt;/executions&gt;
  &lt;/plugin&gt;
  
  &lt;plugin&gt;
   &lt;groupId&gt;com.google.code.maven-replacer-plugin&lt;/groupId&gt;
   &lt;artifactId&gt;replacer&lt;/artifactId&gt;
   &lt;version&gt;1.5.2&lt;/version&gt;
   &lt;executions&gt;
     &lt;execution&gt;
       &lt;phase&gt;prepare-package&lt;/phase&gt;
       &lt;goals&gt;
         &lt;goal&gt;replace&lt;/goal&gt;
       &lt;/goals&gt;
     &lt;/execution&gt;
   &lt;/executions&gt;
   &lt;configuration&gt;
     &lt;ignoreMissingFile&gt;false&lt;/ignoreMissingFile&gt;
     &lt;file&gt;${project.build.directory}/myApp/index.jsp&lt;/file&gt;
     &lt;replacements&gt;
       &lt;replacement&gt;
         &lt;token&gt;PROJECT_VERSION&lt;/token&gt;
         &lt;value&gt;${project.version}&lt;/value&gt;
       &lt;/replacement&gt;
     &lt;/replacements&gt;
   &lt;/configuration&gt;
  &lt;/plugin&gt;
  </code></pre>
  
  <p>I am looking for a way to copy all ressources to the target-directory, run yuicompressor, update the links and rename app/myApp to app/myApp-x.y.z</p>
  
  <p>Compressing and updating the links already, but I can't find a way to rename my own app at build time. </p>
  
  <p>Thanks</p>
  
  <h1>Solution</h1>
  
  <pre><code>    &lt;plugins&gt;
           &lt;plugin&gt;
             &lt;groupId&gt;net.alchim31.maven&lt;/groupId&gt;
             &lt;artifactId&gt;yuicompressor-maven-plugin&lt;/artifactId&gt;
             &lt;version&gt;1.3.2&lt;/version&gt;
             &lt;executions&gt;
               &lt;execution&gt;
                 &lt;goals&gt;
                   &lt;goal&gt;jslint&lt;/goal&gt;
                   &lt;goal&gt;compress&lt;/goal&gt;
                 &lt;/goals&gt;
               &lt;/execution&gt;
             &lt;/executions&gt;
             &lt;configuration&gt;
               &lt;excludeResources&gt;true&lt;/excludeResources&gt;
               &lt;removeIncluded&gt;true&lt;/removeIncluded&gt;
               &lt;suffix&gt;.min&lt;/suffix&gt;
               &lt;failOnWarning&gt;true&lt;/failOnWarning&gt;
               &lt;excludes&gt;
                 &lt;exclude&gt;**/*.xml&lt;/exclude&gt;
                 &lt;exclude&gt;**/*.properties&lt;/exclude&gt;
                 &lt;exclude&gt;**/lib/**/*.js&lt;/exclude&gt;
                 &lt;exclude&gt;**/lib/**/*.css&lt;/exclude&gt;
               &lt;/excludes&gt;
               &lt;sourceDirectory&gt;${project.basedir}/src/main/webapp/app/myApp&lt;/sourceDirectory&gt;
               &lt;outputDirectory&gt;${project.build.directory}/${project.build.finalName}/app/myApp-${project.version}&lt;/outputDirectory&gt;
               &lt;aggregations&gt;
                 &lt;aggregation&gt;
                   &lt;insertNewLine&gt;true&lt;/insertNewLine&gt;
                   &lt;output&gt;${project.build.directory}/${project.build.finalName}/app/myApp-${project.version}/js/all.min.js&lt;/output&gt;
                   &lt;includes&gt;
                     &lt;include&gt;${project.basedir}/src/main/webapp/app/myApp/js/header.txt&lt;/include&gt;
                     &lt;include&gt;**/*.min.js&lt;/include&gt;
                   &lt;/includes&gt;
                 &lt;/aggregation&gt;
               &lt;/aggregations&gt;
             &lt;/configuration&gt;
           &lt;/plugin&gt;
  
           &lt;plugin&gt;
             &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
             &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
             &lt;version&gt;2.3&lt;/version&gt;
             &lt;executions&gt;
               &lt;execution&gt;
                 &lt;id&gt;prepare-war&lt;/id&gt;
                 &lt;phase&gt;prepare-package&lt;/phase&gt;
                 &lt;goals&gt;
                   &lt;goal&gt;exploded&lt;/goal&gt;
                 &lt;/goals&gt;
               &lt;/execution&gt;
             &lt;/executions&gt;
             &lt;configuration&gt;
               &lt;useCache&gt;true&lt;/useCache&gt;
               &lt;!-- this exclude the directory to rename from the default copy resources procedure --&gt;
               &lt;warSourceExcludes&gt;app/myApp/**,.idea/**&lt;/warSourceExcludes&gt;
               &lt;!-- this will do the renaming : i.e. we specify the source directory relative to pom.xml and the target directory relative to root --&gt;
               &lt;webResources&gt;
                 &lt;resource&gt;
                   &lt;directory&gt;src/main/webapp/app/myApp&lt;/directory&gt;
                   &lt;excludes&gt;
                     &lt;exclude&gt;**/*.js&lt;/exclude&gt;
                   &lt;/excludes&gt;
                   &lt;targetPath&gt;/app/myApp-${project.version}&lt;/targetPath&gt;
                 &lt;/resource&gt;
               &lt;/webResources&gt;
             &lt;/configuration&gt;
           &lt;/plugin&gt;
  
           &lt;plugin&gt;
             &lt;groupId&gt;com.google.code.maven-replacer-plugin&lt;/groupId&gt;
             &lt;artifactId&gt;replacer&lt;/artifactId&gt;
             &lt;version&gt;1.5.2&lt;/version&gt;
             &lt;executions&gt;
               &lt;execution&gt;
                 &lt;phase&gt;prepare-package&lt;/phase&gt;
                 &lt;goals&gt;
                   &lt;goal&gt;replace&lt;/goal&gt;
                 &lt;/goals&gt;
               &lt;/execution&gt;
             &lt;/executions&gt;
             &lt;configuration&gt;
               &lt;ignoreMissingFile&gt;false&lt;/ignoreMissingFile&gt;
               &lt;file&gt;${project.build.directory}/myApp/index.jsp&lt;/file&gt;
               &lt;replacements&gt;
                 &lt;replacement&gt;
                   &lt;token&gt;%PROJECT_VERSION%&lt;/token&gt;
                   &lt;value&gt;${project.version}&lt;/value&gt;
                 &lt;/replacement&gt;
               &lt;/replacements&gt;
             &lt;/configuration&gt;
           &lt;/plugin&gt;
          &lt;/plugins&gt;
  </code></pre>