Maven: creating an archive (zip or jar) file containing files outside the project? - maven

I'm currently working on a Maven based project in which I have unit tests that generate PDF files in, let's say "C:/result" directory (I definitely don't want these files to be created within any directory from my project).
Using the maven-assembly plugin, would it be possible to create an archive file (preferably zip or jar) that gathers the files that are thus generated in "C:/result"?
Basically, my goal would be to upload this archive as a regular artefact onto a Nexus server, every time I deploy a new version of my project.
Thx in advance.

You'd need to define an assembly descriptor similar to:
<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>bin</id>
<formats>
<format>zip</format>
</formats>
<baseDirectory>C:/</baseDirectory>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>results</directory>
<outputDirectory>./doc</outputDirectory>
</fileSet>
</fileSets>
</assembly>
Save e.g. as src/assembly/bin.xml, it will be referred from the pom.
The pom would contain in the build section:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>out</id>
<configuration>
<descriptors>
<descriptor>src/assembly/bin.xml</descriptor>
</descriptors>
</configuration>
<phase>verify</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
see for more information maven assembly documentation.

Related

In Maven, can I pack JEE project in Zip archive together SQL scripts?

Hello everyone first of all,
Suppose I have a JEE project contents some modules and a 'sql' folder inside:
I build all modules to a 'integration-version.ear' archive file.
There is the content of my target:
Using Maven, can I build something like this:
There is the ‘3.15’ folder includes some SQL files from the same folder of my project.
Is it possible? What is configuration of Maven needed?
Note:
The original question was like: "In Maven, can I pack JEE project in RAR archive together SQL scripts?" but now the question is changed because RAR is a proprietary format and each tools that create RAR files must be commercially licensed.
Solution:
Using build-helper-maven-plugin
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>parse-version</goal>
</goals>
<configuration>
<propertyPrefix>parsedVersion</propertyPrefix>
</configuration>
</execution>
...
Using maven-assembly-plugin
pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<inherited>false</inherited>
<configuration>
<descriptors>
<descriptor>${project.basedir}/../assembly.xml</descriptor>
</descriptors>
</configuration>
...
assembly.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">
<id>release</id>
<baseDirectory>${file.separator}</baseDirectory>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.basedir}/../sql/migrates/${parsedVersion.majorVersion}.${parsedVersion.minorVersion}</directory>
<outputDirectory>/scripts-${parsedVersion.majorVersion}.${parsedVersion.minorVersion}</outputDirectory>
<includes>
<include>**/*.sql</include>
</includes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputFileNameMapping>${build.finalName}.${packaging}</outputFileNameMappin>
<includes>
<include>*:ear</include>
</includes>
</dependencySet>
</dependencySets>
</assembly>
There are 2 Maven plugins which can do that: maven-assembly-plugin and maven-rar-plugin
If you want to use maven-rar-plugin, i would create a new module with packaging rar and configure the plugin.
I personally think maven-assembly-plugin is easier to use.

Creating multiple zip files with assembly plugin - no assembly descriptors found

In addition to creating a war file in my pom (which works fine) I'm trying to create a couple of ZIP files that include only some of my content. Previously I had just one zip file and created my own assembly descriptor using the zip format, and this worked fine. When I wanted to add a second zip file with different content, I had to change my plugin to multiple executions. Now when I run it I get an error message that says no assembly descriptors found and that the build failed.
What's funny is that it actually is doing everything it needs to do. It creates the war file. It creates the two zip files, even though it says it failed to do so. But this maven build is one of many run by an automated deployment process, and if it gets an error message, that's not going to fly.
This is the plugin section of my pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>zip-metricsscnapshot</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<outputDirectory>src/main/webapp/resources</outputDirectory>
<finalName>metricssnapshot</finalName>
<descriptors>
<descriptor>src/main/assembly/zip.xml</descriptor>
</descriptors>
</configuration>
</execution>
<execution>
<id>zip-metricdetails</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<outputDirectory>src/main/webapp/resources</outputDirectory>
<finalName>metricdetails</finalName>
<descriptors>
<descriptor>src/main/assembly/details-zip.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
Solved
addendum:
I run maven with clean yuicompressor:compress assembly:assembly package.
My src/main/assembly directory contains two files, zip.xml and details-zip.xml. They are mostly identical except for the element and the filesets. Here's the zip.xml file:
<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>widget</id>
<baseDirectory>/</baseDirectory>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<outputDirectory>/</outputDirectory>
<directory>src/main/webapp/resources/metricssnapshotwidget</directory>
<excludes>
<exclude>**/metricssnapshotwidget.js</exclude>
<exclude>**/metricssnapshotwidget.css</exclude>
</excludes>
</fileSet>
</fileSets>
</assembly>

Mule: building embedded application with Maven

Is there a way to build a Mule application as an embedded stand-alone jar using Maven (possibly with a help of Mule maven plugin)?
How the pom should be configured if so?
The MuleSoft documentation says that you can create an embedded app by adding all the dependencies manually. But I hope there is a way to automate this task.
Thank you,
You can create a Maven application and depend only on org.mule.distributions:mule:jar:embedded:3.7.0 but this won't create an app with all Mule classes inside of it, which I think is a useful approach (though I don't understand your scenario).
If you need all dependencies to be packaged in your application bundle you can use Maven Assembly Plugin to include all dependencies in a zip file. First add the plugin configuration to your pom like this:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
And add an assembly.xml file in your project root directory containing this:
<assembly>
<id>application-name</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<scope>runtime</scope>
<fileMode>0644</fileMode>
</dependencySet>
</dependencySets>
</assembly>

Installing MAVEN build dependencies [OSGi bundles] with Maven in to Adobe CQ

We have an CQ REST API project to share content with our consumers. The bundle is created with osgi jax-rs connector. We are still in incubation state, but it seems the integration is working fine.
The issue we are facing is the "osgi jax-rs connector" project requires some bundles (osgified ones, provided by the project) to be installed in CQ. Currently we are doing this manually, using the felix console.
Can we automate the process using MAVEN (we can install the bundle which we created using MAVEN).
Any pointers to this would be helpful.
San
If you also have a UI package, e.g. a ZIP that gets installed with the maven-vault-plugin you can place JAR files in any folder. I usually call it install:
/apps/myproject/install
There is a handler in CQ that recognizes JAR files in a content package and installs them to OSGi.
If you are building a zip package to install you can have the bundles installed with maven and not include the dependencies in your project adding additional space to your SCM.
To do this you can use the maven-assembly-plugin plugin.
For example include this in your pom.xml:
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/zip.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
And then in you zip.xml you would create an assembly file like this:
<?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>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${basedir}/src/main/content/jcr_root</directory>
<outputDirectory>jcr_root</outputDirectory>
<excludes>
<exclude>**/.DS_Store</exclude>
</excludes>
<filtered>false</filtered>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>jcr_root/apps/myapp/install</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<scope>compile</scope>
<includes>
<include>com.my.groupid:my-maven-artifact-id</include>
</includes>
<useStrictFiltering>true</useStrictFiltering>
</dependencySet>
</dependencySets>
</assembly>
You can adjust the dependency set and file set settings as needed. Obviously I made up an AEM app name and maven artifact.

How to package an assembly in Maven by moving resources out of the war?

Our project is set up to package a WAR and this works great for local development:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>package-war</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
...
Included in this war, is a mapper file and logback file used by the application, which lives under: src/main/resources/logback.xml and /src/main/resources/mappers/our-mapper.xml.
In building the war, these files get sent where we expect and want them to be, under our.war/WEB-INF/classes/logback.xml and our.war/WEB-INF/classes/mappers/our-mapper.xml
When we package our assembly to hand off for deployment, we need to have these files separated out of the war, which we do using our assembly descriptor:
<assembly>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<files>
<file>
<source>target/${build.finalName}.war</source>
<outputDirectory>${tomcat-instance}/webapps</outputDirectory>
</file>
<file>
<source>src/main/resources/logback.xml</source>
<outputDirectory>${tomcat-instance}/resources/</outputDirectory>
</file>
</files>
<fileSets>
<fileSet>
<directory>src/main/resources/mapper</directory>
<outputDirectory>${tomcat-instance}/resources/mapper/</outputDirectory>
<includes>
<include>**/*</include>
</includes>
</fileSet>
</fileSets>
</assembly>
And configured in Maven with:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/main/resources/our-assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
This constructs the assembly, but my concern is the logback.xml and mappers/our-mapper.xml files are still contained inside the war which is contained inside the assembly.tar.gz.
Is it possible to keep the configuration where the war outside the assembly contains the .xml files as it already does, but also have the war that goes inside the assembled .tar.gz assembly exclude these same files so they don't appear twice on the classpath?
Thanks in advance for any help.
You've probably found a solution or workaround to this problem now - however, I was intrigued to know whether this was possible so I created a small test project with the same file structure as you described.
I managed to get this project to build out the two war files - with the one outside the assembly containing the .xml files, and the one inside the assembly excluding the .xml files. I achieved this by creating an additional assembly that builds a second temporary war file which explicitly excludes the .xml files. Then the existing assembly includes this new war file, rather than the default war.
The new assembly war-assembly.xml looks like this:
<assembly>
<id>temp</id>
<formats>
<format>war</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>target/${build.finalName}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>**/*</include>
</includes>
<excludes>
<exclude>**/logback.xml</exclude>
<exclude>**/mapper/*</exclude>
</excludes>
</fileSet>
</fileSets>
</assembly>
The existing assembly is then modified to include the new war file:
<assembly>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<files>
<file>
<source>target/${build.finalName}-temp.war</source>
<outputDirectory>${tomcat-instance}/webapps</outputDirectory>
<destName>target/${build.finalName}.war</destName>
</file>
<file>
<source>src/main/resources/logback.xml</source>
<outputDirectory>${tomcat-instance}/resources/</outputDirectory>
</file>
</files>
<fileSets>
<fileSet>
<directory>src/main/resources/mapper</directory>
<outputDirectory>${tomcat-instance}/resources/mapper/</outputDirectory>
<includes>
<include>**/*</include>
</includes>
</fileSet>
</fileSets>
</assembly>
And then the pom.xml is modified to reference the new assembly before the existing assembly.
<descriptors>
<descriptor>src/main/resources/war-assembly.xml</descriptor>
<descriptor>src/main/resources/our-assembly.xml</descriptor>
</descriptors>
That seemed to work ok. It did leave the -temp war file lying around in the target folder which could potentially be removed to save any confusion.

Resources