MSBuild Working with ItemGroup and EXEC Command

可紊 提交于 2019-12-11 03:15:39

问题


I created the ItemGroup shown in the code snippet. I need to iterate through this ItemGroup and run the EXEC command - also shown in the code snippet. I cannot seem to get it to work. The code returns the error shown below (note - the Message is written 2 times, which is correct), but the EXEC Command is not running correctly. The value is not being set; therefore the EXEC is not executing at all. I need the EXEC to execute twice or by however sections I define in the ItemGroup.

ERROR: Encrypting WebServer appSettings section Encrypting WebServer connectionStrings section C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -pef "" "\gaw\UI" -prov "RSACustomProvider" Encrypting configuration section... The configuration section '' was not found.

CODE SNIPPET:

<ItemGroup>
    <SectionsToEncrypt Include="Item">
      <Section>appSettings</Section>     
    </SectionsToEncrypt>
    <SectionsToEncrypt Include="Item">     
      <Section>connectionStrings</Section>
    </SectionsToEncrypt>  
  </ItemGroup>

  <Target Name="EncryptWebServerWebConfigSections">   
    <Message Text="Encrypting WebServer %(SectionsToEncrypt.Section) section" />

    <Exec Command="$(AspNetRegIis) -pef &quot;%(SectionsToEncrypt.Section)&quot; &quot;$(DropLocation)\$(BuildNumber)\%(ConfigurationToBuild.FlavorToBuild)\$(AnythingPastFlavorToBuild)&quot; -prov &quot;$(WebSiteRSACustomProviderName)&quot;"/>
  </Target>

回答1:


The problem is that you are batching on 2 items at a time. What I mean is the you have the statements

%(SectionsToEncrypt.Section)
%(ConfigurationToBuild.FlavorToBuild)

In the same task invocation. When you batch on more than 1 item at a time in the same task invocation, they will be batch independently. That's why you're error is stating The configuration section '' ...

If you your FlavorToBuild just has one value what you should do is to stuff that into a property before you call to Exec and then use the property. So your one liner would then convert to:

<PropertyGroup>
    <_FlavToBuild>%(ConfigurationToBuild.FlavorToBuild)<_FlavToBuild>
</PropertyGroup>
<Exec Command="$(AspNetRegIis) -pef &quot;%(SectionsToEncrypt.Section)&quot; &quot;$(DropLocation)\$(BuildNumber)\$(_FlavToBuild)\$(AnythingPastFlavorToBuild)&quot; -prov &quot;$(WebSiteRSACustomProviderName)&quot;"/>

If you have multiple values for FlavorToBuild then it's more complicated. You would have 2 options:

  1. Hard code Exec more than once
  2. Use target batching with task batching to perform the foreach/foreach

Batching is one of the most confusing elements of MSBuild. I've put together some online resources at http://sedotech.com/Resources#batching. If you want to know more than that then you can pick up a copy of my book.



来源:https://stackoverflow.com/questions/2761967/msbuild-working-with-itemgroup-and-exec-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!