2014-07-03 23 views
0

我在this answer添加以下自定義任務引導下我的項目文件:爲什麼我的自定義CodeTaskFactory任務在Visual Studio 2013中失敗,但是從命令行工作?

<UsingTask 
    TaskName="SetEnvironmentVariable" 
    TaskFactory="CodeTaskFactory" 
    AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v$(MSBuildToolsVersion).dll"> 

    <ParameterGroup> 
     <Name/> 
     <Value/> 
    </ParameterGroup> 
    <Task> 
     <Using Namespace="System" /> 
     <Code Type="Fragment" Language="cs"> 
     <![CDATA[ 
     Environment.SetEnvironmentVariable(Name, Value); 
     ]]> 
     </Code> 
    </Task> 
    </UsingTask> 

    <Target Name="BeforeBuild"> 
    <PropertyGroup> 
     <TargetServer Condition="'$(TargetServer)' == ''">localhost</TargetServer> 
    </PropertyGroup> 
    <SetEnvironmentVariable Name="TARGET_SERVER" Value="$(TargetServer)"/> 
    </Target> 

這時候我嘗試建立在Visual Studio 2013的項目,但失敗時,我建立的命令行項目成功使用MSBuild(我正在使用Visual Studio 2013更新2)。

從Visual Studio中的錯誤信息是:

Target "BeforeBuild: (TargetId:13)" in project "D:\Dev\Tests.csproj" (target "Build" depends on it): 
Initializing task factory "CodeTaskFactory" from assembly "C:\Program Files (x86)\MSBuild\12.0\bin\Microsoft.Build.Tasks.v12.0.dll". 
Using "SetEnvironmentVariable" task from the task factory "Code Task Factory". 
Task "SetEnvironmentVariable" (TaskId:14) 
D:\Dev\Tests.csproj(130,29): error MSB4064: The "Name" parameter is not supported by the "SetEnvironmentVariable" task. Verify the parameter exists on the task, and it is a settable public instance property. 
D:\Dev\Tests.csproj(130,5): error MSB4063: The "SetEnvironmentVariable" task could not be initialized with its input parameters. 
Done executing task "SetEnvironmentVariable" -- FAILED. (TaskId:14) 

任何想法,爲什麼會這樣呢?

正如您從輸出中看到的,我已經將MSBuild的輸出轉換爲診斷級別,但沒有提供任何進一步的線索。我也嘗試過使用MSBuildToolsVersion來確保它引用的是正確的版本(v12.0),它是。

據我瞭解,這類問題應該是過去的事情MSBuild is now embedded in Visual Studio。我已經努力了整個上午,以使用谷歌找到這個特定問題的答案,但似乎這不是一個非常常見的錯誤信息,現在我很難過。

+0

你可以打印$(MSBuildToolsPath)的值來查看它們是否在VS中使用相同的值? –

回答

0

看看你是以下指導,你會發現你的XML缺少該指南中提供了某些屬性:

更新這樣的:

<ParameterGroup> 
    <Name /> 
    <Value /> 
    </ParameterGroup> 

閱讀:

<ParameterGroup> 
    <Name ParameterType="System.String" Required="true" /> 
    <Value ParameterType="System.String" Required="true" /> 
    </ParameterGroup> 
+0

對不起。即使有這些屬性,它仍然失敗。你能重現這個問題嗎?或者它能爲你正確地工作嗎? – MikeD

0

我有這個相同的問題,我正在編輯包含任務的目標文件,所以我的情況可能會有所不同。但重新啓動VS解決了它,我猜測我正在編輯的目標被緩存,所以沒有看到我的更改。

+0

有趣。我沒有考慮潛在的緩存問題。 – MikeD

相關問題