【问题标题】:How to remove a a specific directory from the Maven's target directory at the end of the build?如何在构建结束时从 Maven 目标目录中删除特定目录?
【发布时间】:2016-08-31 11:11:13
【问题描述】:

我的任务是解压 pom.xml 中提到的所有 jar,然后将解压后的内容打包到一个 jar 中。我可以使用依赖插件和 jar 插件的 unpack-dependency 目标来做到这一点。 但是,在我生成新的 jar 之后,我想删除解压后创建的文件夹。我在 pom.xml 中使用了以下代码 sn-p(请阅读每个插件上方的 cmets)。

<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插件基本上是默认删除目标目录。 那么如何在构建结束时从目标目录中删除“alternateLocation”文件夹。 (我想你可以使用 maven-antrun-plugin 来做到这一点,但我不想使用这个插件。)。

【问题讨论】:

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


    【解决方案1】:

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

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

    比你不需要补充配置。

    【讨论】:

      猜你喜欢
      • 2019-02-09
      • 2010-12-05
      • 2021-11-01
      • 1970-01-01
      • 2012-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多