I just started playing around with MSBuild this evening and am porting an nAnt build script over to MSBuild. Compiling seems pretty straight forward, but one thing I want to do is run a Java program against every file in a directory. I finally got it working, but it seems hacky and I'm quite sure there's a cleaner way to do this. Here's my code:
<!-- Crunch Files -->
<ItemGroup>
<ToCrunch Include="$(BuildDir)/WWW/Scripts/kpc*.js;$(BuildDir)/WWW/Styles/*.css" />
</ItemGroup>
<ConvertToAbsolutePath Paths="@(ToCrunch)">
<Output TaskParameter="AbsolutePaths" ItemName="AbsoluteFiles" />
</ConvertToAbsolutePath>
<Message Text="Crunching Script Files..." />
<Exec WorkingDirectory="Crunch" Command="Java.exe -jar yuicompressor-2.4.2.jar %(AbsoluteFiles.Identity) -o %(AbsoluteFiles.Identity) --charset utf-8" />
Here's my issues with the above approach:
I have to set the working directory to Crunch where java.exe lives, EVEN THOUGH Java.exe is in the system path and should just work.
Because I change the working directory, the included files in the ItemGroup are no longer relative to the working directory so now I have to convert them all to their absolute paths.
This program is very slow, and takes several seconds per file. So what I get is "Crunching script files" and then a 20 second delay or so. I would like to output a message to the screen for each file being processed instead.
Here's the exact same thing in nAnt:
<!-- Crunch Javascript/CSS Files -->
<foreach item="File" property="filename">
<in>
<items>
<include name="${build.dir}/WWW/Scripts/kpc*.js" />
<include name="${build.dir}/WWW/Styles/*.css" />
</items>
</in>
&l开发者_JS百科t;do>
<echo message="Crunching File ${filename}" />
<exec program="Crunch/java">
<arg value="-jar" />
<arg value="Crunch/yuicompressor-2.4.2.jar" />
<arg file="${filename}" />
<arg value="-o" />
<arg file="${filename}" />
<arg line="--charset utf-8" />
</exec>
</do>
</foreach>
If anyone has any ways to fix any of the above issues, I'd greatly appreciate it. Thanks!
I ended up solving this by using the FullPath property instead of the Identity property, which gives the full path.
Why MSBuild doesn't find programs even if they're in the system path is a mystery.
Also, there's a .NET Port of YUI Compressor which has MSBuild support - I plan on trying this out which would make the issue no longer relevant.
精彩评论