maven-antrun-plugin 跳过执行
maven-antrun-plugin skips an execution
我将 this Stack Overflow question 中建议的解决方案添加到我的 Maven 项目中。与我介绍的建议解决方案的唯一区别是将 <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>
这个问题与 Ant 的 copy
任务如何工作有关:
By default, files are only copied if the source file is newer than the destination file, or when the destination file does not exist.
这就是问题所在。 Ant 检测到目标文件已经存在,并且不是更新的。有一个粒度可以确定"newer",默认是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" />
我将 this Stack Overflow question 中建议的解决方案添加到我的 Maven 项目中。与我介绍的建议解决方案的唯一区别是将 <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>
这个问题与 Ant 的 copy
任务如何工作有关:
By default, files are only copied if the source file is newer than the destination file, or when the destination file does not exist.
这就是问题所在。 Ant 检测到目标文件已经存在,并且不是更新的。有一个粒度可以确定"newer",默认是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" />