簡體   English   中英

Maven:如何從依賴項中提取特定文件並將它們包含在您自己的項目生成的 jar 中?

[英]Maven: How to extract specific files from a dependency and include them in your own project's generated jar?

我有一個項目需要包含在依賴項中的特定 xml 文件,以便包含在我自己項目的 jar 的根目錄中。

為了更好地說明。 MyProject有一個依賴DependencyA.jar ,其結構如下:

DependencyA.jar
 |
 +-- folder1
 |  |  
 |  +-- file11.xml
 |  |  
 |  +-- file12.xml
 |
 +-- folder2
 |  |  
 |  +-- file21.xml
 |
 +-- Other contents

我正在嘗試生成MyProject.jar以便它具有以下結構

MyProject.jar
 |
 +-- file11.xml
 |
 +-- file12.xml
 |
 +-- file21.xml
 |
 +-- Other contents

我不知道可以使用任何 maven 插件來創建此結構。 我的想法是掛鈎到 maven 的process-resources階段,從DependencyA.jar中提取 xml 文件,並將它們復制到${project.build.directory}/classes以便它打包在MyProject.jar的根目錄中. 不確定這是否可行或我可以使用什么插件進行提取。

我能夠通過 maven 依賴插件的unpack目標獲得所需的行為:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.3.0</version>
            <executions>
                <execution>
                    <id>unpack</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>com.example</groupId>
                                <artifactId>DependencyA</artifactId>
                                <version>1.0</version>
                                <includes>
                                    folder1/*.xml,
                                    folder2/*.xml
                                </includes>
                                <fileMappers>
                                    <fileMapper implementation="org.codehaus.plexus.components.io.filemappers.FlattenFileMapper"/>
                                </fileMappers>
                                <outputDirectory>${project.build.directory}/classes</outputDirectory>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>

FlattenFileMapper負責刪除文件夾結構並將所有 883812976388 文件放在我項目的 jar 文件的根目錄中。

實現此目的的一種方法是使用 Maven 程序集插件創建一個自定義程序集,其中包括來自 MyProject.jar 根目錄中的 DependencyA.jar 的 XML 文件。 程序集插件允許您指定要包含在程序集中的文件,以及它們在最終 JAR 中應放置的位置。

您可以將以下內容添加到您的 MyProject 的 pom.xml 以配置程序集插件:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <version>3.3.0</version>
      <executions>
        <execution>
          <id>make-assembly</id>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <descriptors>
          <descriptor>src/main/assembly/xml-files.xml</descriptor>
        </descriptors>
      </configuration>
    </plugin>
  </plugins>
</build>

然后在 src/main/assembly 目錄中創建一個名為 xml-files.xml 的文件,內容如下:

<assembly>
  <formats>
    <format>jar</format>
  </formats>
  <fileSets>
    <fileSet>
      <directory>path/to/dependencyA/folder1</directory>
      <outputDirectory>/</outputDirectory>
    </fileSet>
    <fileSet>
      <directory>path/to/dependencyA/folder2</directory>
      <outputDirectory>/</outputDirectory>
    </fileSet>
  </fileSets>
</assembly>

這將創建一個自定義程序集,其中包含 MyProject.jar 根目錄中 DependencyA.jar 的 folder1 和 folder2 中的文件。

您可以運行以下命令以使用自定義程序集創建 jar:

mvn clean package

jar 文件將位於目標文件夾中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM