2017-03-05 165 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