问题描述
我是 MSBuild 的新手,想尝试一下,但我就是不明白为什么这不起作用.
I'm a new to MSBuild and wanted to play around with it a bit, but I just cannot figure out why this isn't working.
所以我的解决方案有两个项目:模型"和BuildTasks".BuildTasks 只有一个类:
So my solution has two projects: "Model" and "BuildTasks". BuildTasks just has a single class:
using Microsoft.Build.Utilities;
namespace BuildTasks
{
public class Test : Task
{
public override bool Execute()
{
Log.LogMessage( "FASDfasdf" );
return true;
}
}
}
然后在 Model.csproj 中我添加了这个:
And then in the Model.csproj I've added this:
<UsingTask TaskName="BuildTasks.Test" AssemblyFile="$(SolutionDir)srcBuildTasksinBuildTasks.dll" />
<Target Name="AfterBuild">
<Test />
</Target>
我已经设置了构建顺序,因此BuildTasks"在Model"之前构建.但是当我尝试构建模型时,我得到了这个错误:
I've set up the build order so "BuildTasks" gets built before "Model". But when I try to build Model I get this error:
BuildTasks.Test"任务无法从程序集中加载C:WIPTestSolutionsrcBuildTasksinBuildTasks.dll.无法加载文件或程序集'file:///C:WIPTestSolutionsrcBuildTasksinBuildTasks.dll'或其依赖项之一.系统找不到指定的文件.确认 <UsingTask>声明是正确的,并且程序集及其所有依赖项是可用.
The "BuildTasks.Test" task could not be loaded from the assembly C:WIPTestSolutionsrcBuildTasksinBuildTasks.dll. Could not load file or assembly 'file:///C:WIPTestSolutionsrcBuildTasksinBuildTasks.dll' or one of its dependencies. The system cannot find the file specified. Confirm that the <UsingTask> declaration is correct, and that the assembly and all its dependencies are available.
这个文件肯定存在,为什么MSBuild找不到呢?
This file definitely exists, so why can't MSBuild find it?
我什至尝试用硬编码C:WIPTestSolution"代替$(SolutionDir)"并得到同样的错误.但是,如果我将该 .dll 复制到我的桌面并将路径硬编码到我的桌面,它确实可以工作,但我不知道为什么.
I've even tried hard-coding "C:WIPTestSolution" in place of "$(SolutionDir)" and get the same error. However, if I copy that .dll to my desktop and hard-code the path to my desktop, it DOES work, which I can't figure out why.
编辑:我没有路径错误.我修改了 BuildTasks 的 Debug/Release 构建以将 .dll 输出到 bin 文件夹,因为我不希望 Debug/Release 具有不同的路径.
EDIT: I don't have the path wrong. I modified the Debug/Release builds for BuildTasks to output the .dll to just the bin folder since I didn't want Debug/Release to have different paths.
推荐答案
我们试过这个,我们发现你必须将 UsingTask 放在项目文件的顶部(并且所有路径都正确).但是,一旦到位并且任务加载,它只会工作一次.之后构建开始失败,因为它无法复制任务所在的 DLL.我们实际上是在我们正在构建的同一个程序集/项目中运行一个构建后任务.
We tried this and we found that you have to place the UsingTask at the top of the project file (and have all your paths right). However once thats in place and the task loads up it will only work once. After that the build starts failing because it cant copy the DLL that the task is in. We are actually running a post build task thats inside the same assembly/project that we are building.
我们解决此问题的方法是在单独的 MSBuild 文件上启动单独的 MSBuild 进程来运行 Post Build 任务.这样,DLL 在构建并复制到 bin 目录之后才会加载.
What we did to solve this is to launch a separate MSBuild process on a seperate MSBuild file to run the Post Build tasks. That way the DLL is not loaded until after its built and copied to the bin directory.
<Target Name="AfterBuild">
<Exec Command="$(MSBuildBinPath)MSBuild.exe
"$(MSBuildProjectDirectory)PostBuild.msbuild"
/property:SomeProperty=$(SomeProperty)" />
</Target>
请注意,您可以在命令行上将属性传递给此子构建任务.
Note that you can pass properties into this sub-build task on the command line.
PostBuild.msbuild 看起来像这样:
And the PostBuild.msbuild looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="PostBuild" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<UsingTask TaskName="PostBuild" AssemblyFile="$(MSBuildProjectDirectory)inAssemblyThatJustBuiltAndContainsBuildTask.dll" />
<PropertyGroup>
<SomeProperty>SomePropertyDefaultValue</SomeProperty>
</PropertyGroup>
<Target Name="PostBuild">
<MyPostBuildTask SomeProperty="$(SomeProperty)" />
</Target>
</Project>
这篇关于使用来自同一解决方案的自定义 MSBuild 任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!