ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Steele, Richard" <r...@steelezone.net>
Subject NPE thrown from ProjectHelper2.parse() when parsing from URL
Date Wed, 28 Sep 2011 11:56:36 GMT
Running Ant 1.8.2 on Windows XP, I'm writing a task that returns a string
resource collection of target names in a project.  Well, attempting to write
one.

I'm running into problems when the build file I'm parsing is from a URL
instead of a File.  The main method that I have looks like this:

    private Collection<String> getTargetNames() {

        if (!projectFile.isExists()) {
            throw new BuildException("Cannot find resource " + projectFile);
        }

        Project childProject = project.createSubProject();
        childProject.setJavaVersionProperty();
        project.copyUserProperties(childProject);
        childProject.initProperties();

        ProjectHelper helper =
ProjectHelperRepository.getInstance().getProjectHelperForBuildFile(projectFile);
        childProject.addReference(ProjectHelper.PROJECTHELPER_REFERENCE,
helper);
        helper.parse(childProject, projectFile);

        Collection<String> names = childProject.getTargets().keySet();

        return names;
    }

The projectFile variable is a URLResource.  However, I'm getting a NPE
thrown from helper.parse(Project, Object):

java.lang.NullPointerException
        at
org.apache.tools.ant.helper.ProjectHelper2$ProjectHandler.onStartElement(ProjectHelper2.java:845)
        at
org.apache.tools.ant.helper.ProjectHelper2$RootHandler.startElement(ProjectHelper2.java:600)
        at
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:501)
        at
com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:400)
        at
com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:626)
        at
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3103)
        at
com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:922)
        at
com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
        at
com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
        at
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
        at
com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
        at
com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
        at
com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
        at
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
        at
com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
        at
org.apache.tools.ant.helper.ProjectHelper2.parse(ProjectHelper2.java:307)
        at
org.apache.tools.ant.helper.ProjectHelper2.parse(ProjectHelper2.java:178)

The relevant code in ProjectHelper2 is:

            // set explicitly before starting ?
            if (project.getProperty("basedir") != null) {
                project.setBasedir(project.getProperty("basedir"));
            } else {
                // Default for baseDir is the location of the build file.
                if (baseDir == null) {

project.setBasedir(context.getBuildFileParent().getAbsolutePath());
                } else {
                    // check whether the user has specified an absolute path
                    if ((new File(baseDir)).isAbsolute()) {
                        project.setBasedir(baseDir);
                    } else {

project.setBaseDir(FILE_UTILS.resolveFile(context.getBuildFileParent(),
                                                                  baseDir));
                    }
                }
            }

The problem appears to be that build files will have a parent build file,
but in this case that's null.

Setting the basedir attribute in the build file does fix the problem, and I
can use it as a workaround for now.  But since I can't always depend on that
being set I need to know if is this a Bug or a Feature?  If the former I'll
submit a bug report.

Thanks,
Rich

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message