1

我想建立一个常用函数的库。因此,我创建了一个 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
4

2 回答 2

1

而是构建项目文件。项目文件实际上是一个 MSBuild 文件,因此您只需从命令行 MsBuild 项目文件即可获取输出。

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

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

于 2013-09-11T12:44:27.130 回答
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
于 2013-09-11T13:16:45.473 回答