Search in sources :

Example 1 with AntConsoleAnnotator

use of hudson.tasks._ant.AntConsoleAnnotator in project hudson-2.x by hudson.

the class Ant method perform.

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
    ArgumentListBuilder args = new ArgumentListBuilder();
    EnvVars env = build.getEnvironment(listener);
    AntInstallation ai = getAnt();
    if (ai == null) {
        args.add(launcher.isUnix() ? "ant" : "ant.bat");
    } else {
        ai = ai.forNode(Computer.currentComputer().getNode(), listener);
        ai = ai.forEnvironment(env);
        String exe = ai.getExecutable(launcher);
        if (exe == null) {
            listener.fatalError(Messages.Ant_ExecutableNotFound(ai.getName()));
            return false;
        }
        args.add(exe);
    }
    VariableResolver<String> vr = build.getBuildVariableResolver();
    String buildFile = env.expand(this.buildFile);
    String targets = Util.replaceMacro(env.expand(this.targets), vr);
    FilePath buildFilePath = buildFilePath(build.getModuleRoot(), buildFile, targets);
    if (!buildFilePath.exists()) {
        // because of the poor choice of getModuleRoot() with CVS/Subversion, people often get confused
        // with where the build file path is relative to. Now it's too late to change this behavior
        // due to compatibility issue, but at least we can make this less painful by looking for errors
        // and diagnosing it nicely. See HUDSON-1782
        // first check if this appears to be a valid relative path from workspace root
        FilePath buildFilePath2 = buildFilePath(build.getWorkspace(), buildFile, targets);
        if (buildFilePath2.exists()) {
            // This must be what the user meant. Let it continue.
            buildFilePath = buildFilePath2;
        } else {
            // neither file exists. So this now really does look like an error.
            listener.fatalError("Unable to find build script at " + buildFilePath);
            return false;
        }
    }
    if (buildFile != null) {
        args.add("-file", buildFilePath.getName());
    }
    Set<String> sensitiveVars = build.getSensitiveBuildVariables();
    args.addKeyValuePairs("-D", build.getBuildVariables(), sensitiveVars);
    args.addKeyValuePairsFromPropertyString("-D", properties, vr, sensitiveVars);
    args.addTokenized(targets.replaceAll("[\t\r\n]+", " "));
    if (ai != null)
        env.put("ANT_HOME", ai.getHome());
    if (antOpts != null)
        env.put("ANT_OPTS", env.expand(antOpts));
    if (!launcher.isUnix()) {
        args = args.toWindowsCommand();
        // For some reason, ant on windows rejects empty parameters but unix does not.
        // Add quotes for any empty parameter values:
        List<String> newArgs = new ArrayList<String>(args.toList());
        newArgs.set(newArgs.size() - 1, newArgs.get(newArgs.size() - 1).replaceAll("(?<= )(-D[^\" ]+)= ", "$1=\"\" "));
        args = new ArgumentListBuilder(newArgs.toArray(new String[newArgs.size()]));
    }
    long startTime = System.currentTimeMillis();
    try {
        AntConsoleAnnotator aca = new AntConsoleAnnotator(listener.getLogger(), build.getCharset());
        int r;
        try {
            r = launcher.launch().cmds(args).envs(env).stdout(aca).pwd(buildFilePath.getParent()).join();
        } finally {
            aca.forceEol();
        }
        return r == 0;
    } catch (IOException e) {
        Util.displayIOException(e, listener);
        String errorMessage = Messages.Ant_ExecFailed();
        if (ai == null && (System.currentTimeMillis() - startTime) < 1000) {
            if (getDescriptor().getInstallations() == null)
                // looks like the user didn't configure any Ant installation
                errorMessage += Messages.Ant_GlobalConfigNeeded();
            else
                // There are Ant installations configured but the project didn't pick it
                errorMessage += Messages.Ant_ProjectConfigNeeded();
        }
        e.printStackTrace(listener.fatalError(errorMessage));
        return false;
    }
}
Also used : FilePath(hudson.FilePath) AntConsoleAnnotator(hudson.tasks._ant.AntConsoleAnnotator) ArrayList(java.util.ArrayList) IOException(java.io.IOException) EnvVars(hudson.EnvVars) ArgumentListBuilder(hudson.util.ArgumentListBuilder)

Aggregations

EnvVars (hudson.EnvVars)1 FilePath (hudson.FilePath)1 AntConsoleAnnotator (hudson.tasks._ant.AntConsoleAnnotator)1 ArgumentListBuilder (hudson.util.ArgumentListBuilder)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1