2

I can build my project with the following command...

csc /reference:lib\Newtonsoft.Json.dll SomeSourceFile.cs

... but when I use this command...

msbuild MyProject.csproj

... with the following .csproj file my .dll reference isn't included. Any thoughts?

<PropertyGroup>
    <AssemblyName>MyAssemblyName</AssemblyName>
    <OutputPath>bin\</OutputPath>
</PropertyGroup>

<ItemGroup>
    <Compile Include="SomeSourceFile.cs" />
</ItemGroup>

<ItemGroup>
    <Reference Include="Newtonsoft.Json">
        <HintPath>lib\Newtonsoft.Json.dll</HintPath>
    </Reference>
</ItemGroup>

<Target Name="Build">
    <MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" />
    <Csc Sources="@(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe" />
</Target>

2 Answers2

5

You didn't get your Reference group hooked up to the Csc task. Also the references the way you specified could not be used directly inside the task. Tasks that ship with MSBuild include ResolveAssemblyReference, that is able to transform short assembly name and search hints into file paths. You can see how it is used inside c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Common.targets.

Without ResolveAssemblyReference, the simplest thing that you can do is to write it like this:

<PropertyGroup> 
    <AssemblyName>MyAssemblyName</AssemblyName> 
    <OutputPath>bin\</OutputPath> 
</PropertyGroup> 

<ItemGroup> 
     <Compile Include="SomeSourceFile.cs" /> 
</ItemGroup> 

<ItemGroup> 
    <Reference Include="lib\Newtonsoft.Json.dll" />
</ItemGroup> 

<Target Name="Build"> 
    <MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" /> 
    <Csc Sources="@(Compile)" References="@(Reference)" OutputAssembly="$(OutputPath)$(AssemblyName).exe" /> 
</Target> 

Notice that Reference item specifies direct path to the referenced assembly.

James Ko
  • 32,215
  • 30
  • 128
  • 239
seva titov
  • 11,720
  • 2
  • 35
  • 54
1

What you've done is to overload the default Build target typically imported via Microsoft.CSharp.targets. In the default Build target, it takes the item array @(Compile), in which your .cs source files are resident, and also the @(Reference) array, among other things, and composites the proper call to the C# compiler. You've done no such thing in your own minimal Build target, which effectivly ignores the declaration of @(Reference) and only supplies @(Compile) to the Csc task.

Try adding the References="@(References)" attribute to the Csc task.

Brian Kretzler
  • 9,748
  • 1
  • 31
  • 28