Maven WAR Overlays - including unpacked dependencies - maven-2

I'm trying to set up a project using Maven and am a bit stuck with how to include some third party dependencies that need to be included unpacked in the resultant war file.
My project contains some custom ColdFusion code and includes Java dependencies, including ColdFusion packaged as a war file. I then am attempting to include some 3rd party ColdFusion code, which I've installed in my maven repository packaged as a jar, but I actually want to deploy it unpacked in the resultant war file. It's this unpacking of the 3rd party libraries that I'm stuck with. I'd really like this to be done BEFORE the war is build, so that I can use war:exploded during the development.
Currently my pom.xml looks something like this:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-webapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-webapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- This is the war overlay -->
<dependency>
<groupId>com.adobe.coldfusion</groupId>
<artifactId>coldfusion</artifactId>
<version>9.0.1</version>
<type>war</type>
<scope>runtime</scope>
<optional>false</optional>
</dependency>
<!-- This is the 3rd party ColdFusion dependency -->
<dependency>
<groupId>org.corfield</groupId>
<artifactId>fw1</artifactId>
<version>1.2RC2A</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>my-webapp</finalName>
</build>
</project>
I've kind of got it doing what I want by modifying the build section as follows:
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.corfield</groupId>
<artifactId>fw1</artifactId>
<version>1.2RC2A</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}/${project.artifactId}</outputDirectory>
<includes>**/*.cfc</includes>
</artifactItem>
</artifactItems>
<includes>**/*.cfc</includes>
<outputDirectory>${project.build.directory}/${project.artifactId}</outputDirectory>
<overWriteReleases>false</overWriteReleases>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
The problem with this is that the package:unpack happens after the war is built, so the unpacked dependecies are not in the resulting war file.
I have also tried a few things with the assembly plugin and I came fairly close also by using something like this:
<assembly>
<id>${project.artifactId}</id>
<formats>
<format>war</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<includes>
<include>org.corfield:fw1</include>
</includes>
<unpack>true</unpack>
<outputDirectory>/</outputDirectory>
</dependencySet>
<dependencySet>
<excludes>
<exclude>org.corfield:fw1</exclude>
</excludes>
<unpack>false</unpack>
<outputDirectory>/WEB-INF/lib</outputDirectory>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>src/main/webapp</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</assembly>
However that created a second war file named something like my-webapp-1.0-SNAPSHOT-my-webapp.war as well as the my-webapp-1.0-SNAPSHOT.war. The my-webapp-1.0-SNAPSHOT-my-webapp.war also included my-webapp-1.0-SNAPSHOT.war
At the end of the day I want my resultant war to look like:
org
|-- corfield
|-- framework.cfc
WEB-INF
|-- lib
|-- web.xml
index.cfm
(in reality there's a lot more to it than that, but that's enough to illustrate the point hopefully)
I think I'm pretty close but I'm just missing something I need. I'd be extremely grateful for any assistance.

For the maven-dependency-plugin approach, bind the unpack goal on the prepare-package phase (Maven 2.1+) instead of package.
References
Lifecycle Reference

Have you looked at the overlay option with maven-war-plugin? It esstentially allows you to unpack the ColdFusion war on top your site layout. I've done this with a combination of ColdFusion 9.x and Struts2. Doing a ColdFusion only overlay should make it real easy. You may want to create the ColdFusion war first. You can also then control the neo-*.xml files from your Maven project as well.

Related

Maven – Modules and moduleSet VS dependency and dependencySets

We have installation folder that we use with maven to pack a release up,
This Installation folder has some static files, and a pom.xml
The build goal is to copy the static files to the target installation folder and some zip artifacts from the repository – expand them and put them in the target folder under /unzipped.
installation folder:
/installation_folder
pom.xml
/some_files
/file1
/file2
Target folder should be like:
/target
/installation_files
/some_files
/file1
/file2
/unzipped
/prj1 - unzipped artifact prj1 from the repository
/prj2 - unzipped artifact prj2 from the repository
On this “installation pom” - I have a reference to assembly xml; I am able to copy the static files - and get artifacs from the repository,
The question is – to copy the zip from repository and expand them in the target/unzipped folder
should I use Modules and moduleSet or dependency and dependencySets?
Should the pom.xml + assembly.xml look like:
project.group
installation_project
pom
<modules>
<module>prj1</module>
<module>prj2</module>
</modules>
...
and assembly.xml:
<moduleSets>
<moduleSet>
<includes>
<include>*:*</include>
</includes>
<binaries>
<unpack>true</unpack>
</binaries>
</binaries>
</moduleSet>
Or should it look like this:
<project>
<groupId>project.group</groupId>
<artifactId>installation_project</artifactId>
<packaging>pom</packaging>
<dependencies>
<dependency>
<artifactId>prj1</artifactId>
<groupId>gruop_id</groupId>
<version>1.0-SNAPSHOT</version>
<type>zip</type>
</dependency>
<dependency>
<artifactId>prj2</artifactId>
<groupId>gruop_id</groupId>
<version>2.0</version>
<type>zip</type>
</dependency>
</dependencies>
...
and assembly.xml:
<dependencySets>
<dependencySet>
<outputDirectory>installation_files/unzipped/</outputDirectory>
<outputFileNameMapping>${artifact.artifactId}</outputFileNameMapping>
<includes>
<include>*:*:zip</include>
</includes>
<unpack>true</unpack>
</dependencySet>
</dependencySets>
Thank you!
Another way would be to just maven dependency plugin, with goal=unpack.
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId></groupId>
<artifactId></artifactId>
<version></version>
<type></type>
<overWrite></overWrite>
<outputDirectory></outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
Another way is to use an assembly plugin but i find that quite cumbersome and is usually meant for more complex assembly creation than simple unzipping/zipping.

Referring exploded war dependency in maven antrun plugin

I have a dependency declared as follows
<dependency>
<groupId>com.abc.webapp</groupId>
<artifactId>mywebapp</artifactId>
<version>1.3.2</version>
<type>war</type>
</dependency>
This war file which is part of an ear is exploded in the target directly and I need to copy a file(version.properties) to the WEB-INF/classes folder of this exploded war directory (target/mywebapp-1.3.2.war/). How can I refer to this folder in the antrun plugin without any hardcoding? Thanks in advance.
I'm going to cheat a bit and ask that you declare the dependency like this, with references to properties:
<dependency>
<groupId>${war.groupId}</groupId>
<artifactId>${war.artifactId}</artifactId>
<version>${war.version}</version>
<type>war</type>
</dependency>
and the properties would look something like this in your pom.
<properties>
<war.groupId>com.abc.webapp</war.groupId>
<war.artifactId>mywebapp</war.artifactId>
<war.version>1.3.2</war.version>
</properties>
Now, you still need to change the war GAV if required, but you only need to do it once, and the <dependency> and copy will both refer to the same thing.
Then maybe you could also use the maven-resouces-plugin and its copy-resource goal.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/${war.artifactId}-${war.version}.war/WEB-INF/classes</outputDirectory>
<resources>
<resource>
<directory>where_version.properties_is</directory>
<includes>
<include>version.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

Maven Assembly: include a dependency with a different classifier

I would like to build two different versions of a WAR in Maven (I know that's a no-no, that's just the way it is given the current situation). In the version of a WAR depicted by an assembly, I want to replace a dependency by the same dependency with a different classifier. For example, I was expecting this assembly to work:
<assembly>
<id>end-user</id>
<formats>
<format>war</format>
</formats>
<dependencySets>
<dependencySet>
<excludes>
<exclude>group:artifact:jar:${project.version}</exclude>
</excludes>
<includes>
<include>group:artifact:jar:${project.version}:end-user</include>
</includes>
</dependencySet>
</dependencySets>
</assembly>
This doesn't work, but am I heading in the right direction? I've already read all the pages on the Maven assembly page and the section on the Maven Definitive Guide that seems relevant. Any pointers would be helpful.
Personally, I think that the cleanest solution would be to use two profiles (one of them depending on the artifact with the classifier, the other on the "regular" artifact). But you can indeed achieve what you want with a custom assembly. I just don't think you're heading in the right direction. Here is how I would do it...
First, create a specific project for the assembly and declare both the webapp and the artifact with the classifier as dependencies. Something like this:
<project>
...
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>artifact</artifactId>
<version>${project.version}</version>
<classifier>end-user<classifier>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>zewebapp</artifactId>
<version>${project.version}</version>
<type>war</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/end-user.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- this is used for inheritance merges -->
<phase>package</phase>
<!-- append to the packaging phase. -->
<goals>
<goal>single</goal>
<!-- goals == mojos -->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Then, in your assembly descriptor:
<assembly>
<id>end-user</id>
<formats>
<format>war</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<unpack>true</unpack>
<unpackOptions>
<excludes>
<exclude>**/artifact-*.jar</exclude>
</excludes>
</unpackOptions>
<includes>
<include>*:war</include>
</includes>
</dependencySet>
<dependencySet>
<unpack>false</unpack>
<outputDirectory>WEB-INF/lib</outputDirectory>
<includes>
<include>group:artifact:jar:*:end-user</include>
</includes>
</dependencySet>
</dependencySets>
</assembly>
Basically, this tells the assembly plugin to get the war for zewebapp and to unpack it but to exclude the unwanted artifact while unpacking. Then, the assembly plugin get the artifact with the classifier and place it in WEB-INF/lib (so we substitute it to the original). Finally, the whole thing is packaged as a war.
I tested this on a simplified example, it should work.
The "version" information is not required when specifying artifact coordinates for includes/include*.
This should work:
<assembly>
<id>end-user</id>
<formats>
<format>war</format>
</formats>
<dependencySets>
<dependencySet>
<includes>
<include>group:artifact:jar:end-user</include>
</includes>
</dependencySet>
</dependencySets>
</assembly>
I think the maven assembly documentation for the includes/include* section is incorrect. It says "Artifact coordinatess may be given in simple groupId:artifactId form, or they may be fully qualified in the form groupId:artifactId:type:version[:classifier]." However, from my testing, the "version" is not required. I got the hint from here.
Took me while to find out, thought might be useful to others in the future.

How to create source distribution with self sustainable maven build?

What I want to do is to create source code distribution of my application with all dependencies and burn it on DVD. So that I could build it in 100 years (well, ok, you know what I mean...). No online dependencies on libraries or maven plugins!
I know that Ant would be better for this, but I'm using maven in my project. I'm not going to switch to Ant just for that, I'm asking how to do this with maven. Or, if there is a way how to generate self sustainable Ant build that I could put on DVD that would be great too.
(there is ant:ant plugin but it just generates Ant build.xml that points dependencies to local maven repo)
The approach I've taken is that I wanted to create special local repository that I can put on DVD and then build project with mvn -o -Dmaven.repo.local=repo/on/dvd. I was trying to make such repository with dependency:copy-dependencies anduseRepositoryLayout param set to true. But it doesn't copy freaking maven plugins that my build depends on...
The only way I can think of to include the plugins is to specify a different local repository for the build on the command line and ensure all the dependency sources etc are downloaded, then create an archive including the project's contents and the custom repository.
Here is a pom that downloads the sources and javadocs (it downloads them to the project's target directory, which we exclude from the archive because they will also be in the local repository). The assembly descriptor bundles the project's contents and the local repository into a single (pretty large) archive.
Note the processing is all in a profile because you really don't want this running on every build. If temporary local repository is in the target directory you can easily clean the mess up afterwards with a mvn clean.
To activate the profile do something like the following:
mvn package -Parchive -Dmaven.repo.local=.\target\repo
Here's the pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>name.seller.rich</groupId>
<artifactId>test-archive</artifactId>
<version>0.0.1</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.5</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>archive</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>sources</id>
<phase>pre-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<classifier>sources</classifier>
<failOnMissingClassifierArtifact>false</failOnMissingClassifierArtifact>
<!--the target directory won't be included, but the sources will be in the repository-->
<outputDirectory>${project.build.directory}/sources</outputDirectory>
</configuration>
</execution>
<execution>
<id>javadocs</id>
<phase>pre-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<classifier>javadoc</classifier> <failOnMissingClassifierArtifact>false</failOnMissingClassifierArtifact>
<outputDirectory>${project.build.directory}/javadocs</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/main/assembly/archive.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
And here's the assembly:
<assembly>
<id>archive</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.basedir}</directory>
<outputDirectory>/</outputDirectory>
<excludes>
<exclude>target/**</exclude>
</excludes>
</fileSet>
<fileSet>
<directory>${maven.repo.local}</directory>
<outputDirectory>repo</outputDirectory>
</fileSet>
</fileSets>
</assembly>
Watch this:
Maven Assembly Plugin
Quote from the homepage:
Do you want to create a binary
distribution from a Maven project that
includes supporting scripts,
configuration files, and all runtime
dependencies? You need to use the
Assembly Plugin to create a
distribution for your project.
It's well configurable. I used it especially for making self-running demo versions of web-applications with an embedded jetty server and user documentation.
I don't have a complete answer. Last time I looked at this, I thought that cleaning out the localRepository at the start of the build (or using a separate one) and the running mvn dependency:go-offline.
If you're really keen, you'll also want to bundle maven itself and a JDK into the distribution. This likely takes it out of scope of a pure maven build.

Maven Assembly plugin - Include repo jars

I have a multi module project and I am using assembly plugin to build a tar file. I have included all of my modules in assembly plugin using moduleSets tag.
<moduleSets>
<moduleSet>
<includes>
<include>module1</include>
<include>module2</include>
All of my module jars are assembled into a lib folder. I want to add mysql jar from my local maven repository to the same lib folder that contains all of my other modules. Adding local repository jars in moduleset doesnt seem to work.
[WARNING] The following patterns were never triggered in this artifact inclusion filter:
o 'mysql.mysql-connector-java:mysql-connector-java-version'
How can I include jars from maven repository.
I think that you need to declare your mysql jar in the includes subelement of a dependencySet.
Something like this:
<assembly>
<id>my-assembly</id>
...
<dependencySets>
<dependencySet>
<outputDirectory>/lib</outputDirectory>
<includes>
<include>mysql:mysql-connector-java</include>
</includes>
<unpack>false</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
...
</assembly>
Refer to the Descriptor Format documentation and/or the chapter 12.5.4. dependencySets Section of the Maven Book for more details on this element.
I don't think you need to do anything special, just make sure mysql is listed as dependency in your project and it should work. Same applied to dependencies on modules - just list them as dependencies. Below is typical configuration of assembly plugin.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<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>
I had the same problem, and solved it by changing scope on pom dependency to compile.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>compile</scope>
</dependency>