简体   繁体   English

如何使用 MSBuild 构建文件

[英]How to build the file using MSBuild

I created a C# file.我创建了一个 C# 文件。 Normally we build it using the Visual Studio IDE.通常我们使用 Visual Studio IDE 构建它。 But I want to build it using MSBuild.但我想使用 MSBuild 构建它。

I have one .bat and .xml file.我有一个.bat.xml文件。 What exactly I have to do with .xml file to build that .cs file successfully?我究竟与.xml文件有什么关系才能成功构建该.cs文件?

My .xml file:我的.xml文件:

<property environment="env" />
<property name="tools.dir" location="${env.TOOLS_DIR}"/>

<target name="Buildproject" >
<echo message="I am building project"/>
</target>   

<target name="runtests" depends="Buildproject" description="This is my first ant target">
<echo message="Running tests"/>
</target>

<target name="publish" depends="runtests">
<echo message="Publish artifact"/>
</target>

My .bat file我的.bat文件

SET WORKSPACE=%~dp0 SET TOOLS_DIR=buildtools SET BUILD_FILE=sample_build.xml @echo off echo WORKSPACE: %WORKSPACE% echo TOOLS_DIR: %TOOLS_DIR% echo BUILD_FILE: %BUILD_FILE% %TOOLS_DIR%/apache-ant/bin/ant -Divy.cache.ttl.default=eternal -buildfile %BUILD_FILE% SET WORKSPACE=%~dp0 SET TOOLS_DIR=buildtools SET BUILD_FILE=sample_build.xml @echo off echo WORKSPACE: %WORKSPACE% echo TOOLS_DIR: %TOOLS_DIR% echo BUILD_FILE: %BUILD_FILE% %TOOLS_DIR%/apache-bin .cache.ttl.default=eternal -buildfile %BUILD_FILE%

Your xml file is not right.您的 xml 文件不正确。 It looks like maybe a nant file or some other specification.它看起来可能是一个 nant 文件或其他一些规范。

Here is your most basic .proj (msbuild definition) file.这是您最基本的 .proj(msbuild 定义)文件。

Save this (in the same directory as your .sln file) as "MyMsbuildDef.proj"将此(与 .sln 文件在同一目录中)保存为“MyMsbuildDef.proj”

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="AllTargetsWrapped">
    <PropertyGroup>
        <!-- Always declare some kind of "base directory" and then work off of that in the majority of cases  -->
        <WorkingCheckout>.</WorkingCheckout>
    </PropertyGroup>

    <Target Name="AllTargetsWrapped">
        <CallTarget Targets="BuildItUp" />
    </Target>


    <Target Name="BuildItUp" >
        <MSBuild Projects="$(WorkingCheckout)\MySolution_Or_MyProject_File.sln" Targets="Build" Properties="Configuration=$(Configuration)">
            <Output TaskParameter="TargetOutputs" ItemName="TargetOutputsItemName"/>
        </MSBuild>
        <Message Text="BuildItUp completed" />
    </Target>

</Project>

Obviously, change "MySolution_Or_MyProject_File.sln" to your .sln name.显然,将“MySolution_Or_MyProject_File.sln”更改为您的 .sln 名称。

Now create a .bat file called "MyMsbuildCallIt.bat" and put this in:现在创建一个名为“MyMsbuildCallIt.bat”的 .bat 文件并将其放入:

REM set __msBuildDir=%WINDIR%\Microsoft.NET\Framework\v2.0.50727
REM set __msBuildDir=%WINDIR%\Microsoft.NET\Framework\v3.5
set __msBuildDir=%WINDIR%\Microsoft.NET\Framework\v4.0.30319

call "%__msBuildDir%\msbuild.exe" /target:AllTargetsWrapped "MyMsbuildDef.proj" /p:Configuration=Debug;FavoriteFood=Popeyes /l:FileLogger,Microsoft.Build.Engine;logfile=MyMsbuildDef_Debug.log
call "%__msBuildDir%\msbuild.exe" /target:AllTargetsWrapped "MyMsbuildDef.proj" /p:Configuration=Release;FavoriteFood=Popeyes /l:FileLogger,Microsoft.Build.Engine;logfile=MyMsbuildDef_Release.log


set __msBuildDir=

And run the .bat file.并运行 .bat 文件。

That is your most basic msbuild scenario.这是您最基本的 msbuild 方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM