2017-03-05 163 views
2

我在我的Maven项目中添加了解决方案this Stack Overflow question。与我介绍的建议解决方案唯一的区别是用<target />替换<tasks />(我遇到的问题随任何一个出现)。maven-antrun-plugin跳过执行

在测试方面,一切都非常出色。当我运行我的测试时,正在使用正确的(test-persistence.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 

您会注意到0任务在restore-persistence下执行。奇怪的是在创建文件夹/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> 

回答

2

这个问题已经得到了与如何Ant的copy任务的工作要做:

默认情况下,文件仅当源文件更新复制比目标文件还要多,或者目标文件不存在时。

这是这里的问题。 Ant检测到目标文件已经存在,并且它不新。有一个确定“更新”的粒度,默认情况下,它是DOS系统上的1秒或2秒。所以会发生的是,在构建期间,Maven将persistence.xml复制到构建目录中,其最后修改日期发生更改(资源插件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" /> 
+0

辉煌 - 这解决了我的问题。非常感谢你! – vasigorc