Skip to content

maven for environment

May 1, 2009

[Updated on 01 May 2009]
I must admit it took more time than I imagined to get the following done in Maven.

1. To filter all my configuration files and replace the variables as per the environment.

– This I managed NOT through individual tags because that was making my pom heavy but
kept it at the war build level directly under build – filters, resources.

2. To clean up all the extra non specific environment folders and files ( ! the excludes with webresources never worked no matter what options I tried so i used the warsourceexcludes with property defined in war plugin and exclude the resources based on the profile.

So there is no way to set properties through ANT and access in maven. So based on profile you set some properties in the profiles tag and access these properties in the warsourceexcludes and so you are excluding files based on your profile.

3. To update the spring files to use properties instead wherever possible.

4. To use placeHolderPropertiesConfigurator for loading my own properties and accessing that instance through code. This was not possible as per design so continued to use the Spring PropertiesLoaderUtils to load properties for accessing in code.

5. Configure the ldapTemplate to be injected in my custom bean.
Created the bean id reference in AVFH class. Passed ldapTemplate to ldapUtils which continues to have
static methods.


  <profiles>
  	<!-- **********************  ed***************************** -->
     <profile>
      	<id>edEnv</id>
	  		 <activation>
	  		 	<property>
            		<name>env.profile</name>
					<value>ed.zz</value>
				</property>
          </activation>
			<properties>
     			<exclude>**/WEB-INF/deployerConfigContext-ad.xml,**/WEB-INF/login-webflow-ad.xml,**/WEB-INF/view/jsp/zz2/**,**/WEB-INF/view/jsp/zz3/**</exclude>
				<jv>zz_ed</jv>
				<env .path>ed</env>
  			</properties>
	</profile>
 
 
 <!-- **********************  ad ***************************** -->
     <profile>
      	<id>adEnv</id>
	  		 <activation>
	  		 	<property>
            		<name>env.profile</name>
					<value>ad.zz3</value>
				</property>
           </activation>
			<properties>
     			<exclude>**/WEB-INF/deployerConfigContext-ed.xml,**/WEB-INF/login-webflow-ed.xml,**/WEB-INF/view/jsp/zz4/**</exclude>
				<jv>zz_ed</jv>
				<env .path>ad</env>
  			</properties>
	</profile>
	
	
	
	  <profile>
      	<id>adEnv</id>
	  		 <activation>
	  		 	<property>
            		<name>env.profile</name>
					<value>ad.zz2</value>
				</property>
           </activation>
			<properties>
     			<exclude>**/WEB-INF/deployerConfigContext-ed.xml,**/WEB-INF/login-webflow-ed.xml,**/WEB-INF/view/jsp/zz/**</exclude>
				<jv>zz_ed</jv>
				<env .path>ad</env>
  			</properties>
	</profile>
  </profiles>
   
   
   
 <build>
  			<filters>
						<filter>src/main/envresources/${env.path}/${env}.zzcas.properties</filter>
						<filter>src/main/envresources/${env.path}/common.zzcas.properties</filter>
						<inputdirectory>src/main/webapp/WEB-INF/classes</inputdirectory>
				</filters>
				 <resources>
					  <resource>
					    <directory>src/main/webapp/WEB-INF/classes</directory>
					    <filtering>true</filtering>
						<targetpath>../cas-server-webapp-3.2.1/WEB-INF/classes</targetpath>
					  </resource>
					  <resource>
				    	<directory>src/main/webapp/WEB-INF/</directory>
						<includes>
								<include>**/web.xml</include>
								<include>**/spring-configuration/propertyFileConfigurer.xml</include>
						</includes>
						<filtering>true</filtering>
						<targetpath>../cas-server-webapp-3.2.1/WEB-INF</targetpath>
				  	</resource>
  				</resources>
	
			<plugins>
				
	 
  			<plugin>
				<groupid>org.apache.maven.plugins</groupid>
				<artifactid>maven-war-plugin</artifactid>
				<configuration>
						<warname>cas.${env}</warname>
					 	<warsourceexcludes>
					 		${exclude}
					 	</warsourceexcludes>
    			</configuration>
			</plugin>
	</plugins>
  </build>

[18 Feb 2009]
Here is what I understood:

Using the profiles.xml file you can only specify the properties that shall be replacing the ${variables} in your configurations.

You cannot specify file replacement instead of properties.

For that you need to use maven-antrun-plugin : that integrates ant tasks into maven and you need to do profile settings inside of Pom.xml instead of using profiles.xml

The reason they state is profiles.xml cannot be used for different locations where artifact is moved /relocated.So the profile + build integration is within pom.xml only.

So if you need to have replacements happening for war build process:  Note that maven-antrun-plugin always takes the goal as run. No other goal is valid.  You would do the following in your pom.xml file:

<profiles>
<profile>
      <id>test</id>
           <activation>
            <activebydefault>
                true
            </activebydefault>
          </activation>

          <build>
<plugins>
<plugin>
                    <artifactid>maven-antrun-plugin</artifactid>
                    <executions>
                      <execution>
<phase>process-resources</phase>
                        <goals>
                          <goal>run</goal>
                        </goals>
                        <configuration>
                                <tasks>
                                    <echo message="copying messages file now..."/>   
                                     <delete file="${project.build.directory}/cas-server-webapp-3.2.1/WEB-INF/cas.properties"/>
                                        <copy      file="src/main/envresources/env2.cas.properties"
                                             tofile="${project.build.directory}/cas-server-webapp-3.2.1/WEB-INF/cas.properties"/>
                                    <echo message="end of copying messages file now..."/>
                                 </tasks>
                          </configuration>
                        </execution>
                     </executions>
                   </plugin>
                </plugins>
            </build>
     </profile>
 </profiles>

Somehow – using the directory as resources and then adding exclusion was not working for avoiding copying of environment specific files to WEB-INF/classes so what I did was – changed the resources folder name – even then I rechecked the inclusion /exclusion were not working – Need to check on that if in future need to include external resources:

 
                         
                           
                           
                           
                                **/*.xml
                                *.xml
                           

                           

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s