1

我的任务是解压 pom.xml 中提到的所有 jar,然后将解压后的内容 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 插件基本上是默认删除目标目录。那么如何在构建结束时从目标目录中删除“alternateLocation”文件夹。(我想你可以使用 maven-antrun-plugin 来做到这一点,但我不想使用这个插件。)。

4

1 回答 1

0

您可以通过预定义的描述符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>

比你不需要补充配置。

于 2016-05-05T15:54:04.730 回答