pstanton
pstanton

Reputation: 36640

delete the 'target' directory after build

i know this is probably frowned upon by maven lovers, but the whole 'target' directory is a waste of space in the context of our program and it's deployment. we have other build processes responsible for creating the actual deployment and i currently manually delete the target dir after every maven build, so that its contents don't interfere with my file searches etc...

is there a way to delete this dir automatically at the end of a maven build/install?

thanks, p.

Upvotes: 17

Views: 38817

Answers (4)

cV2
cV2

Reputation: 5319

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>

Upvotes: 3

Jamie Townsend
Jamie Townsend

Reputation: 31

The easiest way is simply to add "clean" to the end of the normal build command. eg. mvn clean install clean.

Upvotes: 3

JoseK
JoseK

Reputation: 31371

Use the maven-clean-plugin as here http://maven.apache.org/plugins/maven-clean-plugin/examples/delete_additional_files.html

<project>


[...]
  <build>
<plugins>
  <plugin>
    <artifactId>maven-clean-plugin</artifactId>
    <version>2.4.1</version>
    <executions>
      <execution>
        <id>auto-clean</id>
        <phase>install</phase>
        <goals>
          <goal>clean</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>
  </build>
  [...]
</project>

Upvotes: 18

khmarbaise
khmarbaise

Reputation: 97359

You should simply add the clean goal to your maven goals at the end.

mvn install clean

The problem with the clean-plugin is that if you like to run the clean at the end of the build it depends which goal you called at the beginning. For example you called mvn package you need to have a phase post-package which does not exist or if you called mvn install you have to have phase post-install which does not exist either.

Upvotes: 6

Related Questions