7

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>
3
  • Why are you not able to compile and use the dll? Cause that should work (using Cs task), and also seems the proper solution Commented Aug 2, 2014 at 15:25
  • I am, but I want to be able to easily change task. If I had just DLL (God forbid it is checked-in in VCS too), it is too much pain to change both source and then commit binary DLL. Another hybrid option (which I am probably going to adopt) is to keep task's source code in VCS as one project and in my project that uses task DLL to just have a reference to task project and know where dll will land and do <UsingTask>. I was thinking to keep everything in source code. Commented Aug 2, 2014 at 18:58
  • My point is that you keep the source in VCS next to your msbuild files etc, and build the dll while invoking the msbuild file, then use it in that same build file (or any other one). That is best of both worlds, and nice to show off as well :P Commented Aug 2, 2014 at 19:07

1 Answer 1

10

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.