Referring exploded war dependency in maven antrun plugin - maven-2

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>

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.

Compile Jasper reports from Maven with ant task?

How can I compile jrxml jasper files using Maven and JRAntCompileTask ant task? I tried using a maven plugin for compiling jasper reports files but it's still in beta, and it caused me many problems. I'd like to see the configuration in pom.xml.
How can I compile jrxml jasper files using Maven and JRAntCompileTask ant task?
You can use the custom Ant Tasks with the Maven AntRun Plugin. See the example provided in Using tasks not included in Ant's default jar.
You can try jasperreports-maven-plugin, that way you don't have to use ant, Here is an example.
Here's a complete example that solves problems with Eclipse m2e reporting errors with maven configuration, has reports neatly set in a separate folder and has classpath configured.
Just be sure to put your .jrxml files in src/main/jasperreports folder and you're set - every time you change the report, .jasper files will be regenerated.
pom.xml:
<properties>
<jasperreports.version>5.0.0</jasperreports.version>
</properties>
<build>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jasperreports-maven-plugin</artifactId>
<versionRange>[1.0-beta-2,)</versionRange>
<goals>
<goal>compile-reports</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<!-- Include the generated reports in classpath -->
<directory>target/jasper</directory>
</resource>
<resource>
<!--Folder with .jrxml report files -->
<directory>src/main/jasperreports</directory>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jasperreports-maven-plugin</artifactId>
<configuration>
<!-- Folder where compiled reports will be generated -->
<outputDirectory>${project.build.directory}/jasper</outputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>compile-reports</goal>
</goals>
<phase>generate-sources</phase>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jasperreports-maven-plugin</artifactId>
<version>1.0-beta-2</version>
<exclusions>
<exclusion>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>${jasperreports.version}</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>4.2.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
...

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).

Problems with maven output directory

I'm using almost same POM for both my projects, they are on the same workspace but they are not related at all, they are related however because in both I use spring and jboss. Here is the pom :
<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.springinaction.hello</groupId>
<artifactId>spring-in-action</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>spring-in-action</name>
<url>http://maven.apache.org</url>
<properties>
<jboss.ome>C:\jboss-5.1.0.GA\server\default\deploy</jboss.ome>
<springversion>2.5.3</springversion>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>${springversion}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0</version>
<configuration>
<warName>spring-book</warName>
<outputDirectory>${jboss.ome}</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
What I want to achieve with this POM is that I want to name my war when built spring-book.war and copy it to Jboss location I specified. Now in my first project this works it does exactly what I requested but in other it does not. I changed springversion and jboss home properties variable but everything remains the same, what can I do ? The project builds and all, everything is working perfectly just I don't want to copy everytime in my jboss dir and previously remove the old war, it takes about 20sec on each source code change its a lot
Problem spotted at this line:
<packaging>jar</packaging>
You're not using the right packaging, it should be:
<packaging>war</packaging>
After this change the war plugin should get called and things should work like in the other project :)
You could leave the output directory at its default, and use a profile instead with the maven jboss plugin. It has a hard-deploy target which copies your artifact to the deploy directory. If it's in a profile, you can activate it when (and only when) you want.
Moreover, with the antrun plugin, you can also delete the old war file before copying over the new one (this is useful when the war filename includes the version, but in your case may not be needed).
<profiles>
<profile>
<id>deploy</id>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>remove-old-war</id>
<phase>prepare-package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<delete>
<fileset dir="${jboss.ome}"
includes="*.war"/>
</delete>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jboss-maven-plugin</artifactId>
<executions>
<execution>
<id>redeploy-server</id>
<phase>pre-integration-test</phase>
<goals>
<goal>hard-deploy</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
You can then activate the profile with
mvn -Pdeploy install

Missing aar file in maven2 multi-project build

I'm trying to use maven2 to build an axis2 project. My project is configured as a parent project with AAR, WAR, and EAR modules. When I run the parent project's package goal, the console shows a successful build and all of the files are created. However the AAR file generated by AAR project is not included in the generated WAR project. The AAR project is listed as a dependency of WAR project. When I explicitly run the WAR's package goal, the AAR file is then included in the WAR file.
Why would the parent's package goal not include the necessary dependency while running the child's package goal does?
I'm using the maven-war-plugin v2.1-alpha-2 in my war project.
Parent POM:
<parent>
<groupId>companyId</groupId>
<artifactId>build</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.nationwide.nf</groupId>
<artifactId>parent</artifactId>
<packaging>pom</packaging>
<version>1.0.0-SNAPSHOT</version>
<modules>
<module>ws-war</module>
<module>ws-aar</module>
<module>ws-ear</module>
</modules>
AAR POM:
<parent>
<artifactId>parent</artifactId>
<groupId>companyId</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>companyId</groupId>
<artifactId>ws-aar</artifactId>
<version>1.0.0-SNAPSHOT</version>
<description/>
<packaging>aar</packaging>
<dependencies>...</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-wsdl2code-maven-plugin</artifactId>
<version>1.4</version>
<configuration>...</configuration>
<executions>
<execution>
<goals>
<goal>wsdl2code</goal>
</goals>
<id>axis2-gen-sources</id>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-aar-maven-plugin</artifactId>
<version>1.4</version>
<extensions>true</extensions>
<configuration>...</configuration>
</plugin>
</plugins>
</build>
WAR POM:
<parent>
<artifactId>parent</artifactId>
<groupId>companyId</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>companyId</groupId>
<artifactId>ws-war</artifactId>
<packaging>war</packaging>
<version>1.0.0-SNAPSHOT</version>
<description/>
<dependencies>
<dependency>
<groupId>companyId</groupId>
<artifactId>ws-aar</artifactId>
<type>aar</type>
<version>1.0.0-SNAPSHOT</version>
</dependency>
.
.
.
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1-alpha-2</version>
<configuration>
<warName>appName</warName>
</configuration>
</plugin>
</plugins>
</build>
Thanks,
Joe
I was able to get my maven build working correctly by adding the following plugin to the ws-war pom file:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/${project.build.finalName}/WEB-INF/services
</outputDirectory>
<includeArtifactIds>
ws-aar
</includeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
Have you tried using the "type" element in your dependencies? For example:
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-b</artifactId>
<version>1.0</version>
<type>aar</type>
</dependency>
Its hard to say for sure what your problem is without seeing your actual pom files.
Update:
What happens if, from the parent project, you run:
mvn clean install
Does "install" have any different behavior than "package" as far as your problem is concerned?
Do you see the .aar file in your local maven repository (~/.m2/repository/com/mycompany/.../)?
As a side note, i've never been very happy with the maven war plugin. I've always ended up using the maven assembly plugin. It just seems to work better and is more consistent. Also, make sure you are using the latest version of maven (2.0.9). I spent half a day fighting a similar problem which was fixed in the latest version.