From 5b53513eaac4334d2b212306e97ef592cebecda2 Mon Sep 17 00:00:00 2001 From: Conor MacNeill Date: Mon, 27 Jan 2003 14:51:49 +0000 Subject: [PATCH] Resolve the executable if possible in PR: 16040 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@273895 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/tools/ant/taskdefs/ExecTask.java | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/ExecTask.java b/src/main/org/apache/tools/ant/taskdefs/ExecTask.java index 404bc35ab..77b390cc0 100644 --- a/src/main/org/apache/tools/ant/taskdefs/ExecTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/ExecTask.java @@ -1,7 +1,7 @@ /* * The Apache Software License, Version 1.1 * - * Copyright (c) 2000-2002 The Apache Software Foundation. All rights + * Copyright (c) 2000-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -67,6 +67,7 @@ import org.apache.tools.ant.Task; import org.apache.tools.ant.types.Commandline; import org.apache.tools.ant.types.Environment; import org.apache.tools.ant.util.StringUtils; +import org.apache.tools.ant.util.FileUtils; /** * Executes a given command if the os platform is appropriate. @@ -97,6 +98,7 @@ public class ExecTask extends Task { private String resultProperty; private boolean failIfExecFails = true; private boolean append = false; + private String executable; /** * Controls whether the VM (1.3 and above) is used to execute the @@ -128,7 +130,7 @@ public class ExecTask extends Task { * The command to execute. */ public void setExecutable(String value) { - cmdl.setExecutable(value); + this.executable = value; } /** @@ -238,11 +240,39 @@ public class ExecTask extends Task { this.append = append; } + /** + * Attempt to figure out where the executable is so that we can feed + * the full path - first try basedir, then the exec dir and then + * fallback to the straight executable name (i.e. on ther path) + * + * @return the executable as a full path if it can be determined. + */ + private String resolveExecutable() { + // try to find the executable + File executableFile = getProject().resolveFile(executable); + if (executableFile.exists()) { + return executableFile.getAbsolutePath(); + } + + // now try to resolve against the dir if given + if (dir != null) { + FileUtils fileUtils = FileUtils.newFileUtils(); + executableFile = fileUtils.resolveFile(dir, executable); + if (executableFile.exists()) { + return executableFile.getAbsolutePath(); + } + } + + // couldn't find it - must be on path + return executable; + } + /** * Do the work. */ public void execute() throws BuildException { File savedDir = dir; // possibly altered in prepareExec + cmdl.setExecutable(resolveExecutable()); checkConfiguration(); if (isValidOs()) { try {