Shortcut to unpack an assembly and copy it to a given folder? - maven

I have a fairly simple Maven assembly which I'd like to deploy to a folder on the filesystem. I could definitely just chain my Maven build with an extraction command, but is there a way to do the following from within Maven by passing it a property of where to deploy the files to?
mvn install && tar -C /my/deployment/folder xvzf target/${project.artifactId}-${project.version}-default.tar.gz ${project.artifactId}-${project.version}-default/
Is there a way that I could provide Maven some configuration so I could do something like:
mvn install deploy -DdeploymentDir=/my/deployment/folder

use dir <format>dir</format> to obtain an unpacked dir
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>path/to/assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
assembly.xml
<?xml version="1.0" encoding="GBK"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.1">
<id></id>
<baseDirectory>${project.artifactId}-${project.version}-default/</baseDirectory>
<formats>
<format>dir</format>
</formats>
<fileSets>
<fileSet>
<directory>../../path/to/${project.artifactId}-${project.version}-default/</directory>
<outputDirectory></outputDirectory>
<includes>
<include>**/**</include>
</includes>
</fileSet>
</fileSets>
</assembly>
use antrun to copy to dest dir
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase> ... choose after assembly </phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<copy todir="/tmp/xx" >
<fileset dir="target/${project.build.finalName}"/>
</copy>
</tasks>
</configuration>
</execution>
</executions>
</plugin>

Related

How to create password-protected zip in maven

currently I am using maven-assembly-plugin to create a zip file via "mvn clean package". The whole process works fine, the zip contains everything I need but a new requirement asks to create this zip file password-protected. I googled but not found any useful; is there anyone who needed to perform this task ?
Current plugin is defined in this way:
<build>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>create-distribution</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>assembly/zip.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</build>
and zip.xml is defined in this way:
<?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>zip</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<outputDirectory>directory1/</outputDirectory>
<directory>directory1</directory>
</fileSet>
<fileSet>
<outputDirectory>/</outputDirectory>
<includes>
<include>config.properties</include>
</includes>
</fileSet>
</fileSets>
<files>
<file>
<source>target/data-jar-with-dependencies.jar</source>
<destName>data-jar-with-dependencies.jar</destName>
</file>
</files>
</assembly>
You must use an external program to unzip and zip the archive again. For example 7z:
7z e Unprotected.zip
7z a Protected.zip -pPASSWORD UNPROTECTEDFILENAME
If you want to integrate it in the build lifecycle then you need the maven exec-plugin to execute those two commands after the package phase.

How can a jar with specific deps be created, after using maven-dependency-plugin to select the wanted deps?

My goal is to create a jar with specific dependencies from my dependency list in the pom. I'm using maven-dependency-plugin like so:
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<phase>prepare-package</phase>
<configuration>
<includeScope>runtime</includeScope>
<excludes>META-INF/*.SF,META-INF/*.DSA,META-INF/*.RSA</excludes>
<outputDirectory>${project.build.directory}/uber-deps/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.some.blaClass</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
and an assembly.xml file holding:
<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>plugin</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
<includes>
<include>
${project.build.directory}/uber-deps/
</include>
</includes>
<excludes>
<exclude>*:sources</exclude>
</excludes>
</dependencySet>
</dependencySets>
</assembly>
After mvn clean install all relevant dependencies appear in target/uber-deps as I would expect. My problem is with the next plugin under <plugins> - maven-assembly-plugin. Seems to me as if it doesn't take uber-deps in.
I know this only by trying to unpack the jar using jar xf to see if the deps in uber-deps were packed in the jar created after mvn clean install.
What should be changed?
1)
The jar you are building as part of the assembly-plugin will be called (by default) ./target/<artifactId>-plugin.jar
Note that the plugin part is what you've put under id in your assembly xml file.
2)
Since you already unpack the dependencies to a folder, you should use fileSets rather then dependencySets:
<fileSets>
<fileSets>
<fileSet>
<outputDirectory>/</outputDirectory>
<includes>
<include>
${project.build.directory}/uber-deps/
</include>
</includes>
</fileSet>
</fileSets>
</fileSets>
3)
BTW if you want the outputs of your own project in that jar you should add another fileSet:
<fileSet>
<outputDirectory>/</outputDirectory>
<includes>
<include>
${project.build.outputDirectory}
</include>
</includes>
</fileSet>
4) Also just noted that your assembly plugin definition is not stating the location of your assembly xml file and that you try to define mainClass using shade-plugin configuration. This is how it should look in assembly plugin (assuming that your assembly file is located under src/assembly/plugin.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<configuration>
<descriptors>
<descriptor>src/assembly/plugin.xml</descriptor>
</descriptors>
<archive>
<manifest>
<mainClass>com.some.blaClass</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>

Maven assembly plugin - "install" folder

I have configured maven assembly plugin which generates "tar.gz" file.
However, when I untar, it somehow includes "install" folder at the beginning:
/install/lib
/install/scripts
My requirement is that, upon untar, it should have folders like below, no install directory:
/lib
/scripts
My pom.xml plugin:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>install</finalName>
<descriptors>
<descriptor>assembly/app-assembly.xml</descriptor>
</descriptors>
<tarLongFileMode>posix</tarLongFileMode>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
app-assembly.xml:
<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>myapp</id>
<formats>
<format>tar.gz</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.basedir}</directory>
<excludes>
<exclude>src/**</exclude>
<exclude>readme.md</exclude>
</excludes>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>lib</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>
Appreciate any help or guidance on this. Thanks for reading.
https://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
includeBaseDirectory: Includes a base directory in the final archive. For example, if you are creating an assembly named "your-app", setting includeBaseDirectory to true will create an archive that includes this base directory. If this option is set to false the archive created will unzip its content to the current directory.
Default value is: true.

Maven assembly: generate a resource zip file and include it into the main assembly zip file

How can I proceed to include a generated zip file into a main zip file with the Maven Assembly plugin?
For example, I have the following folder structure:
./folderA/
./file1
and I want to generate a zip file where the content is like this:
folderA.zip
file1
Here my simplified config':
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project >
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptor>${basedir}/assembly-zipFolderA.xml</descriptor>
<finalName>folderA.zip</finalName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptor>${basedir}/assembly.xml</descriptor>
<finalName>${module-zipFinalName}</finalName>
</configuration>
</plugin>
</plugins>
</build>
</project>
assembly-zipFolderA.xml
<assembly>
<id>folderA-zip</id>
<includeBaseDirectory>false</includeBaseDirectory>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>folderA</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</assembly>
assembly.xml
<assembly>
<id>main</id>
<includeBaseDirectory>false</includeBaseDirectory>
<formats>
<format>zip</format>
</formats>
<files>
<file>
<source>file1</source>
</file>
<file>
<source>folderA.zip</source>
</file>
</files>
</assembly>
===> with this config, Maven complains that it can't find folderA.zip...
Don't declare the plugin twice; instead, you need to declare two executions of the maven-assembly-plugin.
The first execution will create the first assembly and then, the second execution will use this assembly to create the final one. Both of those executions will be bound to the package phase, and Maven will invoke the plugin's execution in the order of declaration in the POM.
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>make-folderA</id>
<goals>
<goal>single</goal>
</goals>
<phase>package</phase>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>${basedir}/assembly-zipFolderA.xml</descriptor>
</descriptors>
<finalName>folderA</finalName> <!-- name without extension -->
<attach>false</attach>
</configuration>
</execution>
<execution>
<id>make-assembly</id>
<goals>
<goal>single</goal>
</goals>
<phase>package</phase>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>${basedir}/assembly.xml</descriptor>
</descriptors>
<finalName>${module-zipFinalName}</finalName>
</configuration>
</execution>
</executions>
</plugin>
A couple of notes:
The <finalName> should not contain the extension, so a finalName of folderA will produce the archive folderA.zip after the ZIP assembly is made.
The first assembly will be created inside the build directory, which is target by default. As such, the second one needs to reference it there with:
<file>
<source>${project.build.directory}/folderA.zip</source>
</file>
in its assembly descriptor.
Since the first assembly is not the final one, you probably don't want it attached, i.e. as an additional artifact produced by your project. You can disable this by setting attach to false. This will make sure only the last final one is considered when deploying or releasing.
descriptor is a deprecated parameter, you should use descriptors instead.
With such a configuration, running mvn clean package will produce the correct archive inside the build folder.

Maven AntRun plugin include root folder on tar

on my project I need to create a tar with the full content of a folder.
I do it with the maven antRun plugin.
My problems is that the main root isn't in to the tar.
How to insert the folder?
Es.
I have
Target
---temp
---Example
------aaa.png
------bbbb.png
------rsc
---------ccc.png
Im my file.tar.gz I Have
aaa.png
bbbb.png
rsc
---ccc.png
I wanna
---Example
------aaa.png
------bbbb.png
------rsc
---------ccc.png
Code to create the tar
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>tar batch</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target name="tar.gz folder">
<echo message="Starting to compress with tar.gz ${main.builddir}${file.separator}${tar.name.batch} in ${main.builddir}${file.separator}${tar.name.batch}.tar.gz " />
<tar destfile="${main.builddir}${file.separator}${tar.name.batch}.tar" basedir="${main.builddir}${file.separator}${tar.name.batch}" />
<gzip destfile="${project.build.directory}${file.separator}${tar.name.batch}.tar.gz" src="${main.builddir}${file.separator}${tar.name.batch}.tar" />
</target>
</configuration>
</execution>
</executions>
</plugin>
I would suggest to use maven-assembly-plugin instead of maven-antrun-plugin. You need a configuration in your pom file like this:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>create-archives</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Furthermore you need to have a assembly descriptor which describes what should be packaged into the archive like this:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>dist-assembly</id>
<formats>
<format>tar</format>
</formats>
<!--
! The following will include the base directory incl. version
! into the resulting tar.
! The default value is: true.
! <includeBaseDirectory>true</includeBaseDirectory>
-->
<fileSets>
<fileSet>
<directory>folder</directory>
<outputDirectory>.</outputDirectory>
</fileSet>
</fileSets>
</assembly>
To control which folders are inside the archive you can use outputDirectory for it. The directory control which folder should be packaged into the archive.
it's not the cleanest solution, but i solved adding another sub folder,
so when I generate the jar, i put it in executable/executable, and after i make the tar.gz of the first executable's folder.

Resources