maven - How to use profiles or targets to get the values from xml properties file using a template file -
using maven or ant wanted values xml file , replace variables using targets/profiles.
properties.xml looks this:
<?xml version="1.0" encoding="utf-8"?> <variables> <variable id="title"> <book.01>abc</book.01> <book.02>def</book.01> <ebook.03>ghi</book.01> <ebook.04>klmn</book.01> </variable> <variable id="author"> <book.01>john</book.01> <book.02>jack</book.01> <ebook.03>simi</book.01> <ebook.04>laura</book.01> </variable> </variables>
using maven or ant if select "book.01" target or profile, want replace values in template.xml values of "book.01" properties.xml
current template.xml looks this:
<?xml version="1.0" encoding="utf-8"?> <projects> <mbean code="org.jboss.naming.jndibindingservicemgr" name="abc.jndi:name=jndiprop"> <attribute name="myprop" serialdatatype="jbxb"> <jndi:bindings xmlns:xs="http://www.w3.org/2001/xmlschema-instance" xmlns:jndi="urn:jboss:jndi-binding-service:1.0" xs:schemalocation="urn:jboss:jndi-binding-service:1.0 resource:jndi-binding-service_1_0.xsd"> <jndi:binding name="title"> <jndi:value type="java.lang.string"> @book.01@ </jndi:value> </jndi:binding> <jndi:binding name="author"> <jndi:value type="java.lang.string"> @book.01@ </jndi:value> </jndi:binding> </jndi:bindings> </attribute> </mbean> <projects>
expected output is:book.01.xml
<?xml version="1.0" encoding="utf-8"?> <projects> <mbean code="org.jboss.naming.jndibindingservicemgr" name="abc.jndi:name=jndiprop"> <attribute name="myprop" serialdatatype="jbxb"> <jndi:bindings xmlns:xs="http://www.w3.org/2001/xmlschema-instance" xmlns:jndi="urn:jboss:jndi-binding-service:1.0" xs:schemalocation="urn:jboss:jndi-binding-service:1.0 resource:jndi-binding-service_1_0.xsd"> <jndi:binding name="title"> <jndi:value type="java.lang.string"> abc </jndi:value> </jndi:binding> <jndi:binding name="author"> <jndi:value type="java.lang.string"> john </jndi:value> </jndi:binding> </jndi:bindings> </attribute> </mbean> <projects>
but wanted know how should use same template when want run values "book.02" or "ebook.03" profiles/target.
note: maven profile name/ant target name matching variables of template.xml @varaible@
name. example: mvn -p book.01
or ant ebook.01
updates:
with of @ken xsl stylesheet file, have used xml-maven plugin transform template.xml.using maven-replacer-plugin able change @book.01@
varaible in template.xml , replace profile name.
example:
<profile> <id>book.02</id> <activation> <activebydefault>true</activebydefault> </activation> <build> <plugins> <plugin> <groupid>com.google.code.maven-replacer-plugin</groupid> <artifactid>replacer</artifactid> <executions> <execution> <phase>prepare-package</phase> <goals> <goal>replace</goal> </goals> </execution> </executions> <configuration> <ignoremissingfile>true</ignoremissingfile> <file>template.xml</file> <outputfile> target/template.xml </outputfile> <regex>false</regex> <token>@book.01@</token> <value>@book.02@</value> </configuration> </plugin> </plugins>
are asking help, or asking people solve entire problem you? obvious haven't worked above data files because neither of them well-formed. asking volunteers fix xml before giving complete answer. suggest in future try solve problem first , ask questions. way can directed regarding where having problem in understanding, , files post @ least well-formed.
a solution below, suspect other readers may interested in approach using <xsl:analyze-string>
.
t:\ftemp>type properties.xml
<?xml version="1.0" encoding="utf-8"?> <variables> <variable id="title"> <book.01>abc</book.01> <book.02>def</book.02> <ebook.03>ghi</ebook.03> <ebook.04>klmn</ebook.04> </variable> <variable id="author"> <book.01>john</book.01> <book.02>jack</book.02> <ebook.03>simi</ebook.03> <ebook.04>laura</ebook.04> </variable> </variables>
t:\ftemp>type template.xml
<?xml version="1.0" encoding="utf-8"?> <projects> <mbean code="org.jboss.naming.jndibindingservicemgr" name="abc.jndi:name=jndiprop"> <attribute name="myprop" serialdatatype="jbxb"> <jndi:bindings xmlns:xs="http://www.w3.org/2001/xmlschema-instance" xmlns:jndi="urn:jboss:jndi-binding-service:1.0" xs:schemalocation="urn:jboss:jndi-binding-service:1.0 resource:jndi-binding-service_1_0.xsd"> <jndi:binding name="title"> <jndi:value type="java.lang.string"> @book.01@ </jndi:value> </jndi:binding> <jndi:binding name="author"> <jndi:value type="java.lang.string"> @book.01@ </jndi:value> </jndi:binding> </jndi:bindings> </attribute> </mbean> </projects>
t:\ftemp>call xslt2 template.xml properties.xsl
<?xml version="1.0" encoding="utf-8"?><projects> <mbean code="org.jboss.naming.jndibindingservicemgr" name="abc.jndi:name=jndiprop"> <attribute name="myprop" serialdatatype="jbxb"> <jndi:bindings xmlns:xs="http://www.w3.org/2001/xmlschema-instance" xmlns:jndi="urn:jboss:jndi-binding-service:1.0" xs:schemalocation="urn:jboss:jndi-binding-service:1.0 resource:jndi-binding-service_1_0.xsd"> <jndi:binding name="title"> <jndi:value type="java.lang.string"> abc </jndi:value> </jndi:binding> <jndi:binding name="author"> <jndi:value type="java.lang.string"> john </jndi:value> </jndi:binding> </jndi:bindings> </attribute> </mbean> </projects>
t:\ftemp>type properties.xsl
<?xml version="1.0" encoding="us-ascii"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="2.0"> <xsl:key name="props" match="variable/*" use="concat(../@id,'
',name(.))"/> <xsl:template match="j:value" xmlns:j="urn:jboss:jndi-binding-service:1.0"> <xsl:copy> <xsl:copy-of select="@*"/> <xsl:variable name="id" select="../@name"/> <xsl:analyze-string select="." regex="@(.*?)@"> <xsl:matching-substring> <xsl:value-of select="key('props',concat($id,'
',regex-group(1)), doc('properties.xml'))"/> </xsl:matching-substring> <xsl:non-matching-substring> <xsl:value-of select="."/> </xsl:non-matching-substring> </xsl:analyze-string> </xsl:copy> </xsl:template> <xsl:template match="@*|node()"><!--identity other nodes--> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet>
t:\ftemp>rem done!
Comments
Post a Comment