Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename directory while building war-file with Maven

Tags:

rename

maven

war

Question

Overall Goal

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.

Details

The WebApp is already structured in a way that all static content is grouped together inside folders:

  • /lib -> All JavaScript libraries, eg /lib/angular-1.0.3 (source at src/main/webapp)
  • /app -> My Own Code, at the moment /app/myApp (source at src/main/webapp)
  • /api -> all dynamic servlets (source at src/main/java)
  • index.jsp -> host page that bootstraps the WebApp

Everything starting with /api and index.jsp is dynamic and not cachable at all.

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.

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.

POM File

<plugins>
<plugin>
 <groupId>net.alchim31.maven</groupId>
 <artifactId>yuicompressor-maven-plugin</artifactId>
 <version>1.3.2</version>
 <executions>
   <execution>
     <goals>
       <goal>jslint</goal>
       <goal>compress</goal>
     </goals>
   </execution>
 </executions>

 <configuration>
   <excludeResources>true</excludeResources>
   <removeIncluded>true</removeIncluded>
   <suffix>.min</suffix>
   <excludes>
     <exclude>**/*.xml</exclude>
     <exclude>**/*.properties</exclude>
     <exclude>**/lib/**/*.js</exclude>
     <exclude>**/lib/**/*.css</exclude>
   </excludes>

   <aggregations>
     <aggregation>
       <insertNewLine>true</insertNewLine>
       <output>${project.build.directory}/${project.build.finalName}/app/myApp/js/all.min.js</output>
       <includes>
         <include>${project.basedir}/src/main/webapp/app/myApp/js/header.txt</include>
         <include>**/*.min.js</include>
       </includes>
     </aggregation>
   </aggregations>
 </configuration>
</plugin>
<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-war-plugin</artifactId>
 <version>2.0.1</version>
 <executions>
   <execution>
     <id>prepare-war</id>
     <phase>prepare-package</phase>
     <goals>
       <goal>exploded</goal>
     </goals>
   </execution>
 </executions>
</plugin>

<plugin>
 <groupId>com.google.code.maven-replacer-plugin</groupId>
 <artifactId>replacer</artifactId>
 <version>1.5.2</version>
 <executions>
   <execution>
     <phase>prepare-package</phase>
     <goals>
       <goal>replace</goal>
     </goals>
   </execution>
 </executions>
 <configuration>
   <ignoreMissingFile>false</ignoreMissingFile>
   <file>${project.build.directory}/myApp/index.jsp</file>
   <replacements>
     <replacement>
       <token>PROJECT_VERSION</token>
       <value>${project.version}</value>
     </replacement>
   </replacements>
 </configuration>
</plugin>

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

Compressing and updating the links already, but I can't find a way to rename my own app at build time.

Thanks

Solution

    <plugins>
         <plugin>
           <groupId>net.alchim31.maven</groupId>
           <artifactId>yuicompressor-maven-plugin</artifactId>
           <version>1.3.2</version>
           <executions>
             <execution>
               <goals>
                 <goal>jslint</goal>
                 <goal>compress</goal>
               </goals>
             </execution>
           </executions>
           <configuration>
             <excludeResources>true</excludeResources>
             <removeIncluded>true</removeIncluded>
             <suffix>.min</suffix>
             <failOnWarning>true</failOnWarning>
             <excludes>
               <exclude>**/*.xml</exclude>
               <exclude>**/*.properties</exclude>
               <exclude>**/lib/**/*.js</exclude>
               <exclude>**/lib/**/*.css</exclude>
             </excludes>
             <sourceDirectory>${project.basedir}/src/main/webapp/app/myApp</sourceDirectory>
             <outputDirectory>${project.build.directory}/${project.build.finalName}/app/myApp-${project.version}</outputDirectory>
             <aggregations>
               <aggregation>
                 <insertNewLine>true</insertNewLine>
                 <output>${project.build.directory}/${project.build.finalName}/app/myApp-${project.version}/js/all.min.js</output>
                 <includes>
                   <include>${project.basedir}/src/main/webapp/app/myApp/js/header.txt</include>
                   <include>**/*.min.js</include>
                 </includes>
               </aggregation>
             </aggregations>
           </configuration>
         </plugin>

         <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-war-plugin</artifactId>
           <version>2.3</version>
           <executions>
             <execution>
               <id>prepare-war</id>
               <phase>prepare-package</phase>
               <goals>
                 <goal>exploded</goal>
               </goals>
             </execution>
           </executions>
           <configuration>
             <useCache>true</useCache>
             <!-- this exclude the directory to rename from the default copy resources procedure -->
             <warSourceExcludes>app/myApp/**,.idea/**</warSourceExcludes>
             <!-- this will do the renaming : i.e. we specify the source directory relative to pom.xml and the target directory relative to root -->
             <webResources>
               <resource>
                 <directory>src/main/webapp/app/myApp</directory>
                 <excludes>
                   <exclude>**/*.js</exclude>
                 </excludes>
                 <targetPath>/app/myApp-${project.version}</targetPath>
               </resource>
             </webResources>
           </configuration>
         </plugin>

         <plugin>
           <groupId>com.google.code.maven-replacer-plugin</groupId>
           <artifactId>replacer</artifactId>
           <version>1.5.2</version>
           <executions>
             <execution>
               <phase>prepare-package</phase>
               <goals>
                 <goal>replace</goal>
               </goals>
             </execution>
           </executions>
           <configuration>
             <ignoreMissingFile>false</ignoreMissingFile>
             <file>${project.build.directory}/myApp/index.jsp</file>
             <replacements>
               <replacement>
                 <token>%PROJECT_VERSION%</token>
                 <value>${project.version}</value>
               </replacement>
             </replacements>
           </configuration>
         </plugin>
        </plugins>
like image 469
phisch Avatar asked Dec 10 '25 00:12

phisch


1 Answers

I think you can do it by configuring a <webResources> element in your maven-war-plugin config.

Doing something like this should work

   <configuration>
      <!-- this exclude the directory to rename from the default copy resources procedure -->
      <warSourceExcludes>app/myApp/**</warSourceExcludes>

      <!-- this will do the renaming : 
           i.e. we specify the source directory relative to pom.xml
           and the target directory relative to root -->
      <webResources>
        <resource>
          <directory>src/main/webapp/app/myApp</directory>
          <!-- override the destination directory for this resource -->
          <targetPath>app/myApp-${project.version}</targetPath>
        </resource>
      </webResources>
    </configuration>

EDIT

I'm not used to work with yucompressor plugin but it seems that you can easily change the output directory through <outputDirectory> element:

       <outputDirectory>${project.build.directory}/${project.build.finalName}/app/myApp-${project.version}/js/all.min.js</outputDirectory>

If all your code is in all.min.js (I think that's what the yucompressor do, but I'm not sure) : then you don't need the <webResources> section in the war plugin, but you have to keep <warSourceExcludes>app/myApp/**</warSourceExcludes> to avoid duplication.

EDIT 2

As an answer to your comment.

Try the following:

  • use yucompressor for *.css and *.js as indicated in previous EDIT
  • exclude /myapp from usual war-plugin copy resources procedure using <warSourceExcludes>app/myApp/**</warSourceExcludes>
  • use a webResources section to include all you non *.js and non *.css to myApp-${project.version}:

      <webResources>
        <resource>
          <directory>src/main/webapp/app/myApp</directory>
          <excludes>
            <exclude>**/*.js</exclude>
            <exclude>**/*.css</exclude>
          </excludes>
          <!-- override the destination directory for this resource -->
          <targetPath>app/myApp-${project.version}</targetPath>
        </resource>
      </webResources>
    
like image 65
ben75 Avatar answered Dec 11 '25 13:12

ben75



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!