3

问题

总体的目标

我想构建一个 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 开头的所有内容都是静态的和版本化的,因此我们可以设置一个过期标题“访问 + 12 个月”。内容更改需要新的版本号。这对于库来说很容易,因为它们不会改变太多,版本号是硬编码的。

由于我自己的应用程序(使用 AngularJS 构建)也是静态的,因此我也想将其 expires 标头设置为“访问 + 12 个月”。为此,我需要将来自 Maven 的当前版本号添加到文件夹名称的末尾,从而将 /app/myApp 从源目录变为目标目录/最终 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-xyz

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

谢谢

解决方案

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

1 回答 1

4

我认为您可以通过<webResources>在 maven-war-plugin 配置中配置一个元素来做到这一点。

做这样的事情应该有效

   <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 所做的,但我不确定):那么您不需要<webResources>war 插件中的部分,但您必须保留<warSourceExcludes>app/myApp/**</warSourceExcludes>以避免重复。

编辑 2

作为对您评论的回答。

尝试以下操作:

  • 对 *.css 和 *.js 使用 yucompressor,如先前编辑中所述
  • /myapp从通常的战争插件复制资源过程中排除使用<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>
    
于 2013-02-27T15:39:24.087 回答