【问题标题】:How to disable antrun if certain file already exists?如果某些文件已经存在,如何禁用 antrun?
【发布时间】:2011-07-15 17:19:45
【问题描述】:

当某些文件已经存在时,如何禁用maven-antrun-plugin 执行?:

[...]
<plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.6</version>
  <executions>
    <execution>
      <phase>test</phase>
      <goals>
        <goal>run</goal>
      </goals>
      <configuration>
        <target>
          <!-- do something really complex in order to create file.txt -->
        </target>
      </configuration>
    </execution>
  </executions>
</build>
[...]

执行需要一些时间,我不想每次file.txt 已经存在时都重复。

【问题讨论】:

    标签: java maven ant maven-2 maven-antrun-plugin


    【解决方案1】:

    检查您的独立 Ant 文件中是否存在该文件。示例:

    <target name="check-file">
        <available file="foo.bar" property="fileExists" />
    </target>
    
    <target name="time-consuming" depends="check-file" unless="fileExists">
        ...
    </target>
    

    【讨论】:

    • 你能在这里举一个简短的例子吗?我尝试了这里的建议 (stackoverflow.com/questions/133710),但未能使其在 Maven 中工作。
    • @yegor256 确实,但我假设您使用外部 Ant 文件并且您的 POM 调用 time-consuming 目标。
    【解决方案2】:

    使用仅在file.txt 不存在时才有效的&lt;profile&gt;

    <profiles>
        <profile>
            <id>createFile</id>
            <activation><file><missing>file.txt</missing></file></activation>
            <build><plugins>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.6</version>
                    <!-- etc -->
                </plugin>
    
            </plugins></build>
        </profile>
    </profiles>
    

    参考:

    【讨论】:

    • 有一个问题。当我运行mvn clean test 时,该文件在POM 初始化期间存在,但在clean 阶段后被销毁。因此,antrun 执行没有开始,而需要...有什么想法吗?
    • @yegor 对不起,这就是它的工作原理。配置文件会在构建开始之前进行评估,并且不会在目标之间重新评估
    • @SeanPatrickFloyd 所以你不能在实际配置文件存在检查完成之前运行 ant 任务?我正在使用 antrun 创建一个文件,然后检查它是否存在于配置文件中。但是,配置文件会像您在下次运行时所说的那样进行评估,因为检查是在清理或安装完成之前完成的。
    • @momo 我认为配置文件是在构建生命周期开始之前评估的,所以我怀疑您是否有办法在构建生命周期内执行任何触发特定配置文件的事情。 Maven 不是那种工具。如果您想要一个命令式构建工具,请使用 ant 或 gradle。 Maven 是声明性的,试图解决这通常会导致恐惧、愤怒和痛苦
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    • 2013-11-07
    • 1970-01-01
    • 2017-10-08
    • 1970-01-01
    • 2018-04-02
    相关资源
    最近更新 更多