使用Maven构建war-file时重命名目录

时间:2013-02-22 15:27:24

标签: maven war rename

问题

总体目标

我想构建一个Java WebApp(war文件),其中包含静态内容的文件夹附加了一个版本号以实现可缓存性。重命名应该在构建期间自动发生。

详细

WebApp的结构已经将所有静态内容组合在文件夹中:

  • / lib - >所有JavaScript库,例如/lib/angular-1.0.3(源于src / main / webapp)
  • / app - >我自己的代码,目前是/ app / myApp(源于src / main / webapp)
  • / api - >所有动态servlet(来自src / main / java)
  • index.jsp - >引导WebApp的主页

以/ api和index.jsp开头的所有内容都是动态的,根本无法缓存。

以/ lib开头的所有东西都是静态的和版本化的,因此我们可以设置'access + 12 month'的过期标题。内容更改需要新版本号。对于库来说这很容易,因为它们没有太大变化,版本号是硬编码的。

由于我自己的应用程序(使用AngularJS构建)也是静态的,我想将其expires标题设置为“access + 12 month”。为此,我需要将当前版本号从Maven添加到文件夹名称的末尾,从而将/ app / myApp从源目录转到目标目录/ final war文件中的/app/myApp-1.2.3。

POM文件

<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>

我正在寻找一种方法将所有资源复制到目标目录,运行yuicompressor,更新链接并将app / myApp重命名为app / myApp-x.y.z

已经压缩和更新链接,但我找不到在构建时重命名我自己的应用程序的方法。

由于

解决方案

    <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>

1 个答案:

答案 0 :(得分:3)

我认为您可以通过在maven-war-plugin配置中配置<webResources>元素来实现。

做这样的事情应该有效

   <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>

编辑

我不习惯使用yucompressor插件,但似乎可以通过<outputDirectory>元素轻松更改输出目录:

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

如果你的所有代码都在all.min.js中(我认为这就是yucompressor所做的,但我不确定):那么你不需要war插件中的<webResources>部分,但是您必须保留<warSourceExcludes>app/myApp/**</warSourceExcludes>以避免重复。

编辑2

作为对你评论的回答。

尝试以下方法:

  • 使用yucompressor获取* .css和* .js,如前一个EDIT中所示
  • 使用/myapp
  • 从通常的war-plugin复制资源过程中排除<warSourceExcludes>app/myApp/**</warSourceExcludes>
  • 使用webResources部分将所有非* .js和非* .css包含在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>