从相同的解决方案中使用自定义的MSBuild任务吗?自定义、解决方案、任务、MSBuild

2023-09-02 02:10:11 作者:萌哒哒得小情兽

我是新来的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)srcBuildTasksbinBuildTasks.dll" />
  <Target Name="AfterBuild">
    <Test />
  </Target>

我已经成立了构建顺序,使BuildTasks示范之前被建立。但是,当我尝试建立模型我得到这个错误:

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: WIP TestSolution的 src BuildTasks ​​BIN BuildTasks.dll。   无法加载文件或程序集   文件:/// C: WIP TestSolution的 src BuildTasks ​​BIN BuildTasks.dll   它的依赖或之一。系统   找不到指定的文件。   确认的&lt; UsingTask&GT;   声明是正确的,那   集及其所有依赖关系   可用。

The "BuildTasks.Test" task could not be loaded from the assembly C:WIPTestSolutionsrcBuildTasksbinBuildTasks.dll. Could not load file or assembly 'file:///C:WIPTestSolutionsrcBuildTasksbinBuildTasks.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?

我甚至尝试硬编码 WIP TestSolution C。但是,如果我复制.dll文件到我的桌面和硬code的路径,我的桌面,它的 DOES 的工作,我想不通这是为什么。

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输出.dll文件,只是因为我不想调试/发布有不同的路径的bin文件夹。

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进程来运行后生成的任务。这样的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 
          &quot;$(MSBuildProjectDirectory)PostBuild.msbuild&quot; 
          /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)binAssemblyThatJustBuiltAndContainsBuildTask.dll" />
    <PropertyGroup>
        <SomeProperty>SomePropertyDefaultValue</SomeProperty>
    </PropertyGroup>

    <Target Name="PostBuild">
        <MyPostBuildTask SomeProperty="$(SomeProperty)" />
    </Target>
</Project>