maven run test for each classifier - maven

I have a maven war project that configure to build for multiple envs using multiple classifier. Here is how i configure the plugin for dev and test envs:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<executions>
<execution>
<id>package-dev</id>
<phase>package</phase>
<configuration>
<classifier>dev</classifier> <webappDirectory>${project.build.directory}/${project.build.finalName}-dev</webappDirectory>
<webResources>
<resource>
<directory>src/env/dev</directory>
</resource>
</webResources>
</configuration>
<goals>
<goal>war</goal>
</goals>
</execution>
<execution>
<id>package-test</id>
<phase>package</phase>
<configuration>
<classifier>test</classifier> <webappDirectory>${project.build.directory}/${project.build.finalName}-test</webappDirectory>
<webResources>
<resource>
<directory>src/env/test</directory>
</resource>
</webResources>
</configuration>
<goals>
<goal>war</goal>
</goals>
</execution>
</executions>
However, I want my test to execute on each of the classifier based on the property files that are overridden for each classifier to make sure all the test will pass in each environment. How can I do that?
Thanks,
Sean

Related

Maven avoid certain files from being copied to test-classes

I'm trying to avoid that certain file types don't end copied in target/test-classes. The code I have right now in my pom file but it's not working is:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>default-testResources</id>
<phase>process-resources</phase>
<goals>
<goal>testResources</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/test-classes
</outputDirectory>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/*.nc</exclude>
</excludes>
</testResource>
</testResources>
</configuration>
</execution>
</executions>
</plugin>
I was finally able to do what I wanted with:
<execution>
<id>default-testResources</id>
<phase>test-compile</phase>
<goals>
<goal>testResources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${project.basedir}/src/test/resources </directory>
<excludes>
<exclude>**/*.nc</exclude>
</excludes>
</resource>
</resources>
</configuration>
</execution>
With this code, which is very similar to the one I posted in the question, and by changing the maven version in my eclipse settings from 3.3.3 to 3.3.9 I'm able to avoid the files that I wanted to be copied to target/test-classes

Move a config file into etc folder(of Karaf) when a (Maven)bundle is deployed

I want to move a cfg file into the etc folder of karaf whenever a bundle is deployed.
the cfg file is in under src/main/resource .i tried the following in the pom but its not working.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>deploy</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>Using env.test.properties</echo>
<copy file="src/main/resources/test.cfg" tofile="${env.KARAF_HOME}/etc/test.cfg"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
How can i do it ?
One of the solution could be:
- put your test.cfg file in a more specific folder. (eg: src/main/resources/cfg)
- use the maven resources plugin
This is a working example based on the maven phase generate-resources (replace that phase by deploy in your case):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-to-karaf</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main/resources/cfg</directory>
<filtering>true</filtering>
</resource>
</resources>
<outputDirectory>D:\apache-karaf-3.0.1\etc\</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

Creating directory structure for application with maven

How can I tell maven to create a particular directory structure and to put jar files in certain places?
For example I want to put put some jars in a directory called plugins which are loaded at runtime.
Is that possible? Or am I missing the principles behind maven?
It looks like your jars is your project dependency, so to copy all dependencies at plugins folder use next:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-alldeps</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/plugins</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<excludeTransitive>false</excludeTransitive>
</configuration>
</execution>
</executions>
</plugin>
When your project use jars like resources, use maven resource section or next plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-config-files</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/plugins/</outputDirectory>
<resources>
<resource>
<filtering>false</filtering>
<directory>${project.basedir}/jars/</directory>
<includes>
<include>file1.jar</include>
<include>file5.jar</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

Attaching only a contents of the folder to the jar

I am using maven-jar-plugin to generate a sources jar.
I have the following folder structure:
folder/foo/baz/A.java
folder/bar/baz/B.java
I want the sources jar to contain the following:
baz/A.java
baz/B.java
I am using the following configuration:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>folder</classesDirectory>
<includes>
<include>foo/**</include>
<include>bar/**</include>
</includes>
<finalName>sources</finalName>
</configuration>
</execution>
</executions>
</plugin>
But this creates the jar like this:
folder/foo/baz/A.java
folder/bar/baz/B.java
How can i modify the code to get my desired structure in the jar?
I could not solve this with the maven-jar-plugin, i had to use maven-resources-plugin, too:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/sources/</outputDirectory>
<resources>
<resource>
<directory>folder/foo/</directory>
</resource>
<resource>
<directory>folder/bar/</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>${project.build.directory}/sources</classesDirectory>
<includes>
<include>**/*</include>
</includes>
<finalName>sources</finalName>
</configuration>
</execution>
</executions>
</plugin>

Build multiple artifacts with different classifiers at once

W want my maven project to produce three artifacts with different classifiers at once. I know that I can produce it with modules etc. This is actually a resources project that I want to produce configuration for DEV, STAGE and PROD environment.
What I want to have is to run mvn:install once and have my.group:resources:1.0:dev, my.group:resources:1.0:stage and my.group:resources:1.0:prod in my repo.
This can be done without profiles if you specify multiple plugin executions and resource filtering.
Create a properties file for each version in ${basedir}/src/main/filters (e.g. prod.properties, dev.properties) holding appropriate values for each environment.
Turn on filtering for your resources:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
Now add the resource plugin executions. Note the different filter file and output directory.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>default-resources</id>
<phase>process-resources</phase>
<goals>
<goal>resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}/dev</outputDirectory>
<filters>
<filter>${basedir}/src/main/filters/dev.properties</filter>
</filters>
</configuration>
</execution>
<execution>
<id>prod</id>
<phase>process-resources</phase>
<goals>
<goal>resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}/prod</outputDirectory>
<filters>
<filter>${basedir}/src/main/filters/prod.properties</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
Finally, the jar plugin; note classifier and input directory:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>dev</classifier>
<classesDirectory>${project.build.outputDirectory}/dev</classesDirectory>
</configuration>
</execution>
<execution>
<id>jar-prod</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>prod</classifier>
<classesDirectory>${project.build.outputDirectory}/prod</classesDirectory>
</configuration>
</execution>
</executions>
</plugin>
Running mvn clean install should produce the properly filtered resources in artifacts with dev and prod classifiers like you want.
In the example, I used execution IDs of default-resources and default-jar for the dev versions. Without this you would also get an unclassified jar artifact when you build.
Just an FYI - put the version number in there to make sure you have the version supporting custom filters. In maven 3 I set mine up like this for example. Without version it didn't work.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
...
</plugin>

Resources