2013-09-05 26 views
19

我在pom.xml的兩個配置文件,和我有我已經加入到目標資源目錄中的一些資源文件從目標目錄中刪除或刪除資源文件。我需要做的是在執行第二個配置文件時刪除這些資源文件。 有什麼辦法從目標目錄中刪除或刪除現有的文件?採用POM文件

回答

21

我與馬修的觀測中同意ns,但我得到的印象是原始海報問如何在(正常)「執行」配置文件期間自動執行clean

可以爲Maven的清潔插件定義plugin execution。它通常只勢必clean,而是通過定義一個插件運行可以綁定clean:clean(也就是clean目標clean插件),取其lifecycle phase你想要的。 Maven Clean Plugin的文檔有如何做到這一點的an example。該文檔還有an example刪除其他文件。合併兩個看起來像這樣:

<plugin> 
    <artifactId>maven-clean-plugin</artifactId> 
    <version>2.5</version> 
    <executions> 
     <execution> 
     <id>auto-clean</id> 
     <phase>initialize</phase> 
     <goals> 
      <goal>clean</goal> 
     </goals> 
     <configuration> 
     <filesets> 
      <fileset> 
       <directory>some/relative/path</directory> 
      </fileset> 
      </filesets> 
     </configuration> 
     </execution> 
    </executions> 
    </plugin> 
22

我得到了解決方案..!

<plugin> 
<groupId>org.apache.maven.plugins</groupId> 
<artifactId>maven-antrun-plugin</artifactId> 
<version>1.7</version> 
<executions> 
    <execution> 
     <phase>test</phase> 
     <goals> 
      <goal>run</goal> 
     </goals> 
     <configuration> 
      <tasks> 
       <delete> 
        <fileset dir="${project.build.outputDirectory}/resources" includes="*.xml" /> 
       </delete> 
      </tasks> 
     </configuration> 
    </execution> 
</executions> 
</plugin> 

參考 - http://maven.apache.org/guides/mini/guide-building-for-different-environments.html

+0

一般來說,在Maven的時候,你會更喜歡聲明的解決方案,在剛剛編碼它(該插件螞蟻一樣的,Groovy:您可以執行的是here

參考Ant任務

名單等)讓你做。 –

2

由於上述答案。最後我來到這樣的:如果你要到j 烏斯刪除目標文件夾中的一些目錄

,你必須創建一些結構是這樣的。
這個例如只刪除文件夾中的所有內容:

  • 目標/解壓
  • GEN-外部apklibs

excludeDefaultDirectories允許到n OT刪除完整的目標文件夾
我用它來清理lint分析前的目標文件夾。

 <plugin> 
      <artifactId>maven-clean-plugin</artifactId> 
      <version>2.6</version> 
      <executions> 
       <execution> 
        <id>Deleting all unnecessary files before lint analysis</id> 
        <phase>verify</phase> 
        <goals> 
         <goal>clean</goal> 
        </goals> 
       </execution> 
      </executions> 
      <configuration> 
       <excludeDefaultDirectories>true</excludeDefaultDirectories> 
       <filesets> 
        <fileset> 
         <directory>target/unpack</directory> 
         <followSymlinks>false</followSymlinks> 
         <excludes> 
          <exclude>*</exclude> 
         </excludes> 
        </fileset> 
        <fileset> 
         <directory>gen-external-apklibs</directory> 
         <followSymlinks>false</followSymlinks> 
         <excludes> 
          <exclude>*</exclude> 
         </excludes> 
        </fileset> 
       </filesets> 
       <verbose>true</verbose> 
      </configuration> 
     </plugin> 
4

解決方案與Apache Maven AntRun Plugin 1.8

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-antrun-plugin</artifactId> 
    <version>1.8</version> 
    <executions> 
    <execution> 
     <phase>initialize</phase> 
     <goals> 
     <goal>run</goal> 
     </goals> 
     <configuration> 
     <target> 
      <delete 
      dir="${project.build.outputDirectory}/resources" 
      includeemptydirs="true"/> 
     </target> 
     </configuration> 
    </execution> 
    </executions> 
</plugin> 
1

我只需要一對夫婦從輸出目錄中刪除的文件,以下爲我工作得很好。

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-antrun-plugin</artifactId> 
    <version>1.8</version> 
    <executions> 
     <execution> 
      <phase>compile</phase> 
      <goals> 
       <goal>run</goal> 
      </goals> 
      <configuration> 
       <tasks> 
        <delete file="${project.build.outputDirectory}/appContextLocal.xml" /> 
        <delete 
         file="${project.build.outputDirectory}/appContextServer.xml" /> 
       </tasks> 
      </configuration> 
     </execution> 
    </executions> 
</plugin> 

我也想通,你可以運行任何螞蟻命令替換這裏什麼都任務中,你需要在<tasks> .... </tasks>之間,它會工作。 http://maven.apache.org/plugins/maven-antrun-plugin/usage.html