Maven Profiles Jar issues - maven

I have dev/XYZ.properties file, i am building a jar file using maven -P dev install. How can I place the property file in child.jar/META-INF/XYZ.properties file

You have to execute the maven-resources-plugin:copy-resources goal for your profile dev and bind it with the appropriate phase (process-resources seems the best phase to me). Here is how to define your profile dev :
<profile>
<id>dev</id>
...
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes/META-INF</outputDirectory>
<resources>
<resource>
<directory>${basedir}/dev</directory>
<includes>
<include>XYZ.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Related

How to copy file from Parent POM into Maven Project

I want to have a Parent Maven project that has two files.
src/license/LICENSE.txt
src/license/NOTICE.txt
When I build my main Project, that references it as a Parent POM, I want those files to be included in the produced JAR.
I know I can use...
<build>
<resources>
<resource>
<targetPath>META-INF</targetPath>
<directory>src/license</directory>
<includes>
<include>NOTICE.txt</include>
<include>LICENSE.txt</include>
</includes>
</resource>
</resources>
</build>
But this will only work when the files are located in main Project, not when they are in the Parent project.
I found a solution to this.
First, create a new repo to hold the items you want to be copied in.
The POM of this should contain this (GAV details are com.jeff:base-pom-license)...
<build>
<plugins>
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<goals>
<goal>bundle</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>**/*.txt</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
Then in your Base pom add this...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.5</version>
<configuration>
<resourceBundles>
<resourceBundle>com.jeff:base-pom-license:${project.version}</resourceBundle>
</resourceBundles>
</configuration>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
</execution>
</executions>
</plugin>
This will cause the files you have in the new maven repo to be included in all the places that extend the base pom.

Maven does not copy resource files

I have a maven web application project that follows the maven standard directory layout so I have my resource files placed in src/main/resources/.
As this follows the standard I was expecting maven to automatically add the resource files to /WEB-INF/classes of the web application during the build but no files are copied.
I have to add the following lines to the build section of the pom file to get maven to copy the files:
<resources>
<resource>
<directory>src/main/resources/</directory>
</resource>
</resources>
My question is: is this supposed to be necessary?
I was wondering if any of the build section plugins I use could somehow interfere with the copying of the resources. Are there any plugins that are known to do this?
The build section of my pom file is here:
<build>
<resources>
<resource>
<directory>src/main/resources/</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<includes>
<include>${tests.to.include}</include>
</includes>
<excludes>
<exclude>${tests.to.exclude}</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<includes>
<include>**/ITSelenium*.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.12</version>
<executions>
<execution>
<id>add extra source directories</id>
<phase>generate-sources</phase>
<goals><goal>add-source</goal></goals>
<configuration>
<sources>
<source>${project.basedir}/generated/src/main/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add extra test directories</id>
<phase>generate-test-sources</phase>
<goals><goal>add-test-source</goal></goals>
<configuration>
<sources>
<source>${project.basedir}/generated/src/test/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add integration test sources</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${project.basedir}/src/it/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warName>res</warName>
<webResources>
<resource>
<filtering>true</filtering>
<directory>${project.basedir}/src/main/webapp</directory>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.4.0.905</version>
</plugin>
</plugins>
</build>
Any help or information will be much appreciated.

maven override/replace file into WEB-INF/classes

I want to override/replace my spring config xml file with specified location file while package as war.
And I do not want to use filter plugin (filter plugin must to use dolloar placeholder, it will run with error while local deploy), is there any plugin or setting I can use to do this?
project structure as follow:
ROOT
----config
----prd
----spring-servlet.xml
----web.xml
----src
----main
----java
----resources
----spring-servlet.xml
----webapp
....
My pom.xml is like as follow:
...
<profiles>
<profile>
<id>prd</id>
<properties>
<filterDir>prd</filterDir>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webXml>${basedir}/config/${filterDir}/web.xml</webXml>
<webResources>
<resource>
<directory>config/${filterDir}</directory>
<targetPath>WEB-INF/classes/</targetPath>
<includes>
<include>mvc-dispatcher-servlet.xml</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
run maven use :mvn clean package -P prd.
Will work fine if not set tartgetPath, file copied to web root.
You can use copy-resources of maven to do this.
Example maven code should be like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy-web.xml</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>your-target-directory</outputDirectory>
<resources>
<resource>
<directory>source-directory</directory>
<!--You can also mention files too-->
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Read more about copy-resources for correct implementation.

How are non-conflicting configurations resolved if multiple profiles are activated?

Following up this question:
I can't find information on how the union of multiple (active) maven profiles is built, under the condition that they are not conflicting.
Consider the following example:
I want to control how my test cases are run with maven-surefire-plugin. First I need to configure two different environments (jenkins-CI-server) and local. Second I want to enable running different sets of tests.
I would have four profiles:
env_jenkins for running on jenkins
env_local for running on local
testset_A for running some tests
testset_B for running some other tests
Note that for all those profiles I need to define the plugin configuration for the maven-surefire-plugin.
(See below for the concrete xml-configuration)
Then I would like to combine the profiles to run - for example - testset A on jenkins.
My questions:
Is such a behaviour supported by maven?
On which stage does the overriding mentioned in the related question occur? Is the entire plugin configuration overridden (-> my example would not work). Or only really conflicting parts (-> my example would work)?
Does the overriding behaviour depend on the plugin or is it consistent among all maven plugins?
Example pom:
<profiles>
<!-- handle system configurations (e.g. one for jenkins environment, one for local) -->
<profile>
<id>env_jenkins</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<arquillian.launch>jbossas-managed</arquillian.launch>
<jbossHttpPortOverride>8080</jbossHttpPortOverride>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>env_local</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<arquillian.launch>jbossas-managed-jenkins</arquillian.launch>
<jbossHttpPortOverride>${jboss.http.port}</jbossHttpPortOverride>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<!-- define different test sets -->
<profile>
<id>testset_A</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>TestA1.java</include>
<include>TestA2.java</include>
<include>TestA3.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>testset_B</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>TestB1.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
It works using the same inheritance/merging process that works between parent and children POMs. Using the Maven help plugin can confirm this (using Maven 3.3.9):
Order of profile declarations in the POM matters. A plugin configuration inherits the same plugin's configuration declared in an active profile appearing before it in the POM.
The attributes combine.self and combine.children can be used to control the merging.
Example with default settings (without using combine.self or combine.children, i.e. with combine.children="merge" implicitly):
mvn help:effective-pom -Penv_local,testset_A
Effective POM shows merged configurations:
...
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>
<include>TestA1.java</include>
<include>TestA2.java</include>
<include>TestA3.java</include>
</includes>
<systemPropertyVariables>
<arquillian.launch>jbossas-managed-jenkins</arquillian.launch>
<jbossHttpPortOverride>${jboss.http.port}</jbossHttpPortOverride>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
<configuration>
<includes>
<include>TestA1.java</include>
<include>TestA2.java</include>
<include>TestA3.java</include>
</includes>
<systemPropertyVariables>
<arquillian.launch>jbossas-managed-jenkins</arquillian.launch>
<jbossHttpPortOverride>${jboss.http.port}</jbossHttpPortOverride>
</systemPropertyVariables>
</configuration>
</plugin>
...
Example with combine.self="override":
<profile>
<id>testset_A</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration combine.self="override">
<includes>
<include>TestA1.java</include>
<include>TestA2.java</include>
<include>TestA3.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
mvn help:effective-pom -Penv_local,testset_A
Effective POM shows only testset_A's configuration:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration combine.self="override">
<includes>
<include>TestA1.java</include>
<include>TestA2.java</include>
<include>TestA3.java</include>
</includes>
</configuration>
</execution>
</executions>
<configuration combine.self="override">
<includes>
<include>TestA1.java</include>
<include>TestA2.java</include>
<include>TestA3.java</include>
</includes>
</configuration>
</plugin>
Example with combine.children="append":
<profile>
<id>testset_A</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration combine.children="append">
<includes>
<include>TestA1.java</include>
<include>TestA2.java</include>
<include>TestA3.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
mvn help:effective-pom -Penv_local,testset_A
Effective POM shows merged configurations. In this case, it happens to be the same as the first example. However, had testset_A introduced configuration XML elements that already exist in env_local, they would have been appended:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration combine.children="append">
<systemPropertyVariables>
<arquillian.launch>jbossas-managed-jenkins</arquillian.launch>
<jbossHttpPortOverride>${jboss.http.port}</jbossHttpPortOverride>
</systemPropertyVariables>
<includes>
<include>TestA1.java</include>
<include>TestA2.java</include>
<include>TestA3.java</include>
</includes>
</configuration>
</execution>
</executions>
<configuration combine.children="append">
<systemPropertyVariables>
<arquillian.launch>jbossas-managed-jenkins</arquillian.launch>
<jbossHttpPortOverride>${jboss.http.port}</jbossHttpPortOverride>
</systemPropertyVariables>
<includes>
<include>TestA1.java</include>
<include>TestA2.java</include>
<include>TestA3.java</include>
</includes>
</configuration>
</plugin>

Maven: how to get a war package with resources copied in WEB-INF?

when I create a war package with maven, files and directories under the directory "src/main/resources" are copied in /WEB-INF/classes instead of /WEB-INF. How can I get them copied in /WEB-INF?
thanks,
rand
UPDATE:
in my pom now I use this:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>war</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>myapp/target/WEB-INF</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
and I launch mvn with:
mvn -Dmaven.test.skip=true clean package resources:copy-resources
but I got:
[INFO] One or more required plugin parameters are invalid/missing for 'resources:copy-resources'
[0] Inside the definition for plugin 'maven-resources-plugin' specify the following:
<configuration>
...
<outputDirectory>VALUE</outputDirectory>
</configuration>.
[1] Inside the definition for plugin 'maven-resources-plugin' specify the following:
<configuration>
...
<resources>VALUE</resources>
</configuration>.
I'm using maven 2.2 and the snippet basically is the same of the documentation
any idea?
either configure the outputDirectory parameter of resources:resources plugin, or put your files under src/main/webapp/WEB-INF/ directory.
resource plugin
EDIT:
This configuration is working for me:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.2</version>
<executions>
<execution>
<id>default-copy-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/WEB-INF/</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
you can run a phase in the form somePhase or a goal somePlugin:someGoal. The phase invocations will invoke all plugins goals hooked on phases in interval [validate,phase] in order, so there's no need to explicitly call them.
Web resources are not the same as java resources, which should be placed in the classpath. Web resources are processed via the war plugin and should be placed into src\main\webapp\WEB-INF\. In this case, it will work automatically without any additional configuration in the pom.xml
This configuration is working add plugin pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<webResources>
<!--copy resource file location-->
<resource>
<directory>${project.build.directory}/classes</directory>
</resource>
</webResources>
<!--location for add file-->
<webappDirectory>${project.build.directory}/${project.build.finalName}</webappDirectory>
</configuration>
</plugin>

Resources