0

I tried to have a condition on a Target tag, but resulted with the error: target has a reference to item metadata. References to item metadata are not allowed in target conditions unless they are part of an item transform.

So i found this work around: How to add item transform to VS2012 .proj msbuild file and tried to implement it, but i can't figure up what i am doing wrong because it's not working as expected.

<CallTarget Targets="CopyOldWebConfigJs" /> 

<Target Name="CopyOldWebConfigJs" 
            Inputs="@(ContentFiltered)" 
            Outputs="%(Identity).Dummy" 
            DependsOnTargets="webConfigJsCase">

        <Message Text="web.config.js Case" />
</Target>

    <!-- New target to pre-filter list -->
<Target Name="webConfigJsCase"
        Inputs="@(FileToPublish)"
        Outputs="%(Identity).Dummy">
    <ItemGroup>
      <ContentFiltered Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('%(FileToPublish.Filename)%(FileToPublish.Extension)', 'web.config.js'))" />
    </ItemGroup>
</Target>

I thought that Inputs="@(ContentFiltered)" will contain the lines that DependsOnTargets="webConfigJsCase" find. But when i run it , i am getting this message: Skipping target "CopyOldWebConfigJs" because it has no inputs.

I know for a fact that the regex work, and it do find a filename_ext that equals web.config.js so it return True What do i do or understand wrong?

Community
  • 1
  • 1
E.Meir
  • 2,146
  • 7
  • 34
  • 52
  • you should have copied the answer to said question *exactly*: you're missing a `Include="@(FileToPublish)" ` when defining `ContentFiltered `, without that you're just creating an empty item, hence the 'Skipping...' message – stijn Jun 01 '16 at 18:33
  • i tried that, and it didnt work- i removed it because, i thought it's not related, because he have ` Inputs="@(FileToPublish)"` – E.Meir Jun 01 '16 at 18:44
  • 1
    You cannot build itemgroups without using `Include`. Inputs has nothing to do with this, it's an attribute of Target. I tried it as well and it does work for me. So for example when FileToPublish contains "someweb.config.js" I get the message "web.config.js Case", as expected. – stijn Jun 02 '16 at 07:27

1 Answers1

0

In <ItemGroup><Item/></ItemGroup>, no change will be made to the Item item because no action was specified. If you want to add entries to the item, you must specify Include="".

The <Item/> documentation describes the various attributes for item elements inside of an <ItemGroup/>. Note that at the top-level of an MSBuild file, directly under the <Project/> element, you would use the attributes Include and Exclude while in a <Target/> you would use the attributes Include and Remove. Not including any attributes at all is nonsensical and—as far as I know—no different than simply deleting the entire line. I am surprised MSBuild doesn’t throw an error or warning this is almost certainly a mistake and not intentional.

The Inputs and Outputs attributes on your <Target Name="webConfigJsCase"/> are unnecessary. In fact, they slow MSBuild down by making it loop over the target unnecessarily. You can filter just in the <Item/> like this:

<Target Name="webConfigJsCase">
  <ItemGroup>
    <ContentFiltered Condition="'%(FileToPublish.Filename)%(FileToPublish.Extension)' == 'web.config.js'" Include="@(FileToPublish)" />
  </ItemGroup>
</Target>

Additionally, I assume that you intended your regular expression to match web.config.js but not match webaconfigbjs. You don’t need to use an advanced feature like Regular Expressions here because MSBuild’s built-in condition operators already support simple string comparison. If fixed the condition above to be more readable.

binki
  • 7,754
  • 5
  • 64
  • 110