maven include different files for different profile - maven

I want to have different spring configuration files for different maven profiles. Filtering is close to what i want, but it change only properties inside one file. What i want is include/exclude and rename files depending on profile. For example i have files profile1-config.xml and profile2-config.xml. For profile1 build profile1-config.xml is renamed to config.xml and profile2-config.xml is excluded from build. For profile2 build profile2-config.xml is renamed to config.xml and profile1-config.xml is excluded from build.
Is this possible in maven?

Your idea won't work that way but if you modify it, it will do as follows:
Say you rather create conf folders for every profile and suck it those files.
src/main/conf
|-/profile1/conf.xml
|-/profile2/conf.xml
and so forth. Configure your profile take in those files. If you intend to deploy a different config for some server, it's best working with additional modules and war overlays because you cannot deploy multiple configs of the same module project at once in Nexus or a local repo. More over, consider that many profile will clutter your pom and introduce more complexity to the build.

The general idea is to use the copy-resources goal in maven-resources-plugin.
You can create a folder to hold all your profiles, for example:
profiles
|-profile1
|-profile2
And in your pom.xml, you can have those settings:
<profiles>
<profile>
<id>profile1</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes</outputDirectory>
<resources>
<resource>
<directory>${basedir}/profiles/profile1</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>profile2</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes</outputDirectory>
<resources>
<resource>
<directory>${basedir}/profiles/profile2</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>

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

Can I defer, in Maven, the resources filtering after the execution of a plugin?

I'm using wro4j for js and css aggregation. So, I work with my js files and in maven build time, wro4j executes and creates the unique js files I need.
There is one js file that has a couple of variables based on the profile from pom.xml (related to the environment, such as url to find something).
When I build the application, I can see inside the war file the replaced variables in the js file.
But, when the plugin for wro4j make the aggregated js file, takes the files from webapps/js and not from target folder (which it is right), so, the aggregated js file has the expression variables instead of the replaced ones.
I want to defer the filtering execution to run after the wro4j plugin. It is possible?
These are the variables on the js file (called global.js):
var mvnProfileEnvName = "${environment.name}";
var mvnProfileFullSearchUrl = "${full.search.url}";
And I have the following code in pom.xml
<profiles>
<profile>
<id>dev</id>
<properties>
<environment.name>dev</environment.name>
<full.search.url>url/for/dev</full.search.url>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<environment.name>test</environment.name>
<full.search.url>url/for/test<full.search.url>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<environment.name>prod</environment.name>
<full.search.url>url/for/prod</full.search.url>
</properties>
</profile>
</profiles>
<build>
<resources>
<resource>
<directory>${basedir}/src/main/webapp/js</directory>
<filtering>true</filtering>
<includes>
<include>global.js</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/webapp/wro</directory>
<filtering>true</filtering>
</resource>
</resources>
</plugins>
<plugin>
<groupId>ro.isdc.wro4j</groupId>
<artifactId>wro4j-maven-plugin</artifactId>
<version>${wro4j.version}</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<configuration>
<minimize>true</minimize>
<jsDestinationFolder>${basedir}/src/main/webapp/wro/js/</jsDestinationFolder>
<cssDestinationFolder>${basedir}/src/main/webapp/wro/css/</cssDestinationFolder>
</configuration>
</plugin>
</plugins>
<build>
You could try setting <filtering>false</filtering> and then manually configure an execution of maven-resources-plugin to occur at a later phase, with filtering enabled. Place this after your wro4j plugin declaration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>filter-resources</id>
<phase>prepare-package</phase>
<goals><goal>resources</goal></goals>
</execution>
</executions>
</plugin>
http://maven.apache.org/plugins/maven-resources-plugin/resources-mojo.html

Maven Profiles Jar issues

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>

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