如何在构建结束时从Maven的目标目录中删除特定目录?

时间:2016-05-05 15:26:04

标签: maven pom.xml maven-dependency-plugin maven-jar-plugin maven-clean-plugin

我有一个任务要解压缩pom.xml中提到的所有jar,然后将解压缩的内容放入一个jar中。我能够使用依赖插件和jar插件的unpack-dependency目标来实现这一点。 但是,在生成新jar之后,我想删除解压缩后创建的文件夹。我在我的pom.xml中使用以下代码片段(请阅读每个插件上面的注释)。

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

clean插件基本上是默认删除目标目录。 那么如何删除&#34; alternateLocation&#34;构建结束时目标目录中的文件夹。 (我猜你可以使用maven-antrun-plugin来做,但我不想使用这个插件。)。

1 个答案:

答案 0 :(得分:0)

您可以通过预定义描述符maven-assembly-plugin使用jar-with-dependencies解决此问题,可以通过以下方式完成:

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

比你不需要补充配置。