编写DLL构建脚本

时间:2013-09-11 12:11:53

标签: c# visual-studio-2010 visual-studio dll

我想构建一个包含常用函数的库。因此,我创建了一个Microsoft Visual C#2010项目。该项目包含多个*.cs个文件,每个文件都包含一个类。 目前,我的项目包含两个文件Common.csVars.cs。 要从我的项目中构建dll,我在命令行中运行以下命令。

C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc /target:library /reference:some.dll /out:Library.dll Library\Common.cs Library\Vars.cs

我的问题是,如果我添加其他*.cs文件,我必须编辑此构建命令。

如何创建构建脚本以自动执行此过程?


编辑:这是适合我的解决方案:

我创建了一个名为build.xml的文件。这是此文件的内容。

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="CompileAll" ToolsVersion="3.5">

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- Import Project="c:\.net3.5\Microsoft.Csharp.targets" /    -->
<PropertyGroup>
    <!-- This AppName thing is the base name of your DLL or EXE -->
    <AppName>MyLibraryName</AppName>
</PropertyGroup>

<!-- This build file compiles each .cs file into its own exe -->
<PropertyGroup Condition="'$(Configuration)'==''">
    <Configuration>Debug</Configuration>
    <!-- Default -->
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Debug'">
    <Optimize>false</Optimize>
    <DebugSymbols>true</DebugSymbols><!-- <OutputPath>.\bin</OutputPath>  -->
    <OutputPath>.\</OutputPath>
    <OutDir>.\</OutDir>
    <IntermediateOutputPath>.\</IntermediateOutputPath>
</PropertyGroup>

<!-- Specify the inputs by type and file name -->
<ItemGroup>
    <CSFile Include="LibraryEtasHil\*.cs"/>
</ItemGroup>

<!-- specify reference assemblies for all builds in this project -->
<ItemGroup>
   <Reference Include="mscorlib" />
   <Reference Include="System" />
   <Reference Include="System.Core" />
   <Reference Include="System.Data" />
   <Reference Include="System.Data.Linq" />
   <Reference Include="System.ServiceModel" />
   <Reference Include="System.ServiceModel.Web" />
   <Reference Include="System.Runtime.Serialization" />
   <!-- <Reference Include=".\ObjectDumper.dll" /> -->
   <Reference Include="dummy.dll" />
</ItemGroup>

<Target Name="CompileAll" DependsOnTargets="ResolveAssemblyReferences">

    <Message Text="Reference = @(Reference)" />
    <Message Text="ReferencePath = @(ReferencePath)" />

    <!-- Message Text="MS Build Tools path:  $(MSBuildToolsPath)" / -->

    <!-- Run the Visual C# compilation on all the .cs files. -->

    <CSC
        Sources="@(CSFile)"
        References="@(ReferencePath)"
        OutputAssembly="$(OutputPath)\$(AppName).dll"
        EmitDebugInformation="$(DebugSymbols)"
        TargetType="library"
        Toolpath="$(MSBuildToolsPath)"
        Nologo="true"
    />
</Target>

<!-- redefine the Clean target, from the Microsoft.csharp.targets file.  (Last definition wins) -->
<Target Name="Clean">
    <Delete Files="$(OutputPath)\$(AppName).dll"/>
    <Delete Files="$(OutputPath)\$(AppName).pdb"/>
    <Delete Files="%(CSFile.identity)~"/>
    <Delete Files="build.xml~"/>
</Target>

要构建dll,只需在Visual Studio命令提示符中执行以下命令即可。

msbuild build.xml

2 个答案:

答案 0 :(得分:1)

改为构建项目文件。项目文件实际上是一个MSBuild文件,因此您可以从命令行中单击MsBuild项目文件以获取输出。

此外,如果您了解项目文件的工作原理,那么您还将学习MSBuild,它是一种构建脚本语言,您可以使用它以您喜欢的任何方式自定义构建。

开源替代品是Ant和Maven,但它的MSBuild与Microsoft产品配合得很好,因此可以更容易地坚持使用MsBuild。

答案 1 :(得分:0)

感谢您的建议。我能够为msbuild创建一个有效的构建脚本。我创建了一个名为build.xml的文件。这是此文件的内容。

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="CompileAll" ToolsVersion="3.5">

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- Import Project="c:\.net3.5\Microsoft.Csharp.targets" /    -->
<PropertyGroup>
    <!-- This AppName thing is the base name of your DLL or EXE -->
    <AppName>MyLibraryName</AppName>
</PropertyGroup>

<!-- This build file compiles each .cs file into its own exe -->
<PropertyGroup Condition="'$(Configuration)'==''">
    <Configuration>Debug</Configuration>
    <!-- Default -->
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Debug'">
    <Optimize>false</Optimize>
    <DebugSymbols>true</DebugSymbols><!-- <OutputPath>.\bin</OutputPath>  -->
    <OutputPath>.\</OutputPath>
    <OutDir>.\</OutDir>
    <IntermediateOutputPath>.\</IntermediateOutputPath>
</PropertyGroup>

<!-- Specify the inputs by type and file name -->
<ItemGroup>
    <CSFile Include="LibraryEtasHil\*.cs"/>
</ItemGroup>

<!-- specify reference assemblies for all builds in this project -->
<ItemGroup>
   <Reference Include="mscorlib" />
   <Reference Include="System" />
   <Reference Include="System.Core" />
   <Reference Include="System.Data" />
   <Reference Include="System.Data.Linq" />
   <Reference Include="System.ServiceModel" />
   <Reference Include="System.ServiceModel.Web" />
   <Reference Include="System.Runtime.Serialization" />
   <!-- <Reference Include=".\ObjectDumper.dll" /> -->
   <Reference Include="dummy.dll" />
</ItemGroup>

<Target Name="CompileAll" DependsOnTargets="ResolveAssemblyReferences">

    <Message Text="Reference = @(Reference)" />
    <Message Text="ReferencePath = @(ReferencePath)" />

    <!-- Message Text="MS Build Tools path:  $(MSBuildToolsPath)" / -->

    <!-- Run the Visual C# compilation on all the .cs files. -->

    <CSC
        Sources="@(CSFile)"
        References="@(ReferencePath)"
        OutputAssembly="$(OutputPath)\$(AppName).dll"
        EmitDebugInformation="$(DebugSymbols)"
        TargetType="library"
        Toolpath="$(MSBuildToolsPath)"
        Nologo="true"
    />
</Target>

<!-- redefine the Clean target, from the Microsoft.csharp.targets file.  (Last definition wins) -->
<Target Name="Clean">
    <Delete Files="$(OutputPath)\$(AppName).dll"/>
    <Delete Files="$(OutputPath)\$(AppName).pdb"/>
    <Delete Files="%(CSFile.identity)~"/>
    <Delete Files="build.xml~"/>
</Target>

要构建dll,只需在Visual Studio命令提示符中执行以下命令即可。

msbuild build.xml
相关问题