Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create MSBuild inline task from multiple source files

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.

I am targeting something like:

<UsingTask TaskName="foo" TaskFactory="CodeTaskFactory" AssemblyFile="Microsoft.Build.Tasks.v4.0.dll">
  <Task>
    <Code Type="Class" Language="cs" Source="mydir\*.cs"/>
  </Task>
</UsingTask>
like image 990
stalker314314 Avatar asked Aug 01 '14 22:08

stalker314314


People also ask

Which of the following interface is used to implement a custom task in MS build?

An MSBuild custom task is a class that implements the ITask interface.

What is MSBuild target?

A target element can have both Inputs and Outputs attributes, indicating what items the target expects as input, and what items it produces as output. If all output items are up-to-date, MSBuild skips the target, which significantly improves the build speed. This is called an incremental build of the target.


1 Answers

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:

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:

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

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>

Relevant output:

> 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
like image 178
stijn Avatar answered Oct 03 '22 21:10

stijn