Remove or delete resource files from target direct

2019-01-06 19:45发布

I have two profiles in pom.xml, and I have some resource files which I have added into target resource directory: ${project.build.outputDirectory}/resources during execution of the first profile. What I need to do is remove those resource files during execution of the second profile. Is there any way to remove or delete existing files from target directory?

标签: maven pom.xml
6条回答
地球回转人心会变
2楼-- · 2019-01-06 19:58

thanks to above answers. finally i came to something like:

if you want to just delete some directories in target folder, you have to create some construct like this.
this for instance deletes just all contents of folders:

  • target/unpack
  • gen-external-apklibs

excludeDefaultDirectories allows to not delete complete target folder.
i used it to clean up target folder before lint analysis.

       <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>
查看更多
该账号已被封号
3楼-- · 2019-01-06 20:01

Solution with 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>
查看更多
欢心
4楼-- · 2019-01-06 20:17

I needed only a couple of files deleted from the output directory, the following worked fine for me.

<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>

I also figured that you can run any ant commands here replace what ever task you need in between the <tasks> .... </tasks> and it will work.

List of ant tasks that you can perform are here

Ref: http://maven.apache.org/plugins/maven-antrun-plugin/usage.html

查看更多
三岁会撩人
5楼-- · 2019-01-06 20:19

I got the solution..!!

<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>

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

查看更多
甜甜的少女心
6楼-- · 2019-01-06 20:19

I do agree with Matthew's observations, but I got the impression that the original poster was asking how to automate execution of clean during (normal) "execution" of a profile.

You can define a plugin execution for the Maven Clean Plugin. It is normally only bound to clean, but by defining a plugin execution you can bind clean:clean (that is the clean goal of the clean plugin) to whichever lifecycle phase you want. The documentation of the Maven Clean Plugin has an example of how to do this. The documentation also has an example of deleting additional files. Merged the two looks like this:

  <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>
查看更多
贼婆χ
7楼-- · 2019-01-06 20:19

mvn clean will remove the target directory (and therefore all the files in it). If you want to remove only certain files from the target directory, a combination of:

  • excludeDefaultDirectories to stop it from deleting the whole directory, and

  • filesets to tell it what to delete

ref: http://maven.apache.org/plugins/maven-clean-plugin/clean-mojo.html

查看更多
登录 后发表回答