如何从多个源文件创建MSBuild内联任务 [英] How to create MSBuild inline task from multiple source files

查看:94
本文介绍了如何从多个源文件创建MSBuild内联任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个CS文件(一个DLL项目),全部位于一个目录中,并且其中的一个类扩展了ITask.现在,它很容易并且有文档记录如何从一个源文件创建内联任务,但是是否可以从多个源文件中执行此任务?我无法编译并使用DLL作为一项任务,因此我希望不必将所有源都塞入一个大源文件中.

I am having several CS files (one DLL project), all in one directory and one of the classes there extends ITask. Now, it is easy and documented how to create inline task from one source file, but is it possible to do this from multiple source files? I am not able to compile and use DLL as a task and I would prefer if I don't have to cram all sources into one big source file.

我的目标是:

<UsingTask TaskName="foo" TaskFactory="CodeTaskFactory" AssemblyFile="Microsoft.Build.Tasks.v4.0.dll">
  <Task>
    <Code Type="Class" Language="cs" Source="mydir\*.cs"/>
  </Task>
</UsingTask>

推荐答案

由于没有其他答案,因此,这里提供了一个完整的示例,可以随时随地从任意数量的源文件中构建dll,就像评论中谈到的那样.两个源文件:

Since there's no other answer, here's a complete sample of building a dll from any number of source files on the go like talked about in the comments. Two sourcefiles:

sometask.cs:

sometask.cs:

using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Bar;

namespace Foo
{
  public class CustomTask : Task
  {
    public override bool Execute()
    {
      Log.LogWarning( LogString.Get() );
      return true;
    }
  }
}

sometask_impl.cs:

sometask_impl.cs:

namespace Bar
{
  public static class LogString
  {
    public static string Get()
    {
      return "task impl";
    }
  }
}

以及带有目标的msbuild文件,该文件使用Foo.CustomTask,但先构建它:

And the msbuild file with a target which uses Foo.CustomTask, but builds it first:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="MyBuild">

  <PropertyGroup>
    <SomeTaskDll>SomeTask.dll</SomeTaskDll>
  </PropertyGroup>

  <Target Name="BuildSomeTaskDll">
    <Csc Sources="$(MSBuildThisFileDirectory)sometask*.cs"
         References="System.dll;mscorlib.dll;Microsoft.Build.Framework.dll;Microsoft.Build.Utilities.v4.0.dll"
         TargetType="Library" OutputAssembly="$(MSBuildThisFileDirectory)$(SomeTaskDll)"/>
  </Target>

  <UsingTask TaskName="Foo.CustomTask" AssemblyFile="$(SomeTaskDll)"/>

  <Target Name="MyBuild" DependsOnTargets="BuildSomeTaskDll">
    <Foo.CustomTask />
  </Target>

</Project>

相关输出:

> msbuild sometask.targets
Project sometask.targets on node 1 (default targets).
BuildSomeTaskDll:
<here it's building SomeTask.dll>

sometask.targets(17,5): warning : task impl

这篇关于如何从多个源文件创建MSBuild内联任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆