Home > Net >  Maven: How to extract specific files from a dependency and include them in your own project's g
Maven: How to extract specific files from a dependency and include them in your own project's g

Time:01-22

I have a project that needs specific xml files contained in a dependency to be included in the root of the jar for my own project.

To better illustrate. MyProject has a dependency DependencyA.jar with the following structure:

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

I'm trying to generate MyProject.jar so that it has the following structure

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

I'm not aware of any maven plugin I can use to create this structure. My idea is to hook to the process-resources phase of maven, extract the xml files from the DependencyA.jar, and copy them to ${project.build.directory}/classes so that it's packaged in the root of the MyProject.jar. Not sure if that would work or what plugin I can use to do the extraction.

CodePudding user response:

I was able to get the desired behavior with the maven dependency plugin's unpack goal:

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

The FlattenFileMapper took care of removing the folder structure and placing all the xml files in the root of my project's jar file.

CodePudding user response:

One way to achieve this would be to use the Maven Assembly Plugin to create a custom assembly that includes the XML files from the DependencyA.jar in the root of your MyProject.jar. The assembly plugin allows you to specify the files you want to include in the assembly, and where they should be placed in the final JAR.

You can add the following to your MyProject's pom.xml to configure the assembly plugin:

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

Then create a file called xml-files.xml in the src/main/assembly directory with the following contents:

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

This will create a custom assembly that includes the files in the folder1 and folder2 of the DependencyA.jar in the root of your MyProject.jar.

You can run the following command to create the jar with the custom assembly:

mvn clean package

The jar file will be in the target folder.

  • Related