【问题标题】:Rename directory while building war-file with Maven使用 Maven 构建战争文件时重命名目录
【发布时间】:2023-03-14 00:21:01
【问题描述】:

问题

总体目标

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

详情

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

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

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

以 /lib 开头的所有内容都是静态的和版本化的,因此我们可以设置一个 expires 标头“访问 + 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-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>

【问题讨论】:

    标签: maven war rename


    【解决方案1】:

    我认为您可以通过在 maven-war-plugin 配置中配置 &lt;webResources&gt; 元素来做到这一点。

    这样做应该可以工作

       <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 插件,但您似乎可以通过&lt;outputDirectory&gt; 元素轻松更改输出目录:

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

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

    编辑 2

    作为对您评论的回答。

    尝试以下方法:

    • 对 *.css 和 *.js 使用 yucompressor,如之前的编辑中所述
    • 使用&lt;warSourceExcludes&gt;app/myApp/**&lt;/warSourceExcludes&gt;从通常的战争插件复制资源过程中排除/myapp
    • 使用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>
      

    【讨论】:

    • 谢谢@ben75。非常感谢您的帮助。我考虑过这种方法,但它只适用于我的 js 和 css 文件。我的 AngularJS 项目还包含部分 (html) 和精灵 (png),它们也是静态的,如果我使用 yuicompressors 输出元素,它们会被抛在后面。
    • 谢谢。我会试一试,但目前我不明白 EDIT 1 和 2 之间的巨大区别。使用上面引用的 webRessources 不会生成 targetPath 指定的版本。我需要先看看它是这样的,否则复制所有内容和只复制部分内容没有区别。
    • “使用上面引用的 webRessources 不会生成由 targetPath 指定的版本”……奇怪的是我自己尝试了一个示例项目,它在战争的根源上生成了 app/MyApp-1.0.0-SNAPSHOT。此目录包含我的/src/main/webapp/app/myApp中的所有非 js 和非 css 文件
    • 我想我看到了一个潜在的问题:maven-war-plugin 版本!你能更新到2.3吗? (版本 2.2 和 2.3 有重要改进)
    • 我没有检查版本,谢谢!这可能是原因之一。我也在使用“爆炸”目标,这是 yuicompressor 网站所要求的(并从该网站复制和粘贴)。让我检查一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多