Skip to content

Commit

Permalink
Fixed JENKINS-4121
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregory committed Apr 2, 2011
1 parent 02e69b7 commit 20ca9e0
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 157 deletions.
69 changes: 42 additions & 27 deletions pom.xml
@@ -1,28 +1,43 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jvnet.hudson.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.319</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>msbuild</artifactId>
<packaging>hpi</packaging>
<version>1.4-SNAPSHOT</version>
<name>Hudson MSBuild Plugin</name>
<url>http://wiki.hudson-ci.org/display/HUDSON/MSBuild+Plugin</url>
<developers>
<developer>
<id>kdsweeney</id>
<name>Kyle Sweeney</name>
<email>kyle.sweeney@valtech.com</email>
</developer>
<developer>
<id>gbois</id>
<name>Gregory Boissinot</name>
<email>gregory.boissinot@gmail.com</email>
<timezone>+1</timezone>
</developer>
</developers>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.404</version>
</parent>

<artifactId>msbuild</artifactId>
<packaging>hpi</packaging>
<version>1.4-SNAPSHOT</version>
<name>Jenkins MSBuild Plugin</name>
<url>http://wiki.jenkins-ci.org/display/JENKINS/MSBuild+Plugin</url>

<licenses>
<license>
<name>MIT license</name>
<comments>All source code is under the MIT license.</comments>
</license>
</licenses>

<developers>
<developer>
<id>kdsweeney</id>
<name>Kyle Sweeney</name>
<email>kyle.sweeney@valtech.com</email>
</developer>
<developer>
<id>gbois</id>
<name>Gregory Boissinot</name>
<email>gregory.boissinot@gmail.com</email>
<timezone>+1</timezone>
</developer>
</developers>

<scm>
<connection>scm:git:git://github.com/jenkinsci/msbuild-plugin.git</connection>
<developerConnection>scm:git:git@github.com:jenkinsci/msbuild-plugin.git</developerConnection>
</scm>

</project>
191 changes: 91 additions & 100 deletions src/main/java/hudson/plugins/msbuild/MsBuildBuilder.java
@@ -1,145 +1,143 @@
package hudson.plugins.msbuild;

import hudson.CopyOnWrite;
import hudson.Extension;
import hudson.Launcher;
import hudson.Util;
import hudson.*;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Descriptor;
import hudson.tasks.Builder;
import hudson.util.ArgumentListBuilder;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

import java.io.IOException;
import java.util.Map;

/**
* Sample {@link Builder}.
*
* <p>
* <p/>
* <p/>
* When the user configures the project and enables this builder,
* {@link DescriptorImpl#newInstance(StaplerRequest)} is invoked
* and a new {@link MsBuildBuilder} is created. The created
* instance is persisted to the project configuration XML by using
* XStream, so this allows you to use instance fields (like {@link #name})
* to remember the configuration.
*
* <p>
* <p/>
* <p/>
* When a build is performed, the {@link #perform(Build, Launcher, BuildListener)} method
* will be invoked.
*
* will be invoked.
*
* @author kyle.sweeney@valtech.com
* 2009/03/01 -- Gregory Boissinot - Zenika - Add the possibility to manage multiple Msbuild version
* 2009/05/20 - Fix #3610
* 2009/03/01 -- Gregory Boissinot - Zenika - Add the possibility to manage multiple Msbuild version
* 2009/05/20 - Fix #3610
*/
public class MsBuildBuilder extends Builder {

/**
* Identifies {@link Visual Studio} to be used.
*/
private final String msBuildName;
private final String msBuildFile;
private final String cmdLineArgs;

/**
* When this builder is created in the project configuration step,
* the builder object will be created from the strings below.
* @param msName The Visual studio logical identifiant name
* @param msBuildFile The name/location of the msbuild file
* @param targets Whitespace separated list of command line arguments
*/
/**
* Identifies {@link Visual Studio} to be used.
*/
private final String msBuildName;
private final String msBuildFile;
private final String cmdLineArgs;

/**
* When this builder is created in the project configuration step,
* the builder object will be created from the strings below.
*
* @param msName The Visual studio logical identifiant name
* @param msBuildFile The name/location of the msbuild file
* @param targets Whitespace separated list of command line arguments
*/
@DataBoundConstructor
public MsBuildBuilder(String msBuildName, String msBuildFile,String cmdLineArgs) {
this.msBuildName=msBuildName;
this.msBuildFile=msBuildFile;
this.cmdLineArgs=cmdLineArgs;
public MsBuildBuilder(String msBuildName, String msBuildFile, String cmdLineArgs) {
this.msBuildName = msBuildName;
this.msBuildFile = msBuildFile;
this.cmdLineArgs = cmdLineArgs;
}

public String getCmdLineArgs(){
public String getCmdLineArgs() {
return cmdLineArgs;
}

public String getMsBuildFile(){
return msBuildFile;
public String getMsBuildFile() {
return msBuildFile;
}

public String getMsBuildName() {
return msBuildName;
}
return msBuildName;
}

public MsBuildInstallation getMsBuild() {
for( MsBuildInstallation i : DESCRIPTOR.getInstallations() ) {
if(msBuildName!=null && i.getName().equals(msBuildName))
for (MsBuildInstallation i : DESCRIPTOR.getInstallations()) {
if (msBuildName != null && i.getName().equals(msBuildName))
return i;
}
return null;
}
}

@Override
public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener) throws InterruptedException {
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException {
ArgumentListBuilder args = new ArgumentListBuilder();
String execName= "msbuild.exe";

String execName = "msbuild.exe";
MsBuildInstallation ai = getMsBuild();
if(ai==null) {
listener.getLogger().println("Path To MSBuild.exe: " +execName);
args.add(execName);
if (ai == null) {
listener.getLogger().println("Path To MSBuild.exe: " + execName);
args.add(execName);
} else {
File exec = ai.getExecutable();
if(!ai.getExists()) {
listener.fatalError(exec+" doesn't exist");
String pathToMsBuild = ai.getPathToMsBuild();
FilePath exec = new FilePath(launcher.getChannel(), pathToMsBuild);
try {
if (!exec.exists()) {
listener.fatalError(pathToMsBuild + " doesn't exist");
return false;
}
} catch (IOException e) {
listener.fatalError("Failed checking for existence of " + pathToMsBuild);
return false;
}
listener.getLogger().println("Path To MSBuild.exe: " +exec.getPath());
args.add(exec.getPath());
}




listener.getLogger().println("Path To MSBuild.exe: " + pathToMsBuild);
args.add(pathToMsBuild);
}

//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 = cmdLineArgs.replaceAll("[\t\r\n]+"," ");
if(normalizedTarget.trim().length()>0)
args.addTokenized(normalizedTarget);
args.addKeyValuePairs("-P:",build.getBuildVariables());
String normalizedTarget = cmdLineArgs.replaceAll("[\t\r\n]+", " ");
if (normalizedTarget.trim().length() > 0)
args.addTokenized(normalizedTarget);

args.addKeyValuePairs("-P:", build.getBuildVariables());

//If a msbuild file is specified, then add it as an argument, otherwise
//msbuild will search for any file that ends in .proj or .sln
if(msBuildFile != null && msBuildFile.trim().length() > 0){
args.add(msBuildFile);
if (msBuildFile != null && msBuildFile.trim().length() > 0) {
args.add(msBuildFile);
}

//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()) {
args.prepend("cmd.exe","/C");
args.add("&&","exit","%%ERRORLEVEL%%");
if (!launcher.isUnix()) {
args.prepend("cmd.exe", "/C");
args.add("&&", "exit", "%%ERRORLEVEL%%");
}

//Try to execute the command
listener.getLogger().println("Executing command: "+args.toStringWithQuote());
listener.getLogger().println("Executing command: " + args.toStringWithQuote());
try {
Map<String,String> env = build.getEnvironment(listener);
Map<String, String> env = build.getEnvironment(listener);
int r = launcher.launch().cmds(args).envs(env).stdout(listener).pwd(build.getModuleRoot()).join();
return r==0;
return r == 0;
} catch (IOException e) {
Util.displayIOException(e,listener);
e.printStackTrace( listener.fatalError("command execution failed") );
Util.displayIOException(e, listener);
e.printStackTrace(listener.fatalError("command execution failed"));
return false;
}
}

@Override
public Descriptor<Builder> getDescriptor() {
// see Descriptor javadoc for more about what a descriptor is.
return DESCRIPTOR;
}

Expand All @@ -149,38 +147,31 @@ public Descriptor<Builder> getDescriptor() {
@Extension
public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl();

/**
* Descriptor for {@link MsBuildBuilder}. 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> {
@CopyOnWrite
private volatile MsBuildInstallation[] installations = new MsBuildInstallation[0];


@CopyOnWrite
private volatile MsBuildInstallation[] installations = new MsBuildInstallation[0];

DescriptorImpl() {
super(MsBuildBuilder.class);
load();
super(MsBuildBuilder.class);
load();
}

/**
* This human readable name is used in the configuration screen.
*/
public String getDisplayName() {
return "Build a Visual Studio project or solution using MSBuild.";
}

@Override
public boolean configure(StaplerRequest req, JSONObject formData) throws FormException{
installations = req.bindParametersToList(MsBuildInstallation.class,"msbuild.").toArray(new MsBuildInstallation[0]);
save();
return true;
public boolean configure(StaplerRequest req, JSONObject formData) throws FormException {
installations = req.bindParametersToList(MsBuildInstallation.class, "msbuild.").toArray(new MsBuildInstallation[0]);
save();
return true;
}

public MsBuildInstallation[] getInstallations() {
return installations;
}
public MsBuildInstallation[] getInstallations() {
return installations;
}

}
}
31 changes: 6 additions & 25 deletions src/main/java/hudson/plugins/msbuild/MsBuildInstallation.java
Expand Up @@ -2,16 +2,12 @@

import org.kohsuke.stapler.DataBoundConstructor;

import java.io.File;

/**
* MsBuild installation.
*
* @author Gregory Boissinot - Zenika
*/
*/
public final class MsBuildInstallation {


private final String name;
private final String pathToMsBuild;

Expand All @@ -21,26 +17,11 @@ public MsBuildInstallation(String name, String pathToMsBuild) {
this.pathToMsBuild = pathToMsBuild;
}


public String getPathToMsBuild() {
return pathToMsBuild;
}
public String getPathToMsBuild() {
return pathToMsBuild;
}

/**
* Human readable display name.
*/
public String getName() {
return name;
}

public File getExecutable() {
return new File(pathToMsBuild);
}

/**
* Returns true if the executable exists.
*/
public boolean getExists() {
return getExecutable().exists();
}
}

0 comments on commit 20ca9e0

Please sign in to comment.