【问题标题】:maven-antrun-plugin skips an executionmaven-antrun-plugin 跳过执行
【发布时间】:2017-03-05 20:57:45
【问题描述】:

我在我的 Maven 项目中添加了 this Stack Overflow question 中建议的解决方案。与我介绍的建议解决方案的唯一区别是将<tasks /> 替换为<target />(我遇到的问题出现在两者中)。

在测试方面一切都很好。当我运行测试时,正在使用正确的 (test-persistence.xml) 持久性文件。但是,当我从我的 IDE(Netbeans 8.2)进行全新安装甚至点击运行时,只有第一个目标(copy-test-persistence)正在执行。在测试之后进入第二次执行(请参阅下面的构建输出),但未执行目标。在每个clean install 之后以及在服务器上运行应用程序时,我得到的是test-persistence.xml 的内容在persistence.xml 文件中。正确的内容保留在第一个目标中创建的persistence.xml.proper 中。

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

您会注意到restore-persistence 下执行了0 个任务。奇怪的是,在创建的 /target/antrun 文件夹中有一个 build-main.xml 文件,其中包含跳过的任务:

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

如果您能给我一个提示,我们将不胜感激,因为我无法理解这一点。因为这很常见,我发布了我当前的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>

【问题讨论】:

    标签: java maven pom.xml persistence.xml maven-antrun-plugin


    【解决方案1】:

    问题与 Ant 的 copy 任务的工作方式有关:

    默认情况下,仅当源文件比目标文件新或目标文件不存在时才会复制文件。

    这就是问题所在。 Ant 检测到目标文件已经存在,并且它不是新的。确定“较新”有一个粒度,默认为 1 秒,在 DOS 系统上为 2 秒。所以发生的情况是,在构建期间,persistence.xml 被 Maven 复制到构建目录中,它的最后修改日期被更改(资源插件 doesn't keep it),然后你自己的副本只是几毫秒后。因此,复制的persistence.xml.proper 永远不会更新,因为这一切都发生在默认粒度期间。

    您可以通过将overwrite 参数设置为true 来强制复制

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

    或者您可以改用move 任务,因为您可能不需要保留.proper 文件:

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

    【讨论】:

    • 这是一个误导性的答案。请忽略那个人。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多