How to create password-protected zip in maven - 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.

Related

creating a tar archive from maven deploy

I am trying to use maven v3 to create a tar.gz output for a non-Java project in order to deploy to nexus. I cannot specify tar in the packaging option in my pom file. Unless mistaken, I can specify tar as an option for packaging when running mvn deploy. Is there anyway I can specify that in the pom file itself?
You can create the TAR.GZ file and "attach" it to the primary artifact. I have an example to repackage a WAR into Linux service on GitHub. The key steps are to package the assembly:
<project ...>
...
<build>
<pluginManagement>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<attach>true</attach>
<formats>
<format>tar.gz</format>
</formats>
<descriptorSourceDirectory>src/main/assemblies</descriptorSourceDirectory>
</configuration>
</plugin>
...
</plugins>
</pluginManagement>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
...
</plugins>
</build>
</project>
<attach>true</attach> installs the artifact. The assembly in this case is:
<?xml version="1.0" encoding="UTF-8"?>
<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>bin</id>
<baseDirectory>${artifactId}-${version}</baseDirectory>
<files>
<file>
<outputDirectory>bin</outputDirectory>
<source>
${project.build.directory}/${project.build.finalName}.jar
</source>
<filtered>false</filtered>
<destName>${artifactId}.jar</destName>
</file>
</files>
<fileSets>
<fileSet>
<outputDirectory>bin</outputDirectory>
<directory>${project.basedir}/src/bin</directory>
<includes>
<include>${artifactId}.conf</include>
</includes>
<filtered>true</filtered>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<includes>
<include>*:*-x86_64:*</include>
</includes>
<scope>runtime</scope>
<outputFileNameMapping>
lib${artifact.artifactId}.${artifact.type}
</outputFileNameMapping>
</dependencySet>
</dependencySets>
</assembly>
but you'll need to find/create one appropriate for your use-case.

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.

how in maven multiple folders to the multiple zip archives with the same names

I want to collect the project module using, Maven
I have a directory structure. Folders can be added dirN:
project-module
|
|-dir1
|-dir2
|-dir3
|-...
|-dirN
|-bin.xml
|-pom.xml
I tried the maven-assembly-plugin
pom.xml
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>bin.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>sql dir</id>
<goals>
<goal>single</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
</plugin>
</plugins>
bin.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>bin</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>../mspost-db</directory>
<excludes>
<exclude>*.*</exclude>
</excludes>
<useDefaultExcludes>false</useDefaultExcludes>
<!--<outputDirectory>/</outputDirectory>-->
</fileSet>
</fileSets>
</assembly>
DESIRED OUTCOME
Each directory to be packaged into a single zip archive folder with the same name, that is.
project-module
|
|-targer
|-dir1.zip
|-dir2.zip
|-dir3.zip
|-...
|-dirN.zip
help me please.
The iterator-maven-plugin could be used to work with a single assembly descriptor:
<plugin>
<groupId>com.soebes.maven.plugins</groupId>
<artifactId>iterator-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>iterator</goal>
</goals>
<configuration>
<items>
<item>dir1</item>
<item>dir2</item>
<item>dir3</item>
</items>
<pluginExecutors>
<pluginExecutor>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
<goal>single</goal>
<configuration>
<descriptors>
<descriptor>${project.basedir}/bin.xml</descriptor>
</descriptors>
<finalName>${item}</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</pluginExecutor>
</pluginExecutors>
</configuration>
</execution>
</executions>
</plugin>
Then reference ${item} in the bin.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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>${item}</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.basedir}/${item}</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</assembly>
This results in target/dir1.zip etc.
Instead of listing the <items> explicitly, it can also iterate over a <folder>, though I don't see a way that could be restricted to such as dir*.
(Edit start)
E.g. to iterate the (immediate) subdirectories of parentDir:
<configuration>
<folder>${project.basedir}/parentDir</folder>
<pluginExecutors>
... as above ...
The assembly descriptor (bin.xml) can then reference something like <directory>${project.basedir}/parentDir/${item}</directory>, much as before.
The lack of a filter would still be an issue if iterating over <folder>${project.basedir}</folder> though - this would zip up the target directory too.

How to provide a custom name to extracted folder from a zip, generated using maven-assembly-plugin

I'm using the maven assembly plugin to package a few directories into a zip file.
The following is the zip.xml.
<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">
<baseDirectory>${project.build.directory}</baseDirectory>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.build.directory}/bin</directory>
<outputDirectory>bin</outputDirectory>
</fileSet>
<fileSet>
<directory>${project.build.directory}/config</directory>
<outputDirectory>config</outputDirectory>
</fileSet>
<fileSet>
<directory>${project.build.directory}/lib</directory>
<outputDirectory>lib</outputDirectory>
</fileSet>
</fileSets>
</assembly>
Following is the plugin configuration in pom.xml
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>create-zip</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>zip.xml</descriptor>
</descriptors>
<finalName>bootstrap</finalName>
</configuration>
</execution>
</executions>
</plugin>
When I run the assemply, a zip file is created with the name of my maven module, which is as expected. But when I extract this zip file, a folder named 'target' is created which is not very readable. How do i customize the name of this 'target' folder?

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

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>

Resources