Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package hudson.plugins.nant;
  import hudson.CopyOnWrite;
  import hudson.Launcher;
  import hudson.Util;
  import hudson.model.Build;
 import java.util.Map;
Sample hudson.tasks.Builder.

When the user configures the project and enables this builder, hudson.model.Descriptor.newInstance(org.kohsuke.stapler.StaplerRequest) is invoked and a new NantBuilder is created. The created instance is persisted to the project configuration XML by using XStream, so this allows you to use instance fields (like nantName) to remember the configuration.

When a build is performed, the hudson.tasks.BuildStepCompatibilityLayer.perform(hudson.model.Build,hudson.Launcher,hudson.model.BuildListener) method will be invoked.

 public class NantBuilder extends Builder {

A whitespace separated list of nant targets to be run
     private final String targets;
The location of the nant build file to run
 	private final String nantBuildFile;

Identifies NantBuilder.NantInstallation to be used.
     private final String nantName;
When this builder is created in the project configuration step, the builder object will be created from the strings below.

nantBuildFile The name/location of the nant build fild
targets Whitespace separated list of nant targets to run
     public NantBuilder(String nantBuildFile,String nantNameString targets) {
     	if(nantBuildFile==null || nantBuildFile.trim().length()==0)
     		this. = "";
     		this. = nantBuildFile;
     	this. = nantName;
     	if(targets == null || targets.trim().length()==0)
     		this. = "";
     		this. = targets;	
Gets the NAnt to invoke, or null to invoke the default one.
     public NantInstallation getNant() {
         forNantInstallation i : .getInstallations() ) {
             if(!=null && i.getName().equals())
                 return i;
         return null;

We'll use these from the config.jelly.
    public String getTargets() {
        return ;
    public String getNantBuildFile(){
    	return ;
    public String getNantName(){
    	return ;
    public boolean perform(AbstractBuild<?,?> buildLauncher launcherBuildListener listenerthrows InterruptedExceptionIOException {
        AbstractProject proj = build.getProject();
        ArgumentListBuilder args = new ArgumentListBuilder();
        String execName;
            execName = "NAnt";
            execName = "NAnt.exe";
        //Get the path to the nant installation
        NantInstallation ni = getNant();
        if(ni==null) {
        } else {
        //If a nant build file is specified, then add it as an argument, otherwise
        //nant will search for any file that ends in .build
        if( != null && .trim().length() > 0){
        //Remove all tabs, carriage returns, and newlines and replace them with
        //whitespaces, so that we can add them as parameters to the executable
        String normalizedTarget = .replaceAll("[\t\r\n]+"," ");
        //According to the Ant builder source code, in order to launch a program 
        //from the command line in windows, we must wrap it into cmd.exe.  This 
        //way the return code can be used to determine whether or not the build failed.
        if(!launcher.isUnix()) {
        //Try to execute the command
    	listener.getLogger().println("Executing command: "+args.toString());
    	Map<String,Stringenv = build.getEnvVars();
        try {
            int r = launcher.launch(args.toCommandArray(),env,listener.getLogger(),proj.getModuleRoot()).join();
            return r==0;
        } catch (IOException e) {
            e.printStackTracelistener.fatalError("command execution failed") );
            return false;
    public Descriptor<BuildergetDescriptor() {
        // see Descriptor javadoc for more about what a descriptor is.
        return ;

Descriptor should be singleton.
    public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl();

Descriptor for NantBuilder. Used as a singleton. The class is marked as public so that it can be accessed from views.
    public static final class DescriptorImpl extends Descriptor<Builder> {
To persist global configuration information, simply store it in a field and call save().

If you don't want fields to be persisted, use transient.

    	public static String PARAMETERNAME_PATH_TO_NANT = "pathToNant";
        private volatile NantInstallation[] installations = new NantInstallation[0];
    	private DescriptorImpl() {
        protected void convert(Map<String,ObjectoldPropertyBag) {
                 = (NantInstallation[]) oldPropertyBag.get("installations");

This human readable name is used in the configuration screen.
        public String getDisplayName() {
            return Messages.NantBuilder_DisplayName();
        public NantInstallation[] getInstallations() {
            return ;
        public boolean configure(StaplerRequest reqthrows FormException{
        	// to persist global configuration information,
            // set that to properties and call save().
            int i;
            String[] names = req.getParameterValues("nant_name");
            String[] homes = req.getParameterValues("nant_home");
            int len;
            if(names!=null && homes!=null)
                len = Math.min(names.length,homes.length);
                len = 0;
            NantInstallation[] insts = new NantInstallation[len];
            fori=0; i<leni++ ) {
                if(names[i].length()==0 || homes[i].length()==0)    continue;
                insts[i] = new NantInstallation(names[i],homes[i]);
            this. = insts;
            return true;
        public Builder newInstance(StaplerRequest reqJSONObject o) {
            return req.bindParameters(NantBuilder.class,"nantBuilder.");
        // web methods
Checks if the NANT_HOME is valid.
        public void doCheckNantHomeStaplerRequest reqStaplerResponse rsp ) throws IOExceptionServletException {
            // this can be used to check the existence of a file on the server, so needs to be protected
            new FormFieldValidator(req,rsp,true) {
                public void check() throws IOExceptionServletException {
                    File f = getFileParameter("value");
                    if(!f.isDirectory()) {
                        error(f+" is not a directory");
                    File nantExe = new File(f,"bin/NAnt.exe");
                    if(!nantExe.exists()) {
                        error(f+" is not a NAnt installation directory.");
    public static final class NantInstallation implements Serializable {
        private final String name;
        private final String nantHome;
        public NantInstallation(String nameString nantHome) {
            this. = name;
            this. = nantHome;

install directory.
        public String getNantHome() {
            return ;

Human readable display name.
        public String getName() {
            return ;
        public String getExecutable(Launcher launcherthrows IOExceptionInterruptedException {
            return launcher.getChannel().call(new Callable<String,IOException>() {
                public String call() throws IOException {
                    File exe = getExeFile();
                        return exe.getPath();
                    throw new IOException(exe.getPath()+" doesn't exist");
        private File getExeFile() {
            String execName;
                execName = "NAnt.exe";
                execName = "NAnt";
            return new File(getNantHome(),"bin/"+execName);

Returns true if the executable exists.
        public boolean getExists() throws IOExceptionInterruptedException {
            LocalLauncher launcher = new LocalLauncher(.);
            return launcher.getChannel().call(new Callable<Boolean,IOException>() {
                public Boolean call() throws IOException {
                    return getExeFile().exists();
        private static final long serialVersionUID = 1L;
New to GrepCode? Check out our FAQ X