maven-assembly-plugin: Excluding files - maven

I have this piece of code in my pom.xml file
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<finalName>elcordelaciutat-${project.version}</finalName>
<packagingExcludes>***/*.pom</packagingExcludes>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
but I have this error: Element packagingExcludes is not allowed here

packagingExcludes does not appear anywhere in maven-assembly.
And issues related to packagingExcludes involve projects like apache/maven-war-plugin, which does declare packagingExcludes in WarMojo.java
So you might not have that option available for making an archive with maven-assembly.

Related

How to provide different set of properties to the different executions of a plugin?

I am using maven-assembly-plugin to assemble different artifacts as following:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>configuration-staging</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
<execution>
<id>configuration-production</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
In assembly.xml, I enabled template filtering:
<fileSets>
<fileSet>
<filtered>true</filtered>
This works great. For example, if I enter ${name} in one of the resources to be assembled, this is replaced by the name of the project. I could also define properties in pom.xml, which will be replaced by the plugin.
Now, I would like to have different properties for each execution of maven-assembly-plugin. For example, I would like to introduce a ${url} which holds the URL to be used on the target environment (staging and production in the example above).
Is this possible? How?
Apparently, it is possible to pass different properties for each execution in the maven-assembly-plugin as following:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>configuration-staging</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>staging</finalName>
<filters>
<filter>src/main/assembly/staging.properties</filter>
</filters>
</configuration>
</execution>
<execution>
<id>configuration-production</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>production</finalName>
<filters>
<filter>src/main/assembly/production.properties</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
Although this does not answer the generic question, it answers the question specifically for maven-assembly-plugin.
More can be found on https://maven.apache.org/plugins/maven-assembly-plugin/examples/single/filtering-some-distribution-files.html.
You can try with the properties Maven plugin
https://www.mojohaus.org/properties-maven-plugin/index.html
that allows you read properties from files or URLs.

How to remove a a specific directory from the Maven's target directory at the end of the build?

I have a task to unpack all the jars mentioned in the pom.xml and then jar the unpacked content into one single jar. I am able to do this using the unpack-dependency goal of the dependency plugin and the jar plugin.
However, after i generate the new jar, I want to delete the folder that was created after unpacking. I am using the following code snippet in my pom.xml(Please read the comments above each plugin).
<build>
<plugins>
<!-- This part of the code is used to unpack all the dependencies mentioned in my pom.xml -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includes>**/*.class</includes>
<outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
<!-- This part of the code is used for creating a jar with all the contents of the "alternateLocation" directory above -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>${project.build.directory}/alternateLocation</classesDirectory>
<outputDirectory>${project.build.directory}</outputDirectory>
<finalName>abc</finalName>
</configuration>
</execution>
</executions>
</plugin>
<!-- I am using this part of the code to delete the "alternateLocation" after everything is done, but it deletes the target directory instead -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>auto-clean</id>
<phase>install</phase>
<goals>
<goal>clean</goal>
</goals>
<configuration>
<filesets>
<fileset>
<directory>${project.build.directory}/alternateLocation</directory>
</fileset>
</filesets>
<excludeDefaultDirectories>${project.build.directory}</excludeDefaultDirectories>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
The clean plugin is basically deleting the target directory by default.
So how can I delete the "alternateLocation" folder from the target directory at the end of the build. (I guess you can do it using the maven-antrun-plugin but i don't want to use this plugin.).
You can solved this by using the maven-assembly-plugin via the predefined descriptor jar-with-dependencies which can be done by the following:
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
[...]
</project>
Than you don't need supplemental configuration.

Can maven assembly produce a file without the format suffix

I've managed to get maven assembly to produce a custom NAME-assemblyId.tar.gz, but I actually want to name the final result just NAME-assemblyId, without the format suffix.
No combination of finalName and appendAssemblyId can fix this it seems?
Currently I'm using a copy mojo to rename it after packaging, but that is not ideal, I guess I could also use a attach-artifact mojo to attach them though.
Set appendAssemblyId to false in your assembly configuration
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>jar-with-dep</id>
<phase>package</phase>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>com.activee.migration.cli</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<goals>
<goal>single</goal>
</goals>
</execution>
<execution>
<id>cli-zip</id>
<phase>package</phase>
<configuration>
<descriptors>
<descriptor>src/main/assembly/cli_zip.xml</descriptor>
</descriptors>
<finalName>com.activee.migration.cli</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

Maven rename a file after everything else finishes

I have a project which I need to rename the final output file generated by the Maven Assembly Plugin after everything else finishes (in the compiling/building/assembly process).
The Maven Assembly Plugin is generating a final .zip file, based on the project's name, and I need to rename this completely to final-version.oxt. I'm trying to use the maven-antrun-plugin to rename it, as pointed by other similar questions here, but no luck (I've never used Maven or Ant before, so maybe I'm missing something).
This is the <build> section of the project's pom.xml. The rename part seems to be completely ignored, since no file is generated in my home folder.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>assembly</id>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
<configuration>
<archive>
<manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
</archive>
<descriptors>
<descriptor>src/main/assembly/ooo-jar.xml</descriptor>
<descriptor>src/main/assembly/ooo.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>deploy</phase>
<configuration>
<tasks>
<copy file="${project.build.directory}/target/libreofficeplugin-ooo.zip"
tofile="/home/brunofinger/final-version.oxt" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
A few modifications made it work, probably the phase was wrong, but using <phase>install</phase> seems to make it work:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>install</phase>
<configuration>
<target>
<copy file="${project.build.directory}/libreofficeplugin-ooo.zip" tofile="${project.build.directory}/final-version.oxt" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
This question is nearly an year old, but in case anyone else is facing similar issue, here is an alternate solution.
If you are looking for a more mavenish way of doing it,you can use
<plugin>
<groupId>com.coderplus.maven.plugins</groupId>
<artifactId>copy-rename-maven-plugin</artifactId>
<version>1.0.1</version>
<executions>
<execution>
<id>rename-file</id>
<phase>install</phase>
<goals>
<goal>rename</goal>
</goals>
<configuration>
<sourceFile>${project.build.outputDirectory}/libreofficeplugin-ooo.zip</sourceFile>
<destinationFile>${project.build.outputDirectory}/final-version.oxt</destinationFile>
</configuration>
</execution>
</executions>
</plugin>
and in case you want to copy instead of rename, use the copy goal instead of the rename goal.
You don't need to use antrun to rename the output file. Just use the tag finalName of the assembly plugin to rename the output file.
<?xml version="1.0" encoding="UTF-8"?>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>assembly</id>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
<configuration>
<archive>
<manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
</archive>
<descriptors>
<descriptor>src/main/assembly/ooo-jar.xml</descriptor>
<descriptor>src/main/assembly/ooo.xml</descriptor>
</descriptors>
<finalName>final-version.oxt</finalName>
</configuration>
</execution>
</executions>
</plugin>

How can I combine jars using Maven?

The following snippet creates 2 JARS:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>build-dependency</id>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>dependency</classifier>
<classesDirectory>${project.build.directory}\dependency</classesDirectory>
<includes>
<include>${dependancyInclude}</include>
</includes>
</configuration>
</execution>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>module</classifier>
<classesDirectory>${project.build.directory}\classes</classesDirectory>
</configuration>
</execution>
</executions>
</plugin>
I would like to combine these two JARs into one using the assembly plugin, currently I have the following:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2</version>
<executions>
<!-- Combine all the JARs in the /target folder into one JAR -->
<execution>
<id>make-assembly</id>
<phase>compile</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<attach>true</attach>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>${project.artifactId}-${project.version}</finalName>
<appendAssemblyId>true</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
Currently only one of the two JARS in included in the final JAR that is created by the assembly plugin.
If I understand you correctly, you actually want to merge the jar files into one big jar file. This can be achieved using the maven-shade-plugin (instead of the maven-assembly-plugin).
For example:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.0</version>
<configuration>
<!-- Put your configuration here, if you need any extra settings.
(You should be okay with the defaults). -->
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>

Resources