我编写了一个C#模板,用于为AutoCAD创建.Net扩展。在此之前,对于每个AutoCAD版本,都需要指出各个引用集、输出目录、目标.Net框架平台等。AutoCAD的版本很多:AutoCAD 2009、2010、.、2015年。现在我的模板代替了我。我的csproj-file具有CAD_Year属性:
<PropertyGroup>
<CAD_Year>2013</CAD_Year>
<Min_Year>2009</Min_Year>
<Max_Year>2015</Max_Year>
</PropertyGroup>当我更改CAD_Year值时(在csproj-file中手动编辑此选项)--我的项目的所有设置也会根据目标AutoCAD版本进行更改。效果很好。
但是我需要为所有版本的AutoCAD编译我的代码。每次更改CAD_Year都不方便.:((
当我按下Min_Year菜单项时,如何创建编译Max_Year、.、Rebuild Solution版本的项目的循环?
发布于 2015-03-03 09:55:59
如果将其添加到项目文件中:
<ItemGroup>
<CADYears Include="2013;2014;2015"/>
</ItemGroup>
<Target Name="BatchRebuild">
<Msbuild Projects="$(MsBuildThisFile)" Targets="Rebuild" Properties="CAD_Year=%(CADYears.Identity)"/>
</Target>然后打电话
msbuild <path_to_projectfile> /t:BatchRebuild在命令行上,它将构建path_to_projectfile 3次,每一次都具有不同的CAD_Year属性。
要让VS调用这个目标要复杂得多,因为您需要覆盖重建目标,但这对VS2013是有效的(Actualrebuild目标是从C:\Program中的Rebuild目标复制的)
<ItemGroup>
<CADYears Include="2013;2014;2015"/>
</ItemGroup>
<Target Name="ActualRebuild"
Condition=" '$(_InvalidConfigurationWarning)' != 'true' "
DependsOnTargets="$(RebuildDependsOn)"
Returns="$(TargetPath)"/>
<Target Name="BatchRebuild">
<Msbuild Projects="$(MsBuildThisFile)" Targets="ActualRebuild" Properties="CAD_Year=%(CADYears.Identity)"/>
</Target>
<Target Name="Rebuild">
<Msbuild Projects="$(MsBuildThisFile)" Targets="BatchRebuild"/>
</Target>编辑,因为VS中的模板系统试图复制它在项目根中找到的ItemGroups (在我看来,这似乎是一个bug,或者至少是一个非常恼人的特性),您可以通过使用属性并在需要时将其转换为项来解决这个问题:
<PropertyGroup>
<CADYears>2013;2014;2015<CADYears/>
</PropertyGroup>
<Target Name="BatchRebuild">
<ItemGroup>
<CADYearsItem Include="$(CADYears)"/>
</ItemGroup>
<Msbuild Projects="$(MsBuildThisFile)" Targets="Rebuild" Properties="CAD_Year=%(CADYearsItem .Identity)"/>
</Target>注意:在发布在链接中的项目中,您将调用Afterbuild目标中的重建目标。我没有尝试,但这几乎肯定会导致无限递归。因此,您应该坚持解决方案,如上面张贴的一个单独的目标。
发布于 2015-03-10 16:41:38
“谢谢你,”斯蒂恩。我会把你的答案标记为解决方案。在这里,我为突出显示代码创建了一个“答案”。我的当前代码工作如下:
<!-- Redefine the CoreClean target, otherwise MSBuild will remove all results
of building except for the last. -->
<Target Name="CoreClean">
<ItemGroup>
<AllFiles Include="$(OutputPath)\*.*" />
</ItemGroup>
<Copy SourceFiles="@(AllFiles)" DestinationFolder="$(OutputPath)\temp" />
</Target>
<Target Name="BatchRebuild">
<ItemGroup>
<CADYearsItem Include="$(BuildFor)" />
</ItemGroup>
<Msbuild Projects="$(MsBuildThisFile)" Targets="Rebuild" Properties="CAD_Year_Platform=%(CADYearsItem.Identity)" />
<ItemGroup>
<AllFilesBack Include="$(OutputPath)\temp\*.*" />
</ItemGroup>
<Move SourceFiles="@(AllFilesBack)" DestinationFolder="$(OutputPath)" />
<!-- Doesn't work for Debug. The $(OutputPath)\temp\ will not removed.
But it work for Release.-->
<RemoveDir Directories="$(OutputPath)\temp\" />
</Target>我知道,RemoveDir任务对我来说并不适用于Debug,但它不是一个大问题。现在我的模板已经完成,我将对此进行重构。非常感谢!
https://stackoverflow.com/questions/28826881
复制相似问题