How to remove a a specific directory from the Maven's target directory at the end of the build?

1.2k views Asked by At

I have a task to unpack all the jars mentioned in the pom.xml and then jar the unpacked content into one single jar. I am able to do this using the unpack-dependency goal of the dependency plugin and the jar plugin. However, after i generate the new jar, I want to delete the folder that was created after unpacking. I am using the following code snippet in my pom.xml(Please read the comments above each plugin).

<build>
    <plugins>
     <!-- This part of the code is used to unpack all the dependencies mentioned in my pom.xml -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.10</version>
        <executions>
          <execution>
            <id>unpack-dependencies</id>
            <phase>package</phase>
            <goals>
              <goal>unpack-dependencies</goal>
            </goals>
            <configuration>
              <includes>**/*.class</includes>
              <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
              <overWriteReleases>false</overWriteReleases>
              <overWriteSnapshots>true</overWriteSnapshots>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <!-- This part of the code is used for creating a jar with all the contents of the "alternateLocation" directory above -->
      <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <configuration>
                        <classesDirectory>${project.build.directory}/alternateLocation</classesDirectory>
                        <outputDirectory>${project.build.directory}</outputDirectory>
                        <finalName>abc</finalName>
                    </configuration>
                </execution>
            </executions>
        </plugin>

       <!-- I am using this part of the code to delete the "alternateLocation" after everything is done, but it deletes the target directory instead -->
        <plugin>
            <artifactId>maven-clean-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <id>auto-clean</id>
                    <phase>install</phase>
                    <goals>
                        <goal>clean</goal>
                    </goals>
                    <configuration>
                        <filesets>
                            <fileset>
                                <directory>${project.build.directory}/alternateLocation</directory>
                            </fileset>
                        </filesets>
                        <excludeDefaultDirectories>${project.build.directory}</excludeDefaultDirectories>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
  </build>

The clean plugin is basically deleting the target directory by default. So how can I delete the "alternateLocation" folder from the target directory at the end of the build. (I guess you can do it using the maven-antrun-plugin but i don't want to use this plugin.).

1

There are 1 answers

0
khmarbaise On

You can solved this by using the maven-assembly-plugin via the predefined descriptor jar-with-dependencies which can be done by the following:

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      [...]
</project>

Than you don't need supplemental configuration.