For later versions of MSBuild that come with Visual Studio, I think the best way is to run vswhere (which could be a custom tool) and derive the path of MSBuild.exe from its output.
I put this in one stage:
environment {
VSWHERE_DIR = tool(type: 'com.cloudbees.jenkins.plugins.customtools.CustomTool', name: 'vswhere')
}
Then this as the first step:
script {
// The @ sign prevents CMD.EXE from echoing the command and
// thus messing up the JSON output.
def VisualStudioInstances = readJSON(
text: bat(
script: '@"%VSWHERE_DIR%\\vswhere.exe" -products Microsoft.VisualStudio.Product.BuildTools Microsoft.VisualStudio.Product.Professional -requires Microsoft.Component.MSBuild -format json',
returnStdout: true))
def VisualStudioInstallationPath = VisualStudioInstances[0].installationPath
echo "Using Visual Studio from ${VisualStudioInstallationPath}"
env.MSBUILD_DIR = VisualStudioInstallationPath + '\\MSBuild\\15.0\\Bin'
echo "Using MSBuild from ${env.MSBUILD_DIR}"
}
This seems to work but a few things could be improved:
- Error handling, if no matching version of Visual Studio has been installed.
- Also recognize Enterprise, Express, and Community editions. Perhaps it's best to say -products *.
- Can we get the \MSBuild\15.0\Bin part from vswhere somehow?
- Could use NuGet to install vswhere. Then, only NuGet itself would need to be configured as a custom tool.
- Perhaps move to a sharable pipeline library.
|