Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven assembly plugin: include file without taking its path folders

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!

like image 919
Daniel Rodríguez Avatar asked Sep 07 '17 09:09

Daniel Rodríguez


People also ask

What is descriptorRef in Maven Assembly plugin?

Use src as the descriptorRef in your assembly-plugin configuration to create source archives for your project. The archive will contain the contents of your project's /src directory structure, for reference by your users. The src descriptorId produces an assembly archive with the classifier src in three formats: tar.

What is fileset in Maven?

Defines the rules for matching and working with files in a given base directory. Element.

How do I use Maven to package a zip file?

In your maven project create a folder assembly . Add zip. xml file in the assembly folder. Add below code in zip.

What is assembly descriptor?

So in order for you to customize the way the Assembly Plugin creates your assemblies, you need to know how to use the Assembly Descriptor. This descriptor specifies the type of assembly archive to create, the contents of the assembly, and the ways in which dependencies or its modules are bundled with an assembly.


2 Answers

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>
like image 100
zafar142003 Avatar answered Sep 30 '22 03:09

zafar142003


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>
like image 20
Tome Avatar answered Sep 30 '22 03:09

Tome