With maven 3.x profiles.xml is no longer supported. We had all our properties in the profiles.xml. So we changed to maven 3 and had to convert properties in profiles.xml to properties file. Here are the details about maven2.x to maven3.x migration
So here is the java utility class that reads the properties in pom.xml and writes them to the properties file. This utility class also cares about the comments in the file and writes them also to the property file.
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.xml.sax.SAXException; /** * * @author tak * */ public class PropertiesFileCreator { public static void main(String[] args) throws FileNotFoundException, SAXException, IOException, ParserConfigurationException { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(new InputSource(new FileInputStream("mavenproject//pom.xml"))); // change this line to point to your pom.xml document.getDocumentElement().normalize(); NodeList rootList = document.getChildNodes().item(0).getChildNodes(); NodeList propertyList = null; for (int temp = 0; temp < rootList.getLength(); temp++) { Node nNode = rootList.item(temp); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; if (eElement.getTagName().equals("properties")) { propertyList = eElement.getChildNodes(); } } } PrintWriter out = new PrintWriter("mavenproject//profile.properties"); // change here to the properties file for (int temp = 0; temp < propertyList.getLength(); temp++) { Node nNode = propertyList.item(temp); if (nNode.getNodeType() == Node.COMMENT_NODE) { String nodeValue = nNode.getNodeValue(); nodeValue = nodeValue.replaceAll("(r|n|rn)+", "r#"); System.out.println("#" + nodeValue); out.println("#" + nodeValue); } if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; out.println(eElement.getTagName() + "=" + eElement.getTextContent()); System.out.println(eElement.getTagName() + "=" + eElement.getTextContent()); } } out.close(); } }
Questions and suggestions are always welcome!