我想要创建一个MSBuild项目,它反映了解决方案中的项目依赖关系,并将VS项目封装在可重用的目标中。
我喜欢解决的问题是在BizTalk应用程序中导出、构建和部署特定的程序集(及其依赖项)。
我的问题是:如何使svn的目标导出、构建和部署可重用,以及如何在为不同的依赖关系构建包装项目时重用它们?
我知道只构建解决方案并只部署所需的程序集会更简单,但我希望尽可能地重用目标。
的零件
我喜欢部署的项目
<Project DefaultTargets="Deploy" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ExportRoot Condition="'$(Export)'==''">Export</ExportRoot>
</PropertyGroup>
<Target Name="Clean_Export">
<RemoveDir Directories="$(ExportRoot)\My.Project.Dir" />
</Target>
<Target Name="Export_MyProject">
<Exec Command="svn export svn://xxx/trunk/Biztalk2009/MyProject.btproj --force" WorkingDirectory="$(ExportRoot)" />
</Target>
<Target Name="Build_MyProject" DependsOnTargets="Export_MyProject">
<MSBuild Projects="$(ExportRoot)\My.Project.Dir\MyProject.btproj" Targets="Build" Properties="Configuration=Release"></MSBuild>
</Target>
<Target Name="Deploy_MyProject" DependsOnTargets="Build_MyProject">
<Exec Command="BTSTask AddResource -ApplicationName:CORE -Source:MyProject.dll" />
</Target>
</Project>它所依赖的项目几乎与此类似(其他.btproj和.csproj)。
发布于 2010-03-19 03:53:28
哇,这是一个论坛帖子加载的问题。我写了大约20页关于在我的.targets中创建可重用的书文件的文章,但是我将在这里让您从这里开始学习基本知识。我认为创建可重用构建脚本(即.targets文件)的关键是三个元素:
这样做的想法是,您希望将所有目标放在单独的文件中,然后这些文件将被驱动构建过程的文件导入。这些是包含数据的文件。由于导入了.targets文件,所以获得所有目标,就好像它们是内联定义的一样。.proj和.targets文件之间将有一个无声的契约。本合同定义为双方都使用的属性和项目。这就是需要验证的内容。
这里的想法并不新鲜。遵循此模式的是.csproj (以及Visual生成的其他项目)。如果您查看您的.csproj文件,您将不会找到一个目标,只找到属性和项。然后,在导入Microsoft.csharp.targets的文件底部(可能因项目类型不同而有所不同)。这个项目文件(以及它导入的其他文件)包含实际执行构建的所有目标。
所以它是这样排列的:
其中MyProdcut.proj看起来可能是:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- This uses a .targets file to off load performing the build -->
<PropertyGroup>
<Configuration Condition=" '$(Configuration)'=='' ">Release</Configuration>
<OutputPath Condition=" '$(OutputPath)'=='' ">$(MSBuildProjectDirectory)\BuildArtifacts\bin\</OutputPath>
</PropertyGroup>
<ItemGroup>
<Projects Include="$(MSBuildProjectDirectory)\..\ClassLibrary1\ClassLibrary1.csproj"/>
<Projects Include="$(MSBuildProjectDirectory)\..\ClassLibrary2\ClassLibrary2.csproj"/>
<Projects Include="$(MSBuildProjectDirectory)\..\ClassLibrary3\ClassLibrary3.csproj"/>
<Projects Include="$(MSBuildProjectDirectory)\..\WindowsFormsApplication1\WindowsFormsApplication1.csproj"/>
</ItemGroup>
<Import Project="SharedBuild.targets"/>
</Project>SharedBuild.targets看起来可能是:
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- This represents a re-usable build file -->
<Target Name="SharedBuild_Validate">
<!-- See http://sedodream.com/2009/06/30/ElementsOfReusableMSBuildScriptsValidation.aspx for more info
about this validation pattern
-->
<ItemGroup>
<_RequiredProperties Include ="Configuration">
<Value>$(Configuration)</Value>
</_RequiredProperties>
<_RequiredProperties Include ="OutputPath">
<Value>$(OutputPath)</Value>
</_RequiredProperties>
<_RequiredItems Include="Projects">
<RequiredValue>%(Projects.Identity)</RequiredValue>
<RequiredFilePath>%(Projects.Identity)</RequiredFilePath>
</_RequiredItems>
</ItemGroup>
<!-- Raise an error if any value in _RequiredProperties is missing -->
<Error Condition="'%(_RequiredProperties.Value)'==''"
Text="Missing required property [%(_RequiredProperties.Identity)]"/>
<!-- Raise an error if any value in _RequiredItems is empty -->
<Error Condition="'%(_RequiredItems.RequiredValue)'==''"
Text="Missing required item value [%(_RequiredItems.Identity)]" />
<!-- Validate any file/directory that should exist -->
<Error Condition="'%(_RequiredItems.RequiredFilePath)' != '' and !Exists('%(_RequiredItems.RequiredFilePath)')"
Text="Unable to find expeceted path [%(_RequiredItems.RequiredFilePath)] on item [%(_RequiredItems.Identity)]" />
</Target>
<PropertyGroup>
<BuildDependsOn>
SharedBuild_Validate;
BeforeBuild;
CoreBuild;
AfterBuild;
</BuildDependsOn>
</PropertyGroup>
<Target Name="Build" DependsOnTargets="$(BuildDependsOn)"/>
<Target Name="BeforeBuild"/>
<Target Name="AfterBuild"/>
<Target Name="CoreBuild">
<!-- Make sure output folder exists -->
<PropertyGroup>
<_FullOutputPath>$(OutputPath)$(Configuration)\</_FullOutputPath>
</PropertyGroup>
<MakeDir Directories="$(_FullOutputPath)"/>
<MSBuild Projects="@(Projects)"
BuildInParallel="true"
Properties="OutputPath=$(_FullOutputPath)"/>
</Target>
</Project>还不要过于关注SharedBuild_Validate的目标。我把它放在那里是为了完整,但不要专注于它。你可以在http://sedodream.com/2009/06/30/ElementsOfReusableMSBuildScriptsValidation.aspx的我的博客上找到更多的信息。
需要注意的重要部分是可扩展性点。尽管这是一个非常基本的文件,但它包含可重用.targets文件的所有组件。您可以通过传递要构建的不同属性和项来自定义它的行为。您可以通过重写一个目标(BeforeBuild、AfterBuild甚至CoreBuild)来扩展它的行为,并且您可以用以下方法将自己的目标注入构建中:
<Project ...>
...
<Import Project="SharedBuild.targets"/>
<PropertyGroup>
<BuildDependsOn>
$(BuildDependsOn);
CustomAfterBuild
</BuildDependsOn>
</PropertyGroup>
<Target Name="CustomAfterBuild">
<!-- Insert stuff here -->
</Target>
</Project>在您的示例中,我将创建一个使用所需属性的SvnExport.targets文件:
然后为Biztalk构建和部署创建另一个Biztalk。如果有必要的话,你可以把这个分成两个。
然后,在您的.proj文件中,您只需导入这两个文件,并按照正确的顺序设置要构建的目标,然后设置off。
这实际上只是创建可重用的构建元素的开始,但这应该会让您的头脑转向。我将把所有这些发布到我的博客,以及下载所有文件的链接。
更新:
张贴在http://sedodream.com/2010/03/19/ReplacingSolutionFilesWithMSBuildFiles.aspx博客上
https://stackoverflow.com/questions/2472183
复制相似问题