【问题标题】:Remove or delete resource files from target directory using pom file使用 pom 文件从目标目录中删除或删除资源文件
【发布时间】:2013-09-09 08:45:00
【问题描述】:

我在 pom.xml 中有两个配置文件,并且我有一些资源文件已添加到目标资源目录中:${project.build.outputDirectory}/resources 在执行第一个配置文件期间。我需要做的是在执行第二个配置文件期间删除这些资源文件。 有没有办法从目标目录中删除或删除现有文件?

【问题讨论】:

    标签: maven pom.xml


    【解决方案1】:

    我得到了解决方案..!!

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <executions>
        <execution>
            <phase>test</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <tasks>
                    <delete>
                        <fileset dir="${project.build.outputDirectory}/resources" includes="*.xml" />
                    </delete>
                </tasks>
            </configuration>
        </execution>
    </executions>
    </plugin>
    

    供参考 - http://maven.apache.org/guides/mini/guide-building-for-different-environments.html

    【讨论】:

    • 通常,在 Maven 中,您更喜欢声明式解决方案,而不是仅仅编写代码(Ant、Groovy 等插件)让您这样做。
    • 这篇文章是完全正确的,但应该更新以使用目标,因为任务已被弃用。
    • 是的,您可以将tasks 替换为target,这样就可以了。如果文件存在,我试图删除,最后我使用了&lt;delete failonerror="false"&gt;
    【解决方案2】:

    我确实同意 Matthew 的观察,但我的印象是原始发帖人在询问如何在(正常)“执行”配置文件期间自动执行 clean

    您可以为 Maven Clean 插件定义 plugin execution。它通常只绑定到clean,但通过定义插件执行,您可以将clean:clean(即clean 插件的clean 目标)绑定到您想要的任何lifecycle phase。 Maven Clean Plugin 的文档有an example 说明如何执行此操作。该文档还具有删除附加文件的an example。合并两者看起来像这样:

      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>auto-clean</id>
            <phase>initialize</phase>
            <goals>
              <goal>clean</goal>
            </goals>
            <configuration>
             <filesets>
                <fileset>
                  <directory>some/relative/path</directory>
                </fileset>
              </filesets>
            </configuration>
          </execution>
        </executions>
      </plugin>
    

    【讨论】:

    • 我发现你需要在配置中使用&lt;excludeDefaultDirectories&gt;true&lt;/excludeDefaultDirectories&gt;,否则整个target目录也会被删除。
    • 好吧,当然。但无论如何,target 文件夹是您希望看到在 mvn clean 上被删除的东西。
    • 这不是关于执行mvn clean,而是关于在执行任意阶段时删除特定文件/文件夹,例如mvn package - 您可能不想清理整个项目并拥有重新编译。
    • 很公平,我必须回去更好地阅读自己的答案才能得到答案:)
    • 我认为这个答案比公认的更好,因为它是一个通用的解决方案,并且对通用问题的响应也很好,而不是maven-antrun-plugin 的解决方案。 @SanderVerhagen 您可以按照 daiscog 的建议添加 excludeDefaultDirectories 吗?
    【解决方案3】:

    mvn clean 将删除target 目录(以及其中的所有文件)。如果您只想从target 目录中删除某些文件,可以使用以下组合:

    • excludeDefaultDirectories 阻止它删除整个目录,并且

    • filesets 告诉它要删除什么

    参考:http://maven.apache.org/plugins/maven-clean-plugin/clean-mojo.html

    【讨论】:

      【解决方案4】:

      Apache Maven AntRun Plugin 1.8 的解决方案:

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.8</version>
        <executions>
          <execution>
            <phase>initialize</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <target>
                <delete 
                  dir="${project.build.outputDirectory}/resources"
                  includeemptydirs="true"/>
              </target>
            </configuration>
          </execution>
        </executions>
      </plugin>
      

      【讨论】:

      • 这也适用于我删除多个文件。对于多个文件,我使用
      【解决方案5】:

      我只需要从输出目录中删除几个文件,以下对我来说很好。

      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.8</version>
          <executions>
              <execution>
                  <phase>compile</phase>
                  <goals>
                      <goal>run</goal>
                  </goals>
                  <configuration>
                      <tasks>
                          <delete file="${project.build.outputDirectory}/appContextLocal.xml" />
                          <delete
                              file="${project.build.outputDirectory}/appContextServer.xml" />
                      </tasks>
                  </configuration>
              </execution>
          </executions>
      </plugin>
      

      我还认为您可以在这里运行任何 ant 命令来替换 &lt;tasks&gt; .... &lt;/tasks&gt; 之间您需要的任何任务,它会起作用。

      你可以执行的蚂蚁任务列表是here

      参考:http://maven.apache.org/plugins/maven-antrun-plugin/usage.html

      【讨论】:

        【解决方案6】:

        感谢以上答案。最后我想到了类似的东西:

        如果你想删除 一些目标文件夹中的目录,你必须创建一些这样的结构。
        例如,这会删除文件夹的所有内容:

        • 目标/解包
        • gen-external-apklibs

        excludeDefaultDirectories 允许不删除完整的目标文件夹
        我在 lint 分析之前用它来清理目标文件夹。

               <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>2.6</version>
                    <executions>
                        <execution>
                            <id>Deleting all unnecessary files before lint analysis</id>
                            <phase>verify</phase>
                            <goals>
                                <goal>clean</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <excludeDefaultDirectories>true</excludeDefaultDirectories>
                        <filesets>
                            <fileset>
                                <directory>target/unpack</directory>
                                <followSymlinks>false</followSymlinks>
                                <excludes>
                                    <exclude>*</exclude>
                                </excludes>
                            </fileset>
                            <fileset>
                                <directory>gen-external-apklibs</directory>
                                <followSymlinks>false</followSymlinks>
                                <excludes>
                                    <exclude>*</exclude>
                                </excludes>
                            </fileset>
                        </filesets>
                        <verbose>true</verbose>
                    </configuration>
                </plugin>
        

        【讨论】:

        • 这里不应该&lt;excludes/&gt;&lt;includes/&gt; 吗? (当然还有&lt;include/&gt; 而不是&lt;exclude/&gt;。)
        猜你喜欢
        • 2015-06-20
        • 1970-01-01
        • 2023-04-03
        • 2014-04-09
        • 1970-01-01
        • 2023-03-11
        • 2015-12-26
        • 2016-12-27
        • 1970-01-01
        相关资源
        最近更新 更多