Java – how to parse and interpret ant’s build xml
Is there any for reading and ant build XML and retrieve the ant API of the element from it? Specifically, I want to be able to retrieve the values in the path elements and traverse all the elements in the path
My goal is to retrieve the given path and ensure that it is referenced correctly in the manifest so that it can be built to match the manifest when the product is put into production
Editor: with regard to the response using the XML API (and thank them), the problem is that the currently built build file is more complex than this That is, the classpath refers to and contains different Classpaths, and the elements referenced in the classpath themselves are defined in the property file, so there are too many ant APIs that can be reasonably recreated
Solution
You can use the projecthelper class to configure a project using a build file If the path to check is contained in a reference, you can get the reference from the project through its ID
For example, if you're building There are such things in XML:
<path id="classpath"> <fileset dir="${basedir}/lib" includes="*.jar"/> </path>
You can then obtain the path reference using 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"); } }
Notice that projecthelper Configureproject in ant 1.6 2 is deprecated but not recommended in 1.7