Search code examples
msbuildmsbuild-task

Why do these MSBuild Output Items seem empty, when they are not?


Strange way to put a question, anyway ... Basically this is my first real attempt with MSBuild.

I managed to create a Target (RenameJs) which produces a new Output Items List named JsMinifyOutput (a renamed version of another input list).

Then, in a dependent Target (ClosureCompile) I'm trying to actually use the previously generated Output list. I have a Message Task as the following, where I can see the Output List values printed on the console:

    <Message Text="Outputs: %(JsMinifyOutput.FullPath)" />

But then, in the same dependent Target, when I try to use the Output List in the actual Exec Task, the list seems to be empty. The following task:

<Exec Command="$(ClosureCompilerCmd) --js %(JsMinify.FullPath) --js_output_file %(JsMinifyOutput.FullPath)" />

appears on the console as just:

java -jar Some\Path\To\SomeInputFile.debug.js --js_output_file

while I would expect something like:

java -jar Some\Path\To\SomeInputFile.js --js_output_file Some\Path\To\SomeFileRenamed.js

If I try with this simple Exec Task, it properly shows all file names:

<Exec Command="echo %(JsMinifyOutput.FullPath)" />

What I am trying to achieve here is the following: starting from an input ItemGroup list, create another (output) ItemGroup list by means of some transformation over the former (e.g. renaming, replacing text, ...). Then, having this two ItemGroup lists, loop over them in parallel so to say, and perform an Exec task over them. In pseudo-language, something like:

 for(i from 0 to inList.length)
 {
   Exec Command="Path\To\Some\Command --in inList[i] --out outList[i]
 } 

Am I doing something completely silly here? PS: This is with Build Engine Version 4.0, .NET Framework Version 4.0


Solution

  • Uhm, it seems that Exec Task cannot iterate over two parallel ItemGroups at the same time. ... At least that is what a MSBuild guru answers here:

    The problem is that you are batching on 2 items at a time.

    I'm not totally sure if what I'm trying to do here is "batching on 2 items at a time".

    EDIT

    Having cleared what's the actual reason for the task not working, here's my approach to (what I think is) a clean solution:

    1. Add a new metadata to the existing input ItemGroup. That metadata is the calculated output path and takes the place of the former output ItemGroup. Inside the metadata you find the filepath transformation (rename, replace, ...) achieved somehow (through Inline Task, Property Function, Transform, you name it).
    2. Reference the new metadata where the former output ItemGroup was used, e.g. within Exec Command attribute, or within the Target Outputs attribute.

    By way of an example:

    <Target Name="JsRename"> <!-- This adds the metadata -->
      <ItemGroup>
        <JsMinify>
          <OutputPath>[do something over %(FullPath)</OutputPath>
        </JsMinify>
      </ItemGroup>
    </Target>
    <Target Name="ClosureCompile" DependsOnTargets="JsRename"> <!-- Here is the actual task -->
      <Exec Command="$(ClosureCompilerCmd) --js %(JsMinify.FullPath) --js_output_file %(JsMinify.OutputPath)" />
    </Target>