我在我的Maven项目中添加了this Stack Overflow question中建议的解决方案。我介绍的建议解决方案的唯一不同之处是用<target />替换<target />(我正在经历的问题出现在这两个问题中)。
在测试方面,一切都运行得很好。当我运行测试时,将使用正确的持久性文件(test-sistence.xml)。然而,当我做干净安装,甚至点击我的IDE (Netbeans 8.2)运行时,只执行第一个目标(复制测试-持久性)。第二次执行是在测试之后输入的(请参阅下面的构建输出),但并不执行目标。在每个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>发布于 2017-03-05 21:21:28
这个问题与Ant的copy任务如何工作有关:
默认情况下,只有在源文件比目标文件更新或目标文件不存在时才复制文件。
这就是问题所在。Ant检测到目标文件已经存在,并且它不是更新的。有一个粒度来确定“更新”,默认情况下,在DOS系统上是1秒或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" />https://stackoverflow.com/questions/42613908
复制相似问题