vasigorc
vasigorc

Reputation: 962

maven-antrun-plugin skips an execution

I added to my Maven project the solution advised in this Stack Overflow question. The only difference to the suggested solution that I introduced was to replace the <tasks /> with the <target /> (the issue I am experiencing appears with either).

Everything works excellent on the testing side. When I run my tests the correct (test-persistence.xml) persistence file is being used. However when I am doing clean install or even hit run from my IDE (Netbeans 8.2) only the first target (copy-test-persistence) is being executed. The second execution is being entered after the tests (see the build output below), but target is not executed. What I do get after every clean install and when running the app on the server is that the contents of the test-persistence.xml are in the persistence.xml file. The right content remains in the persistence.xml.proper created in the first target.

--- maven-antrun-plugin:1.8:run (copy-test-persistence) @ RimmaNew ---
Executing tasks

main:
     [copy] Copying 1 file to /my-project-home/target/classes/META-INF
     [copy] Copying 1 file to /my-project-home/target/classes/META-INF
Executed tasks

...

--- maven-antrun-plugin:1.8:run (restore-persistence) @ RimmaNew ---
Executing tasks

main:
Executed tasks

You will notice that 0 tasks are executed under restore-persistence. Strangely enough in the created /target/antrun folder there's a build-main.xml file which includes the skipped task:

<?xml version="1.0" encoding="UTF-8" ?>
<project name="maven-antrun-" default="main"  >
<target name="main">
  <copy file="/home/vgorcinschi/NetBeansProjects/rimmanew/target/classes/META-INF/persistence.xml.proper" tofile="/home/vgorcinschi/NetBeansProjects/rimmanew/target/classes/META-INF/persistence.xml"/>
</target>
</project>

It would be appreciated if you could give me a hint as I can't get my head around this. As it is common I am posting my current pom.xml:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <id>copy-test-persistence</id>
            <phase>process-test-resources</phase>
            <configuration>
                <target>
                    <!--backup the "proper" persistence.xml-->
                    <copy file="${project.build.outputDirectory}/META-INF/persistence.xml" tofile="${project.build.outputDirectory}/META-INF/persistence.xml.proper" />
                    <!--replace the "proper" persistence.xml with the "test" version-->
                    <copy file="${project.build.testOutputDirectory}/META-INF/test-persistence.xml" tofile="${project.build.outputDirectory}/META-INF/persistence.xml" />
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
        <execution>
            <id>restore-persistence</id>
            <phase>prepare-package</phase>
            <configuration>
                <target>
                    <!--restore the "proper" persistence.xml-->
                    <copy file="${project.build.outputDirectory}/META-INF/persistence.xml.proper" tofile="${project.build.outputDirectory}/META-INF/persistence.xml" />
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Upvotes: 2

Views: 1297

Answers (1)

Tunaki
Tunaki

Reputation: 137084

The issue has got to do with how Ant's copy task work:

By default, files are only copied if the source file is newer than the destination file, or when the destination file does not exist.

This is the problem here. Ant detects that the target file already exists, and that it is not newer. There is a granularity to determine "newer", and by default, it is 1 second, or 2 seconds on DOS systems. So what happens is that, during the build, the persistence.xml is copied by Maven into the build directory, its last modified date is changed (the Resources Plugin doesn't keep it), and then your own copy is just few milliseconds later. Thus, the copied persistence.xml.proper will never be newer because this all happens during the default granularity.

You can force the copy by setting the overwrite parameter to true with

<copy file="${project.build.outputDirectory}/META-INF/persistence.xml.proper"
      tofile="${project.build.outputDirectory}/META-INF/persistence.xml"
      overwrite="true"/>

Or you could use the move task instead, since you probably don't need to keep the .proper file anyway:

<move file="${project.build.outputDirectory}/META-INF/persistence.xml.proper"
      tofile="${project.build.outputDirectory}/META-INF/persistence.xml" />

Upvotes: 2

Related Questions