
hudson.tasks.Ant Maven / Gradle / Ivy
package hudson.tasks;
import hudson.CopyOnWrite;
import hudson.Launcher;
import hudson.Util;
import hudson.FilePath;
import hudson.EnvVars;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Descriptor;
import hudson.model.ParametersAction;
import hudson.model.TaskListener;
import hudson.remoting.Callable;
import hudson.util.ArgumentListBuilder;
import hudson.util.FormFieldValidator;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import javax.servlet.ServletException;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.io.StringReader;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
/**
* Ant launcher.
*
* @author Kohsuke Kawaguchi
*/
public class Ant extends Builder {
/**
* The targets, properties, and other Ant options.
* Either separated by whitespace or newline.
*/
private final String targets;
/**
* Identifies {@link AntInstallation} to be used.
*/
private final String antName;
/**
* ANT_OPTS if not null.
*/
private final String antOpts;
/**
* Optional build script path relative to the workspace.
* Used for the Ant '-f' option.
*/
private final String buildFile;
/**
* Optional properties to be passed to Ant. Follows {@link Properties} syntax.
*/
private final String properties;
@DataBoundConstructor
public Ant(String targets,String antName, String antOpts, String buildFile, String properties) {
this.targets = targets;
this.antName = antName;
this.antOpts = Util.fixEmptyAndTrim(antOpts);
this.buildFile = Util.fixEmptyAndTrim(buildFile);
this.properties = Util.fixEmptyAndTrim(properties);
}
public String getBuildFile() {
return buildFile;
}
public String getProperties() {
return properties;
}
public String getTargets() {
return targets;
}
/**
* Gets the Ant to invoke,
* or null to invoke the default one.
*/
public AntInstallation getAnt() {
for( AntInstallation i : DESCRIPTOR.getInstallations() ) {
if(antName!=null && i.getName().equals(antName))
return i;
}
return null;
}
/**
* Gets the ANT_OPTS parameter, or null.
*/
public String getAntOpts() {
return antOpts;
}
public boolean perform(AbstractBuild,?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
AbstractProject proj = build.getProject();
ArgumentListBuilder args = new ArgumentListBuilder();
AntInstallation ai = getAnt();
if(ai==null) {
args.add(launcher.isUnix() ? "ant" : "ant.bat");
} else {
String exe = ai.getExecutable(launcher);
if (exe==null) {
listener.fatalError(Messages.Ant_ExecutableNotFound(ai.name));
return false;
}
args.add(exe);
}
FilePath buildFilePath = buildFilePath(proj.getModuleRoot());
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(proj.getWorkspace());
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());
}
args.addKeyValuePairs("-D",build.getBuildVariables());
if (properties != null) {
Properties p = new Properties();
try {
p.load(new StringReader(properties));
} catch (NoSuchMethodError e) {
// load(Reader) method is only available on JDK6.
// this fall back version doesn't work correctly with non-ASCII characters,
// but there's no other easy ways out it seems.
p.load(new ByteArrayInputStream(properties.getBytes()));
}
for (Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy