Daniel Rodríguez
Daniel Rodríguez

Reputation: 582

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:

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

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!

Upvotes: 7

Views: 9745

Answers (3)

AlpharettaTechy
AlpharettaTechy

Reputation: 390

I had the same problem, using dependencies plugin dumped a lot of other files, which I am sure it can be improved. But I think I found a simpler and better solution.

All you need to do is add another <fileSet>. The fileSet has the relative source path and an output directory. The value of your outputDirectory determines the path in the resulting zip. It is as simple as that. Hope this helps someone out there, fighting with producing lambda zips for AWS (rolling my eyes)

Here is my assembly.xml BEFORE(HAS PROBLEM) that I was struggling with:

  <assembly>
        <formats>
            <format>zip</format>
        </formats>
        <includeBaseDirectory>false</includeBaseDirectory>
        <fileSets>
            <fileSet>
                <directory></directory>
                <outputDirectory/>
                <includes>
                    <include>lib/**.*</include>
                   <include>${basedir}/target/classes/com/abc/test.LambdaTest.*</include>
                </includes>
            </fileSet>
        </fileSets>
    </assembly>

The above produces zip with directory target, but here is an improved and FIXED (CORRECT) which does not have the path "target" in zip.

<assembly>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory></directory>
            <outputDirectory/>
            <includes>
                <include>lib/**.*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${basedir}/target/classes</directory>
            <outputDirectory/>
            <includes>
                <include>com/abc/test.LambdaTest.*</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

Pay attention to the value of <directory> in both fileSets, which provides a relative path which is not used to when files are added to the zip

Upvotes: 0

zafar142003
zafar142003

Reputation: 2159

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>

Upvotes: 6

Tome
Tome

Reputation: 3474

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>

Upvotes: 4

Related Questions