Maven filtering doesn't work with properties in pom.xml - spring

I am trying to configure my pom.xml file in order to replace placeolders in a configuration file for Spring.
Here is my pom.xml :
<?xml version="1.0" encoding="UTF-8"?>
<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>tbetous.spring</groupId>
<artifactId>blog</artifactId>
<name>learning-tp-blog</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<java-version>1.6</java-version>
<env>dev</env>
<org.springframework-version>3.1.1.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.6.6</org.slf4j-version>
</properties>
<dependencies>
[ALL DEPENDENCIES]
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/webapp</directory>
<filtering>true</filtering>
<includes>
<include>WEB-INF/spring/application-context-infrastructure-env.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/webapp</directory>
<filtering>false</filtering>
<excludes>
<exclude>WEB-INF/spring/application-context-infrastructure-env.xml</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
<additionalBuildcommands>
<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
</additionalBuildcommands>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>org.test.int1.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
Here is my Spring configuration file (*path : /src/main/webapp/WEB-INF/spring/application-context-infrastructure-env.xml) :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- ${env} is a variable that is replaced at build time by Maven (see pom.xml).-->
<import resource="infrastructure/${env}/application-context-${env}-infrastructure.xml"/>
</beans>
But when I try to launch my application after a mvn clean install, I get this :
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [application-context-infrastructure-env.xml]
Offending resource: ServletContext resource [/WEB-INF/spring/application-context.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/spring/application-context-infrastructure-env.xml]; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'env'
And when I look at my application-context-infrastructure-env.xml in the target directory, nothing have changed. I have the ${env} placeholder.
I think my maven filtering doesn't work correctly. Could you help me to fix this ?

I think you have to use the maven-war-plugin.
Example configuration from one of my projects:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.5</version>
<configuration>
<filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
<webResources>
<resource>
<directory>src/main/webapp</directory>
<includes>
<include>**/*.html</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/webapp/META-INF</directory>
<targetPath>/META-INF</targetPath>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>
Simply adjust the <directory> elements.

Related

Renaming jar of the same Maven project before bundling it into its war file

We've a Maven project that has war packaging. It has a couple of other projects as dependencies. The requirement here is that all projects added as dependencies should have a common naming convention such that they can be patched in every release. So we decided to replace the version from all such artifacts by -1.0-SNAPSHOT. The below code does it well for artifacts added as dependencies.
We want the classes of this project itself to be included as a jar file. So we set archiveClasses to true. Now the problem here is that the jar generated out of this has the version appended to it - ns-commonservices-6.5.x and maven-dependency-plugin is unable to rename it to ns-commonservices-1.0-SNAPSHOT (Hence, I've removed that code).
Is there any way by which we can rename the jar/artifact of the same project before bundling it into its own war?
Kindly refer the screenshot below. In this we want ns-commonservices-6.5.x.jar to named as ns-commonservices-1.0-SNAPSHOT.jar.
<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.ns.commonservices</groupId>
<artifactId>ns-commonservices</artifactId>
<packaging>war</packaging>
<version>6.5.x</version>
<name>NSHub</name>
<properties>
<nsweb.version>6.5.x</nsweb.version>
<maven-compiler-plugin.version>3.2</maven-compiler-plugin.version>
<buildDirectory>${project.basedir}/target</buildDirectory>
</properties>
<prerequisites>
<maven>3.2.5</maven>
</prerequisites>
<dependencies>
<dependency>
<groupId>com.ns</groupId>
<artifactId>ns-core</artifactId>
<version>${nsweb.version}</version>
</dependency>
<dependency>
<groupId>com.ns</groupId>
<artifactId>ns-common</artifactId>
<version>${nsweb.version}</version>
</dependency>
</dependencies>
<build>
<directory>${buildDirectory}</directory>
<outputDirectory>target/classes</outputDirectory>
<testOutputDirectory>target/test-classes</testOutputDirectory>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>Cp1252</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<archiveClasses>true</archiveClasses>
<attachClasses>true</attachClasses>
<warSourceIncludes>WEB-INF/**</warSourceIncludes>
<packagingExcludes>
WEB-INF/lib/ns-common-${nsweb.version}.jar,
WEB-INF/lib/ns-core-${nsweb.version}.jar,
WEB-INF/classes
</packagingExcludes>
<webResources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xls</include>
<include>**/*.xml</include>
<include>**/*.properties</include>
<include>**/*.version</include>
<include>**/*.json</include>
</includes>
<targetPath>WEB-INF/classes</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.ns</groupId>
<artifactId>ns-common</artifactId>
<version>${nsweb.version}</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${buildDirectory}/${project.build.finalName}/WEB-INF/lib</outputDirectory>
<destFileName>ns-common-1.0-SNAPSHOT.jar</destFileName>
</artifactItem>
<artifactItem>
<groupId>com.ns</groupId>
<artifactId>ns-core</artifactId>
<version>${nsweb.version}</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${buildDirectory}/${project.build.finalName}/WEB-INF/lib</outputDirectory>
<destFileName>ns-core-1.0-SNAPSHOT.jar</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</plugin>
</plugins>
<finalName>nshub</finalName>
</build>
</project>
I guess that the
https://maven.apache.org/plugins/maven-war-plugin/war-mojo.html#outputFileNameMapping
outputFileNameMapping parameter allows you to customise that.

How to set values for properties tag of maven by reading the .properties file

I have 5 projects, here I want to copy the java classes from one non-maven project to maven project for this requirement I used maven-ant-plugin. with this I am able to do the copy successfully.
pom.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>Test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project1_maven.src.main.java>c:/maven1/src/main/java</project1_maven.src.main.java>
<project1_maven.src.test.java>c:/maven1/src/test/java</project1_maven.src.test.java>
<project1.nonmaven.src>c:/non-maven1/src</project1.nonmaven.src>
<project1.nonmaven.test>c:/non-maven1/test</project1.nonmaven.test>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>install</phase>
<configuration>
<target>
<copy file="${project1.nonmaven.src}"
todir="${project1_maven.src.main.java}" />
<copy file="${project1.nonmaven.test}"
todir="${project1_maven.src.test.java}" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
</build>
</project>
But here my question is, instead of giving the source and destination paths (I want to give this like for remaining 4 projects) inside the pom.xml directly, I just want to read it from the properties file.
for example :
<project1.nonmaven.src>
${get the location from property bundle by using key}</project1.nonmaven.src>
Can you please some one suggest me how ??
Thanks.

How to exclude separate files with nonFilteredFileExtension

I have this bundle.
admin.common.upper_case=Uma ou mais letras maiúsculas
During building project this bundle becomes corrupted.
admin.common.upper_case=Uma ou mais letras mai�sculas
In order to fix issue we use this configuration for maven-resources-plugin
Namely we added nonFilteredFileExtension tag for properties extension.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>properties</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
This configuration caused issue with another property file like build.properties :
build.version=${project.version}
static.url.version=${project.build.timestamp}
We tried to use this configuration(namely added filter tag in order to filter build.properties file):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<filters>
<filter>properties/build.properties</filter>
</filters>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>properties</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
Also we tried with this configuration in order to avoid filtering for current bundles:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<webResources>
<resource>
<directory>src/main/resources</directory>
<!-- enable filtering -->
<filtering>true</filtering>
<excludes>
<exclude>${basedir}/src/main/resources/one.properties</exclude>
<exclude>${basedir}/src/main/resources/two.properties</exclude>
</excludes>
</resource>
</webResources>
</configuration>
</plugin>
The question is how to allow filtering only for this property file build.properties
First of all, you should try to fix the root of your problem by Specifying a character encoding scheme:
<project ...>
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
...
</properties>
..
</project>
One way to do what you want is to specify files to include and files to excludes. From the documentation:
<project>
...
<name>My Resources Plugin Practice Project</name>
...
<build>
...
<resources>
<resource>
<directory>src/my-resources</directory>
<includes>
<include>**/*.txt</include>
</includes>
<excludes>
<exclude>**/*test*.*</exclude>
</excludes>
</resource>
...
</resources>
...
</build>
...
</project>
The elements include and exclude should also works with specific files such as path/to/abc.properties.

Why my Maven project automatic generate apidoc in target folder?

I didn't configurated maven-javadoc-plugin in my maven project pom.xml file,but when I run mvn install command,then generate apidoc?
configuration of pom.xml as follow :
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<build>
<finalName>webcnmobile</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<!-- web.xml is not mandatory since JavaEE 5 -->
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
</build>
the spring-security-oauth-parent pom.xml see here
The javadoc plugin is configured in the parent pom that you are referencing (spring-security-oauth-parent). This means that you inherit this configuration and it automatically gets applied to your project.
The configuration in the parent pom is as follows:
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>javadoc</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
This is then being applied to your project and hence you are getting javadoc generated.

Not getting values from properties files in context.xml file

I am unable to retrieve value from .properties file into my context.xml
pom.xml ( didn't mention dependencies )
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>myproj</artifactId>
<version>0.1.1</version>
<packaging>war</packaging>
<name>myproj</name>
<url>http://maven.apache.org</url>
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env>dev</env>
<build.number>0</build.number>
<svn.revision.number>0</svn.revision.number>
</properties>
</profile>
</profiles>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/environment</directory>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.8</version>
<configuration>
<printSummary>false</printSummary>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
<excludes>
<exclude>**/*_Roo_*</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.7</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
<wtpversion>2.0</wtpversion>
<additionalBuildcommands>
<buildCommand>
<name>org.eclipse.ajdt.core.ajbuilder</name>
<arguments>
<aspectPath>org.springframework.aspects</aspectPath>
</arguments>
</buildCommand>
<buildCommand>
<name>org.springframework.ide.eclipse.core.springbuilder</name>
</buildCommand>
</additionalBuildcommands>
<additionalProjectnatures>
<projectnature>org.eclipse.ajdt.ui.ajnature</projectnature>
<projectnature>com.springsource.sts.roo.core.nature</projectnature>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<warName>myproj</warName>
</configuration>
</plugin>
</plugins>
</build>
</project>
context.xml (inside src/main/webapp/META-INF)
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<!-- Specify a JDBC datasource -->
<Resource name="jdbc/mydb" auth="Container"
type="javax.sql.DataSource" username="${database.usernameee}" password="${database.password}"
driverClassName="net.sourceforge.jtds.jdbc.Driver"
url="jdbc:jtds:sybase://localhost:9000/common_db"
maxActive="10" maxIdle="4" />
</Context>
I have some .properties files inside under:
src/main/resources
src/main/environment/dev
I am unable to get values inside my context.xml of ${database.usernameee} and ${database.password}
please suggest what went wrong?
Do the following:
1) Remove the <resources> section. It's not needed for the filtering you want to do and src/main/resources is the default - it doesn't need to specified separately.
<build>
<!-- Delete the <resources> section -->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/environment</directory>
<filtering>false</filtering>
</resource>
</resources>
2) Add a <filters> element under <build> specifying the environment specific properties file
<build>
<filters>
<filter>src/main/environment/${env}/some.properties</filter>
</filters>
...
The above assumes your properties files are called some.properties - so change this to the real name of your file.
3) Modify the configuration of the maven-war-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<webResources>
<resource>
<filtering>true</filtering>
<directory>src/main/webapp</directory>
<includes>
<include>**/META-INF/context.xml</include>
</includes>
</resource>
</webResources>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
<warName>myproj</warName>
</configuration>
</plugin>
That will now filter the context.xml using the environment specific properties - depending upon what profile you used.
(note that you may have a typo in 'database.usernameee' - as it has extra ee at the end)
If you want to get your Maven properties from external files (not from properties inside POM's), you should use Maven Filters: see here and here.

Resources