How to parse and interpret ant build.xml

Is there an Ant API to read and Ant build.xml and extract elements from it? In particular, I want to be able to retrieve values ​​in a path element and be able to move all elements in a path.

My goal is to get the given path and make sure that it is correctly specified in the manifest, so that the assembly and manifest coincide when the product goes into production.

EDIT: Regarding the answers (and thanks to them) for using the XML API, the problem is that the assembly file, as currently built, is more complex than that. Namely, the class path refers to another class path and includes it, and the elements specified in the class path are themselves defined in the properties file, therefore there are too many Ant APIs for a reasonable recreation.

+3
source share
4 answers

You can use the class ProjectHelperto customize your project using the build file. If the path you want to check is contained in the link, then you can simply get the link from the project by its identifier.

For example, if you have something like this in build.xml:

<path id="classpath">
    <fileset dir="${basedir}/lib" includes="*.jar"/>
</path>

Then you can get a link Pathwith the following code:

import java.io.File;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;
import org.apache.tools.ant.types.Path;

public class Test {
    public static void main(String[] args) throws Exception {    
        Project project = new Project();
        File buildFile = new File("build.xml");
        project.init();
        ProjectHelper.configureProject(project, buildFile);

        Path path = (Path) project.getReference("classpath");
     }
}

Note that it is ProjectHelper.configureProjectdeprecated in ant 1.6.2, but not in 1.7.

+7
source

XML Ant script, java . , <xmlproperty>. xml xml Ant , Ant.

<root>
    <properties>
      <foo>bar</foo>
    </properties>
</root>

Ant script : <property name="root.properties.foo" value="bar"/> ${root.properties.foo}.

: 1. xml, , Info.xml 2. Ant script, , Check.xml

info.xml

<?xml version="1.0" encoding="UTF-8"?>
<Students>

  <Student>
    <name>Binod Kumar Suman</name>
        <roll>110</roll>
    <city> Bangalore </city>
  </Student>


</Students>

Check.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="Check" default="init">
 <xmlproperty file="Info.xml" collapseAttributes="true"/>

<target name = "init">
  <echo> Student Name :: ${Students.Student.name} </echo>
  <echo> Roll :: ${Students.Student.roll} </echo>
  <echo> City :: ${Students.Student.city} </echo>
</target>

</project>

(Check.xml) Ant script

: C:\XML_ANT_Workspace\XML_ANT\src\Check.xml :    [echo] ::    [] Roll:: 110    [] :: : 125

, xml (StudentsInfo.xml), ,

: C:\XML_ANT_Workspace\XML_ANT\src\Check.xml :    [echo] :: , ,    [echo] Roll:: 110 120 120    [] :: , , : 109

+3

, API, , script, . SAX python .

from xml.sax import make_parser
from xml.sax.handler import ContentHandler 

def PathHandler(ContentHandler):
    def __init__(self):
        self._reading_path = False
        self._path = ""

    def start_element(self, name, attrs):
        if name == "path":
            self._reading_path = True

    def characters(self, ch):
        if self._reading_path:
            self._path += ch

    def end_element(self, name):
        if name == "path":
            self._reading_path = False
            #self._path is now populated with the contents of the path element

    @property
    def path(self):
        return self._path

handler = PathHandler()
parser = make_parser()
parser.setContentHandler(handler)
parser.parse("build.xml")

This will read any text nodes that are between two xml elements. This should be fairly easy to configure to fulfill other simple XML parsing requirements.

+2
source

If you want to get only a special element, use the Java XML API.

0
source

Source: https://habr.com/ru/post/1707788/


All Articles