Writing a Liferay Deployment Script Part 4

Created: 21 September 2016  Modified:

In part 3 of this article we covered using waitFor, fail and exec to handle files. Today we are going to look into using Apache Ant filters to retrieve a password from an existing configuration file and then using the replace task to place the password in a new configuration file. There are a variety of filters available in Ant and I recommend you browse them to gain familiarity. We will be using the tokenfilter and striplinebreaks filter to retrieve our password. Below is an example build file.

build.xml

<project name="Transfer Passwords" default="transferPasswords" basedir=".">
  <description>
	Transfer passwords from old configuration file to the new file.
  </description>

  <target name="transferPasswords">
	<loadfile property="previousPassword" srcfile="/path/to/old/configFile.xml">
	  <filterchain>
		<tokenfilter>
		  <containsstring contains="password="/>
		  <replaceregex pattern="\s*password\=&quot;" replace="" flags=""/>
		  <replaceregex pattern="&quot;" replace="" flags=""/>
		</tokenfilter>
		<striplinebreaks/>
	  </filterchain>
	</loadfile>

	<replace file="/path/to/new/configFile.xml" token="###PASSWORD###" value="${previousPassword}"/>
  </target>
</project>

In this case I set a prerequisite that our configuration file will have a seperate line in the format of password=”thePassword” and there will only be one. Another is that the new configuration file will have a unique string to mark the location where the password belongs. The transferPasswords target loads our configuration file running it through the chain of filters we specified. The first filter, tokenFilter, discards lines that do not contain the text “password=”. Then it removes the text “password=” and double quotes from the line. Given our prerequisites this leaves us with one line containing just the password. This one line is then fed into the striplinebreaks filter which removes any carriage returns or line feeds. Next we use the replace task to replace the string marking the password location with the password.

For brevities sake I have not included any targets to verify the config files are present, which should be done. Also recommended is to load the new config files after they have been updated to verify the passwords were correctly updated. The following is an example.

build.xml

<project name="Transfer Passwords" default="transferPasswords" basedir=".">
  <description>
	Transfer passwords from old configuration file to the new file.
  </description>

  <target name="transferPasswords">
	<loadfile property="previousPassword" srcfile="/path/to/old/configFile.xml">
	  <filterchain>
		<tokenfilter>
		  <containsstring contains="password="/>
		  <replaceregex pattern="\s*password\=&quot;" replace="" flags=""/>
		  <replaceregex pattern="&quot;" replace="" flags=""/>
		</tokenfilter>
		<striplinebreaks/>
	  </filterchain>
	</loadfile>

	<replace file="/path/to/new/configFile.xml" token="###PASSWORD###" value="${previousPassword}"/>

	<loadfile property="currentPassword" srcfile="/path/to/new/configFile.xml">
	  <filterchain>
		<tokenfilter>
		  <containsstring contains="password="/>
		  <replaceregex pattern="\s*password\=&quot;" replace="" flags=""/>
		  <replaceregex pattern="&quot;" replace="" flags=""/>
		</tokenfilter>
		<striplinebreaks/>
	  </filterchain>
	</loadfile>

	<fail message="Passwords do not match between old and new config file.">     
	  <condition>
		<not><equals arg1="${previousPassword}" arg2="${currentPassword}"/></not>
	  </condition>
	</fail>        
  </target>
</project>

This code example is very similar to the one above. In this case we use the same code to read the password from the new configuration file as we used to read the old one. Then using the fail task we top Ant from continuing if the passwords do not match. In the next part we will setup Ant targets to stop a Apache Tomcat server running a Liferay portal. If we don’t want to fa

tags: liferay - 6.2 - java - deploy - command line
   Less Is More