public void execute()

in src/main/org/apache/tools/ant/taskdefs/Ant.java [338:460]


    public void execute() throws BuildException {
        File savedDir = dir;
        String savedAntFile = antFile;
        Vector<String> locals = new VectorSet<>(targets);
        try {
            getNewProject();

            if (dir == null && inheritAll) {
                dir = getProject().getBaseDir();
            }

            initializeProject();

            if (dir != null) {
                if (!useNativeBasedir) {
                    newProject.setBaseDir(dir);
                    if (savedDir != null) {
                        // has been set explicitly
                        newProject.setInheritedProperty(MagicNames.PROJECT_BASEDIR,
                                                        dir.getAbsolutePath());
                    }
                }
            } else {
                dir = getProject().getBaseDir();
            }

            overrideProperties();

            if (antFile == null) {
                antFile = getDefaultBuildFile();
            }

            File file = FILE_UTILS.resolveFile(dir, antFile);
            antFile = file.getAbsolutePath();

            log("calling target(s) "
                + (locals.isEmpty() ? "[default]" : locals.toString())
                + " in build file " + antFile, Project.MSG_VERBOSE);
            newProject.setUserProperty(MagicNames.ANT_FILE, antFile);

            String thisAntFile = getProject().getProperty(MagicNames.ANT_FILE);
            // Are we trying to call the target in which we are defined (or
            // the build file if this is a top level task)?
            if (thisAntFile != null && file.equals(getProject().resolveFile(thisAntFile))
                    && getOwningTarget() != null && getOwningTarget().getName().isEmpty()) {
                if ("antcall".equals(getTaskName())) {
                    throw new BuildException(
                            "antcall must not be used at the top level.");
                }
                throw new BuildException(
                        "%s task at the top level must not invoke its own build file.",
                        getTaskName());
            }

            try {
                ProjectHelper.configureProject(newProject, file);
            } catch (BuildException ex) {
                throw ProjectHelper.addLocationToBuildException(
                    ex, getLocation());
            }

            if (locals.isEmpty()) {
                String defaultTarget = newProject.getDefaultTarget();
                if (defaultTarget != null) {
                    locals.add(defaultTarget);
                }
            }

            if (newProject.getProperty(MagicNames.ANT_FILE)
                .equals(getProject().getProperty(MagicNames.ANT_FILE))
                && getOwningTarget() != null) {

                String owningTargetName = getOwningTarget().getName();

                if (locals.contains(owningTargetName)) {
                    throw new BuildException(
                        "%s task calling its own parent target.",
                        getTaskName());
                }

                final Map<String, Target> targetsMap = getProject().getTargets();

                if (locals.stream().map(targetsMap::get)
                    .filter(Objects::nonNull)
                    .anyMatch(other -> other.dependsOn(owningTargetName))) {
                    throw new BuildException(
                        "%s task calling a target that depends on its parent target '%s'.",
                        getTaskName(), owningTargetName);
                }
            }

            addReferences();

            if (!locals.isEmpty() && !(locals.size() == 1
                    && locals.get(0) != null && locals.get(0).isEmpty())) {
                BuildException be = null;
                try {
                    log("Entering " + antFile + "...", Project.MSG_VERBOSE);
                    newProject.fireSubBuildStarted();
                    newProject.executeTargets(locals);
                } catch (BuildException ex) {
                    be = ProjectHelper
                        .addLocationToBuildException(ex, getLocation());
                    throw be;
                } finally {
                    log("Exiting " + antFile + ".", Project.MSG_VERBOSE);
                    newProject.fireSubBuildFinished(be);
                }
            }
        } finally {
            // help the gc
            newProject = null;
            for (Property p : properties) {
                p.setProject(null);
            }

            if (output != null && out != null) {
                FileUtils.close(out);
            }
            dir = savedDir;
            antFile = savedAntFile;
        }
    }