问题
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>
回答1:
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
来源:https://stackoverflow.com/questions/25089520/how-to-create-msbuild-inline-task-from-multiple-source-files