For example, your properties file (portal.properties) under DEV environment might look like
portal.rest.path.accounts=@PORTAL_REST_PATH_ACCOUNT_SERVICE@
portal.rest.path.tax=@PORTAL_REST_PATH_TAX_SERVICE@
portal.logout.url=@PORTAL_LOGOUT_URL@
The values within "@" are tokens that needs to be replaced at build time. The properties files reside under src/main/resources folder as per the Maven structure.Here is a sample DEV resource myapp.properties that sits under /env/dev folder.
PORTAL_REST_PATH_ACCOUNT_SERVICE=http://DEV-1:8080/accounts_service
PORTAL_REST_PATH_TAX_SERVICE=http://DEV-1:8080/tax_service
PORTAL_LOGOUT_URL=https://smlogin-dev.myapp.net/siteminderagent/ssologout/Logout.html
Next step is to configure the pom.xml file in Maven. This is done via the profiles as shown below.
<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">
....
<profiles>
<profile>
<id>dev-tomcat</id>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<filters>
<filter>env/dev/myapp.properties</filter>
</filters>
</build>
</profile>
</profiles>
....
</project>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
You also need to turn filtering on as shown below in the pom file.
Finally, you can invoke this profile with the -P option in the mvn command.
clean install tomcat:run -Dmaven.tomcat.port=8080 -Pdev-tomcat
There 3 types of Maven profiles.
- Per Project profile: Defined in the pom.xml file as shown above.
- Per User profile: Defined in Maven settings.xml file under %USER_HOME%/.m2
- Global profile: Defined in Maven global settings.xml file under %M2_HOME%/conf/
and these profiles can be activated a number of ways
1. Via command line with -P option as shown above.
2. You can activate it via the Maven settings.xml file as shown below
<settings 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/settings-1.0.0.xsd">
<mirrors>
<mirror>
<id>maven.dev.snaponglobal.com</id>
<name>Internal Artifactory Maven repository</name>
<url>http://repo1.maven.org/maven2/</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
<activeProfiles>
<activeProfile>dev-tomcat</activeProfile>
</activeProfiles>
</settings>
All you have to do is type mvn install and the dev-tomcat profile will be kicked off
3. Via environment variables.
<profile>
<id>dev-tomcat</id>
<activation>
<property>
<name>env</name>
<value>dev-tomcat</value>
</property>
</activation>
</profile>
4. Via operating system (OS)
<profile>
<id>test</id>
<activation>
<os>
<name>Windows </name>
<family>Windows</family>
<arch>x86</arch>
<version>6.1<version>
</os>
</activation>
</profile>
5. Based on a missing file
<profile>
<id>test</id>
<activation>
<file>
<missing>target/generated-sources/wsdl</missing>
</file>
</activation>
</profile>
Q. Can you give an example where you used a profile?
A. Firstly, to tokenize properties as demonstrated in the beginning of the post. Secondly, in local development you set up a datasource for your tomcat server to use as shown below.
<profiles>
<!-- Used for running tomcat locally to do application testing by substituting
tokenized variables from a properties file-->
<profile>
<id>dev-tomcat</id>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<filters>
<filter>src/main/filters/myapp.properties</filter>
</filters>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<configuration>
<!-- define your datasource here-->
<contextFile>src/main/resources/tomcat-maven-plugin/context.xml</contextFile>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
The context.xml file will look like
<?xml version="1.0" encoding="ISO-8859-1"?>
<Context>
<Resource
name="jdbc/myapp_ds"
type="javax.sql.DataSource"
driverClassName="com.sybase.jdbc3.jdbc.SybDataSource"
auth="Container"
username="aes_service"
password="service1"
url="jdbc:sybase:Tds:myserver:5500/my_db?applicationname=myapp"
initialSize="5"
maxActive="5"
maxIdle="1"
minIdle="0"
validationQuery="my_validateQuery"
testOnBorrow="true"
testOnReturn="false"
testWhileIdle="false"
poolPreparedStatements="false"
removeAbandoned="false"
logAbandoned="false"
/>
</Context>