maven assembly descriptor- copy folder and files of any level - maven

I am using maven-assembly-plugin to build a tar.gz file. I want content of a folder and all subfolders to be included in the tar file. Can anyone help in this? Thanks in advance. :)
Here is my assembly.xml content:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>dist</id>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.basedir}/src/main/dist/lib</directory>
<outputDirectory>/lib</outputDirectory>
<includes>
<include>*</include>
</includes>
<lineEnding>unix</lineEnding>
<filtered>true</filtered>
</fileSet>
</fileSets>
I have two directories under /src/main/dist/lib and need all the content of these two directories to be copied inside my tar file.
With above assembly.xml, only first level folder are copied with no content under them.
e.g. If I have folders like : /src/main/dist/lib/folder1/... and /src/main/dist/lib/folder1/...
Generated tar just have lib/folder1 and lib/folder2 with no files and subfolders under it.

Use the syntax: `
<includes>
<include>**</include>
</includes>
`

Related

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

How can I rename a file when assembling a spring boot project?

I have the following assembly.xml. I was trying to figure out how when including files I can rename them to something else. Currently, my tar has the war file as project-1.0.0.0.war and I want it to be project.war. How can I accomplish this?
<assembly>
<id>${version}-tar</id>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<baseDirectory>project-${version}</baseDirectory>
<fileSets>
<fileSet>
<directory>target</directory>
<outputDirectory>.</outputDirectory>
<includes>
<include>project-${version}.war</include>
</includes>
</fileSet>
<fileSet>
<directory>etc/bin</directory>
<outputDirectory>.</outputDirectory>
<includes>
<include>start</include>
<include>stop</include>
</includes>
</fileSet>
</fileSets>
If you want to control the destination name of the file in the assembly, you shouldn't use a <fileSet>, but a <file>. The reason is that the first groups several files together, and as such, doesn't provide a way to control the name of each file in the group. Since a <file> targets only a single file, you can control the destination name with the <destName> element:
Sets the destination filename in the outputDirectory. Default is the same name as the source's file.
You should have instead:
<files>
<file>
<source>target/project-${version}.war</directory>
<destName>project.war</destName>
</file>
</files>
<!-- the other "fileSets" for etc/bin, unchanged -->
instead of the <fileSet>. This will make sure that the file specified in the <source> element is renamed to <destName> in the resulting assembly.
You are looking to add:
<build>
...
<finalName>${artifactId}</finalName>
...
</build>
Documentation

Maven: add non-JAR dependency

I'am using the maven-assembly-plugin to create a tar.gz archive. Now I need to include files in that archive. These are just a bunch of plain old text files and are a dependency at runtime, not at compile or (unit) test time.
I thought about adding a maven dependency on that but these files are not stored in a maven repository but in a simple remove folder. To make it even worse, this folder is password protected. I could, in a pre-build-step, download these password-protected files into a local folder. But then there is still the problem of using those files in the assembly. Also, this is extra work I want to avoid.
Is there any way to do that?
If you can download those files, workaround password. Maybe this will help.
Create mod assembly project where you put something like that:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>${artifact.version}</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
<useTransitiveDependencies>true</useTransitiveDependencies>
<unpack>false</unpack>
<excludes>
<exclude>${project.groupId}:*:*</exclude>
</excludes>
<outputFileNameMapping>project${timestamp}_code-${artifact.name}.${artifact.extension}</outputFileNameMapping>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<includes>
<include>readme.txt</include> <!-- here's that plain file -->
</includes>
</fileSet>
</fileSets>
You can include those files in fileSets.
http://maven.apache.org/plugins/maven-assembly-plugin/examples/single/filtering-some-distribution-files.html for more info

maven-assembly-plugin include *.properties at root level of zip

I'm trying to get maven to include my *.properties files when it zips up my artifacts. They are located inside src/main/resources. I tried adding the fileSet element to my assembly file, but the resources are not being included in the zip. I saw this question which seems to indicate that adding fileSet should work.
plugins.xml:
<?xml version="1.0"?>
<assembly>
<id>release</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.properties</include>
</includes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<useTransitiveFiltering>true</useTransitiveFiltering>
</dependencySet>
</dependencySets>
</assembly>
The properties that you want to include inside your ZIP are located in the src/main/resources source directory of your project. So the <fileSet> element should point to this directory.
${project.build.directory} is Maven current build directory, which is by default target. You could also point to the temporary directory where Maven copies all resources during the build but it is preferable to stick with the permanent data where possible.
As such, you just need to change your <fileSet> element with:
<directory>src/main/resources</directory>

Deploy Akka java microkernel with maven

I am trying my hands on JCranky's tutorials about distributing akka microkernel with maven and tbt start scripts click. from the descriptor.xml file I suppose there are four directories to be created by the assembly pluggin.
below is the descriptor.xml file
<id>akka</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/deploy</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>/lib</outputDirectory>
</dependencySet>
</dependencySets>
<files>
<file>
<source>src/main/start</source>
<outputDirectory>/bin</outputDirectory>
</file>
<file>
<source>src/main/resources/application.conf</source>
<outputDirectory>/config</outputDirectory>
</file>
</files>
</assembly>
The following directories are created in the zip folder. that is the lib, config and bin directories. Reading from the descriptor.xml file , looks like the deploy directory has to be created. don't know if I am missing something. Will be very happy for a clarification.
You have the wrong directory in the fileset.
Change to:
<directory>${project.build.directory}</directory>
It cannot find any files in the place you stated. Thus no files were copied and the deploy directory didn't have to be created.

Resources