Maven in multimodule web project: how to put sibling modules output to WEB-INF/classes and not into WEB-INF/lib as JARs? - maven-2

I have a multi-module Maven project.
By default when I build a web module, all sibling modules of type JAR it depends on are copied to WEB-INF/lib folder. I want output of sibling modules to be placed in WEB-INF/classes folder without packaging to JAR.
More general question may be: how to keep sibling modules' configuration files out of JARs so that they can be edited after deployment easily?

You could use an overlay, although that requires that the sibling be of type war rather than jar. There's also using the dependency plugin to unpack the jar, but it will only unpack the version in your local repository, not the one you just packaged.
As for your 'more general' question, there's the excludes tag for the jar plugin.

In case if somebody is interested, I found this solution. I had exactly the same issue.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<packagingExcludes>WEB-INF/lib/MYPACKAGE_TO_EXCLUDE.jar</packagingExcludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-MYPACKAGE_TO_EXCLUDE</id>
<phase>compile</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>MYPACKAGE_TO_EXCLUDE</artifactId>
<version>${project.version}</version>
<type>jar</type>
<outputDirectory>${project.build.directory}/classes
</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Related

How can I add a specific jar only in a specific folder with Maven?

i have a maven project using the maven soapui plugin.
Unfortunately this one in my case need an external jar for jdbc for
some groovy scripts.
And according to their documentation, you have to add a repertory called "ext".
And after a dig in their code, this path is hardcoded in their plugin source.
You cant specify it.
So i have to add it directly to a subdirectory of MyProject :
Myproject/ext/postgresql.X.X.jar
I dont want to commit a jar in my project.
Is there a way to tell to maven to pickup a specific jar& add it to a specific directory ?
Thanks in advance for your answer.
You could use maven-dependency-plugin configured in your pom file to copy the artifact into that location during the generate-resources phase, something like this:
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>copy-postgresql-artifact</id>
<phase>generate-resources</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/ext</outputDirectory>
<!-- if you need to rename the jar -->
<destFileName>postgresql.X.X.jar</destFileName>
<artifactItems>
<artifactItem>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>X.X</version>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>

adding artifacts to standard maven deploy

I was hoping someone could help me with maven deployments (typically run through the release plugin).
I want to deploy files other than just the packaged jar to the repo upon release, such as specific instruction documents and generated SQL files.
It would be good if i did not have to use deploy:deploy-file for each one. it would be best if I could just add each file to a list within my POM file and it would be picked up automatically for me upon release.
Either use the Maven Assembly Plugin to package them into an assembly that will get installed/deployed.
Or use the attach-artifact goal of the build-helper plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>some file</file>
<type>extension of your file</type>
<classifier>optional</classifier>
</artifact>
...
</artifacts>
</configuration>
</execution>
</executions>
</plugin>

Maven2: How do I generate a file that contains the names of the project's dependencies?

I would like to place the names of the dependencies in a text file that is distributed inside a package that is built with Maven.
I am planning to use the maven assembly plugin to generate the tarball package, and use filtering to put the names in the text file.
The only problem is, I don't know how to reference the dependencies in the first place.
You don't need to use filtering for that, use the Maven Dependency plugin and its a dependency:tree goal to display the dependency tree for this project. Set an output file with the... outputFile optional parameter. So the configuration might look like:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>tree</id>
<phase>prepare-package</phase>
<goals>
<goal>tree</goal>
</goals>
<configuration>
<outputFile>${project.build.outputDirectory}/dep.txt</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
Running the package phase would generate the dependency tree in target/classes/dep.txt and package it in the artifact. Adapt it to suit your needs.
You can use the maven-dependency-plugin dependency:tree to output the tree of dependencies into a file.
mvn dependency:resolve seems to be what you are looking for. Put following plugin configuration to your POM file:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>list-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>resolve</goal>
</goals>
<configuration>
<outputFile>dependencies.txt</outputFile>
</configuration>
</execution>
</executions>
</plugin>
It will produce file dependencies.txt with content similar to:
The following files have been resolved:
am:amagent:jar:1.0:system
am:amclientsdk:jar:1.0:system
aopalliance:aopalliance:jar:1.0:compile
asm:asm:jar:2.2.3:compile
com.sun.jdmk:jmxtools:jar:1.2.1:compile
com.sun.jmx:jmxri:jar:1.2.1:compile
com.sun.xml.bind:jaxb-impl:jar:2.1.12:compile
com.sun.xml.fastinfoset:FastInfoset:jar:1.2.2:compile
com.sun.xml.messaging.saaj:saaj-impl:jar:1.3.2:compile
commons-lang:commons-lang:jar:2.3:compile
commons-logging:commons-logging:jar:1.1:compile
dom4j:dom4j:jar:1.6.1:compile
javax.activation:activation:jar:1.1:provided
javax.jms:jms:jar:1.1:compile
javax.mail:mail:jar:1.4:compile
javax.xml.bind:jaxb-api:jar:2.1:compile
javax.xml.soap:saaj-api:jar:1.3:compile
junit:junit:jar:4.4:test
log4j:log4j:jar:1.2.15:compile

Maven copy project output into other project resources

There are two projects:
1) applet project that outputs jar file
2) web app project that should host the jar file.
After (1) finished building, the applet jar file should be copied into the webapp folder of (2). The purpose is that (2) will host the applet (1) on the Internet.
A lot of examples explain how to use another project as a library dependency. Other examples, show how to use ant plugin to copy files. I am unsure on how to properly set this up, so that 'mvn install' on the parent project will do the copying at the right time.
I would declare the applet as a dependency of the webapp, copy it to the webapp just before packaging using the Dependency plugin and its copy goal. The whole solution might looks like this:
<project>
...
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>my-applet</artifactId>
<version>${project.version}</version>
<scope>provided</scope> <!-- we don't want the applet in WEB-INF/classes -->
</dependency>
...
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>copy</id>
<phase>prepare-package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>my-applet</artifactId>
<version>${project.version}</version>
<outputDirectory>${project.build.directory}/${project.build.finalName}</outputDirectory>
<destFileName>the-applet.jar</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
</project>
Declaring the applet as dependency is for the reactor build order (but I'm not 100% sure it is required).

How to make jetty-maven-plugin deploy a war that is retrieved from a repository?

I'm setting up an integration test module for a good sized web project. The integration test module is separated from the web project itself, and it has it's own pom.
The idea is to use the maven-soapui-plugin to send requests and verify the response. Setting up the soapui-plugin is no hassle. However, I'm having trouble with figuring out how I can tell the jetty-maven-plugin to deploy a war from a remote repository.
If I have understood correctly, the jetty-maven-plugin has a property called '<webApp>/<webApp>' which lets me specify the war file to deploy. The problem is that the war file is not present in the module itself.
I have heard that I can use the maven assembly plugin to retrieve the war from a repository via the projects artifactId, but I am yet to figure out how I would go about doing so.
Here's a summary of what I want:
Retrieve a specific war from a repository or the like, in example via its artifactId.
Deploy this war to the jetty-maven-plugin (goal deploy-war?)
get maven-soapui-plugin to run tests and report the results back in the integration-test phase.
I am pretty sure I've got step 3 covered, but I am very unsure how to achieve step 1 and 2.
Any help is greatly appreciated
It is maybe possible to use dependency:copy to retrieve the war, unpack it and to get the whole thing working with the maven jetty plugin, but this would be hacky and kinda ugly. A cleaner solution would be to use the Maven Cargo plugin and this is my suggestion. Below, a sample POM showing how to retrieve a WAR artifact using its coordinates and how to deploy it on an embedded Jetty container using Cargo:
<dependencies>
<dependency>
<groupId>war group id</groupId>
<artifactId>war artifact id</artifactId>
<type>war</type>
<version>war version</version>
</dependency>
...
</dependencies>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<configuration>
<!-- Container configuration -->
<container>
<containerId>jetty6x</containerId>
<type>embedded</type>
</container>
<!-- Configuration to use with the container or the deployer -->
<configuration>
<deployables>
<deployable>
<groupId>war group id</groupId>
<artifactId>war artifact id</artifactId>
<type>war</type>
<properties>
<context>war context</context>
</properties>
</deployable>
</deployables>
</configuration>
<!-- Don't wait, execute the tests after the container is started -->
<wait>false</wait>
</configuration>
<executions>
<execution>
<id>start-container</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-container</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
...
</build>
Finally, just bind the soapui plugin on the integration-test phase.
I'm doing the same thing, John, but I took a different approach with the Jetty plugin. I think the end result is the same. I'm developing an integration-test suite to run against several web service WARs. I'm using dependency:copy in the package phase and then a list of <contextHandler/>s configured for maven-jetty-plugin:
<project>
…
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-wars</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/wars-to-be-tested</outputDirectory>
<stripVersion>true</stripVersion>
<artifactItems>
…
<artifactItem>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<version>version</version>
<type>war</type>
</artifactItem>
…
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>7.1.3.v20100526</version>
<configuration>
…
<contextHandlers>
…
<contextHandler implementation="org.mortbay.jetty.plugin.JettyWebAppContext">
<war>${project.build.directory}/wars-to-be-tested/artifactId.war</war>
<contextPath>/context</contextPath>
</contextHandler>
</contextHandlers>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>stop</goal>
<goal>run</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I would prefer to declare the various wars as dependencies and then use dependency:copy-dependencies to set up the wars-to-be-tested directory; this would allow the Maven reactor to figure out that it needs to build my integration-test module after the wars it'll be testing. The problem I ran into was that the Jetty plugin thought that I wanted to "overlay" all of the wars that were listed as dependencies (a concept that I'd never even heard of until I saw it happen); I don't know if allowing that to happen would have hurt anything, but I didn't like it, so I went with the dependency:copy method.
This is just an alternative to using Cargo. I'll be looking into that myself, but I just wanted to provide another way of doing it.