4

How can I disable maven-antrun-plugin execution when certain file already exists?:

[...]
<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>
[...]

The execution takes some time and I don't want to repeat it every time when file.txt is already there.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
yegor256
  • 102,010
  • 123
  • 446
  • 597

2 Answers2

7

Check for the presence of the file in your standalone Ant file. Example:

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

<target name="time-consuming" depends="check-file" unless="fileExists">
    ...
</target>
yegor256
  • 102,010
  • 123
  • 446
  • 597
Laurent Pireyn
  • 6,735
  • 1
  • 29
  • 39
  • could you please give a short example here? I tried what is suggested here (http://stackoverflow.com/questions/133710), but failed to make it working inside Maven. – yegor256 Mar 15 '11 at 11:59
  • 2
    @yegor256 Indeed, but I assume you use an external Ant file and your POM calls the `time-consuming` target. – Laurent Pireyn Mar 15 '11 at 12:13
2

Use a <profile> that's only active if file.txt doesn't exist:

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

Reference:

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • There is one problem. When I run `mvn clean test` the file exists during POM initialization, but is destroyed after `clean` phase. Thus, antrun execution is not started, while required... Any thoughts? – yegor256 Mar 15 '11 at 12:38
  • @yegor Sorry, that's how it works. Profiles get evaluated before the build is started and they don't get re-evaluated between goals – Sean Patrick Floyd Mar 15 '11 at 13:01
  • @SeanPatrickFloyd so you can not run the ant task before the actual profile file exists check is done? i am using antrun to create a file and then checks if it exists in profile. however, the profile is evaluated like you said on the next run because the check is done before clean or install is done. – mjs Oct 09 '18 at 15:00
  • @momo I think profiles are evaluated before the build lifecycle is even started, so I doubt you'll have any way to do anything inside a build lifecycle that triggers a specific profile. Maven is just not that type of tool. If you want an imperative build tool, use ant or gradle. Maven is declarative, and trying to work around that usually leads to fear, anger and suffering – Sean Patrick Floyd Oct 09 '18 at 21:03