ItemGroup中的MSBuild通配符有时会扩展,有时也不会扩展

时间:2014-07-18 21:19:55

标签: .net jenkins msbuild

我有一个msbuild脚本,我试图通过jenkins运行,它有时会成功,有时会因为外卡扩展而失败。这是剧本的主体:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="FinalCopy">
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Dev</Configuration>
    <BuildPlatform Condition=" '$(BuildPlatform)' == '' ">Any CPU</BuildPlatform>
    <DeployDir>$(MSBuildThisFileDirectory)Deploy_$(Configuration)</DeployDir>
    <BuildOutput>$(DeployDir)\Build_Output</BuildOutput>
    <AdminDir>$(DeployDir)\Admin</AdminDir>
    <TempDir>$(DeployDir)\Temp</TempDir>
    <SolutionFile>$(MSBuildThisFileDirectory)MyApplication.sln</SolutionFile>
    <NUnitConsole>C:\Program Files\NUnit 2.6.3\bin\nunit-console.exe</NUnitConsole>
    <AdminConfigLocation>$(ConfigRepo)\Admin\$(Configuration)</AdminConfigLocation>
    <WebConfig>$(AdminConfigLocation)\Web.config</WebConfig>
  </PropertyGroup>

  <ItemGroup>
    <TestConfigs Include="$(AdminConfigLocation)\MyApplication.*.Tests.dll.config" />
    <TestResultsPath Include="$(DeployDir)\TestResults.xml" />
    <ProjectsToBuild Include="$(SolutionFile)">
        <AdditionalProperties>
            VisualStudioVersion=$(VisualStudioVersion);
            Configuration=$(Configuration);
            OutputPath=$(BuildOutput);
            WebPublishMethod=FileSystem;
            publishUrl=$(TempDir);
            DeleteExistingFiles=True;
            DeployOnBuild=True;
            DeployTarget=WebPublish;
            PublishProfile=$(MSBuildThisFileFullPath);
            Platform=$(BuildPlatform)
        </AdditionalProperties>
    </ProjectsToBuild>
  </ItemGroup>
  <ItemGroup>
     <TestDlls Include="$(BuildOutput)\MyApplication.*.Tests.dll" />
  </ItemGroup>
  <Target Name="FinalCopy" DependsOnTargets="RunUnitTests">
    <Message Text="Moving files to final destination" Importance="high" />
    <Exec Command='move "$(TempDir)" "$(AdminDir)"' />
    <Message Text="Finished moving files to final destination" Importance="high" />
  </Target>

  <!-- This target runs the unit tests and publishes the results -->
  <Target Name="RunUnitTests" DependsOnTargets="CopyTestConfigs">
    <Exec Command='"$(NUnitConsole)" "%(TestDlls.Identity)" /xml="@(TestResultsPath)"' />
  </Target>

  <!-- This target copies config files for tests from our config repo -->
  <Target Name="CopyTestConfigs" DependsOnTargets="CopyWebConfig">
    <Message Text="Copying test configuration files" Importance="high" />
    <Copy SourceFiles="@(TestConfigs)" DestinationFolder="$(BuildOutput)" />
    <Message Text="Finished copying test configuration files" Importance="high" />
  </Target>

  <!-- This target copies the web.config files from our config repo -->
  <Target Name="CopyWebConfig" DependsOnTargets="DeployAdmin">
    <Message Text="Copying web configuration files" Importance="high" />
    <Copy SourceFiles="$(WebConfig)" DestinationFolder="$(TempDir)" />
    <Message Text="Finished copying web configuration files" Importance="high" />
  </Target>

  <!-- This target builds and deploys the admin application to a temp folder -->
  <Target Name="DeployAdmin" DependsOnTargets="Init">
    <Message Text="Copying admin files" Importance="high" />
    <MSBuild Projects="@(ProjectsToBuild)" />
    <Message Text="Finished copying admin files" Importance="high" />
  </Target>

  <!-- This target creates the directory in which deployment files are going to be held -->
  <Target Name="Init" DependsOnTargets="Clean">
    <Message Text="Making deployment directory" Importance="high" />
    <MakeDir Directories="$(DeployDir)" />
    <Message Text="Finished making deployment directory" Importance="high" />
  </Target>

  <!-- This target removes any existing build/deploy artifacts -->
  <Target Name="Clean">
    <Message Text="Removing existing deployment directory" Importance="high" />
    <RemoveDir Directories="$(DeployDir)" />
    <Message Text="Finished removing existing deployment directory" Importance="high" />
  </Target>
</Project>

导致问题的一行是:

<Exec Command='"$(NUnitConsole)" "%(TestDlls.Identity)" /xml="@(TestResultsPath)"'

%(TestDlls.Identity)有时会扩展为ItemArray,有时却没有,我完全不知道为什么。似乎如果我删除我的jenkins工作区文件夹并尝试构建,它会因上述问题而失败。非常随机,如果我在$(DeployDir)中手动创建任何文件(例如,空文本文件),然后运行jenkins作业,它将按预期运行。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

我找到了解决这个问题的方法,实际上这个解决方案实际上是一个更清晰的解决方案。我更改了名为RunUnitTests的目标:

<!-- This target runs the unit tests and publishes the results -->
<Target Name="RunUnitTests" DependsOnTargets="CopyTestConfigs">
    <ItemGroup>
        <TestDlls Include="$(BuildOutput)\MyApplication.*.Tests.dll" />
    </ItemGroup>
    <Exec Command="&quot;$(NUnitConsole)&quot; @(TestDlls->'&quot;%(FullPath)&quot;', ' ') /xml=&quot;@(TestResultsPath)&quot;" />
</Target>

这样做的好处是所有测试(在不同的dll中)都会立即传递给NUnit调用,并且所有测试的结果都会在结果文件中输出。我已经在本地和我们的构建服务器上测试了它,它按预期工作。

答案 1 :(得分:0)

我遇到了类似的问题:

  <ItemGroup>
    <NuGetPackageConfigs Include="../**/packages.config " />
  </ItemGroup>
  <Target Name="LoadNuGetPackages">
    <Message Importance="high" Text="%0ARetrieving packages for %(NuGetPackageConfigs.Identity)" />
    <PropertyGroup>
      <Comm>nuget install %(NuGetPackageConfigs.Identity) -o packages"</Comm>
    </PropertyGroup>
    <Exec Command="$(Comm)" />
  </Target>

当我从jenkins工作目录运行localy时:C:\ Program Files(x86)\ Jenkins \ jobs \ project \ workspace, msbuild无法解析../**/packages.config。 当我从C:\ Program Files(x86)\ a \ project运行它时,它可以工作

我改为

  <ItemGroup>
    <NuGetPackageConfigs Include="../*/*/packages.config " />
    <NuGetPackageConfigs Include="../*/packages.config " />
  </ItemGroup>

它有效,坚持不一样,仍然不知道发生了什么。