Maven assembly plugin: include file without taking its path folders

梦想的初衷 提交于 2019-12-12 18:24:35

问题


I'm using maven-assembly-plugin to include files from a dependency ZIP (also generated with assembly plugin) into a final release ZIP file.

The issue is that I want to select which files from the dependency to get, but not copying the folder path where those files are. Just the files.

For example:

<assembly>
    <formats>
        <format>zip</format>
        <format>dir</format>
    </formats>

    <includeBaseDirectory>false</includeBaseDirectory>

    <dependencySets>
        <dependencySet>
            <includes>
                <include>package:artifactId:zip:*</include>
            </includes>
            <outputDirectory>sql/update/01.00.00_to_01.01.00</outputDirectory>
            <unpack>true</unpack>
            <unpackOptions>
                <includes>
                    <include>oracle/update/1_alter_schema.sql</include>
                    <include>oracle/update/2_insert_data.sql</include>
                </includes>
            </unpackOptions>
            <useProjectArtifact>false</useProjectArtifact>
            <useTransitiveDependencies>false</useTransitiveDependencies>
        </dependencySet>
    </dependencySet>
</assembly>

This copies the required files like this:

  • sql/update/01.00.00_to_01.01.00/oracle/update/1_alter_schema.sql
  • sql/update/01.00.00_to_01.01.00/oracle/update/2_insert_data.sql

I would like to copy just the files without the original oracle/update/ folder, resulting in this folder structure:

  • sql/update/01.00.00_to_01.01.00/1_alter_schema.sql
  • sql/update/01.00.00_to_01.01.00/2_insert_data.sql

The dependency ZIP contains many files used by different projects, therefore the structure to differentiate oracle from sql-server files makes sense there, but for this distribution I don't need those folders, just the files.

Does somebody knows if this is possible with maven-assembly-plugin?

Many thanks in advance!


回答1:


It should work by splitting the dependency unzipping and the file assembly.

Configure the dependency-plugin to unpack the desired dependency before performing the assembly work:

<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <configuration>
        <includeArtifactIds>project-sql</includeArtifactIds>
        <outputDirectory>${project.build.directory}/extract</outputDirectory>
        <includes>oracle/update/1_alter_schema.sql,oracle/update/2_insert_data.sql</includes>
    </configuration>
    <executions>
        <execution>
            <id>unpack-sql</id>
            <phase>prepare-package</phase>
            <goals><goal>unpack-dependencies</goal></goals>
        </execution>
    </executions>
</plugin>

Then, in your assembly-distribution.xml, just assemble from the sub-directory:

<?xml version="1.0" encoding="UTF-8"?>
<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>distribution-${project.version}</id>

    <formats>
        <format>zip</format>
        <format>dir</format>
    </formats>

    <includeBaseDirectory>false</includeBaseDirectory>

    <fileSets>
        <fileSet>
            <directory>${project.build.directory}/extract/oracle/update</directory>
            <outputDirectory>sql/update/01.00.00_to_01.01.00</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>



回答2:


From the documentation of maven-assembly-plugin (maven-assembly-plugin) I can see that the <fileSets> tag does not provide us with the option of changing the path of an included resource. Instead we can use the <file> tag which gives us this flexibility.

For example, the below configuration will include file1.jar and run.bat in the root folder of the extracted zip file, skipping their original paths.

<assembly>
  <formats>
    <format>zip</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <files>
    <file>
      <source>target/file1.jar</source>
      <destName>file1.jar</destName>
    </file>
    <file>
      <source>src/main/resources/run.bat</source>
      <destName>run.bat</destName>
    </file>
  </files>
</assembly>


来源:https://stackoverflow.com/questions/46092911/maven-assembly-plugin-include-file-without-taking-its-path-folders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!