我想创建一个MSBuild项目,它反映了解决方案中的项目依赖项,并将VS项目包装在可重用目标中.
我喜欢解决的问题是在BizTalk应用程序中svn导出,构建和部署特定程序集(及其依赖项).
我的问题是:如何为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>
Run Code Online (Sandbox Code Playgroud)
它所依赖的项目看起来几乎就像这样(其他.btproj和.csproj).
Say*_*imi 16
哇,这是一个论坛帖子的加载问题.我写了大约20页关于在我的书中创建可重用的.targets文件,但是我会在这里开始介绍这些基础知识.我相信创建可重用构建脚本(即.targets文件)的关键是三个要素:
我们的想法是,您希望将所有目标放在单独的文件中,然后这些文件将由驱动构建过程的文件导入.这些是包含数据的文件.由于您导入了.targets文件,因此您可以获得所有目标,就像它们已经内联定义一样..proj和.targets文件之间将存在静默契约.此合同在两个使用的属性和项目中定义.这是需要验证的.
这里的想法并不新鲜.此模式后跟.csproj(以及Visual Studio生成的其他项目).如果您查看.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>
Run Code Online (Sandbox Code Playgroud)
而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>
Run Code Online (Sandbox Code Playgroud)
不要太看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>
Run Code Online (Sandbox Code Playgroud)
在您的情况下,我将创建一个使用所需属性的SvnExport.targets文件:
然后为Biztalk构建和部署创建另一个.如有必要,您可以将其拆分为2.
然后在.proj文件中导入两个并设置目标以按正确的顺序构建,然后关闭.
这只是创建可重复使用的构建元素的开始,但这应该让车轮转向你的脑袋.我将把所有这些发布到我的博客以及下载所有文件的链接.
更新:
发表于博客http://sedodream.com/2010/03/19/ReplacingSolutionFilesWithMSBuildFiles.aspx
| 归档时间: |
|
| 查看次数: |
3519 次 |
| 最近记录: |