如何将项目转换添加到VS2012 .proj msbuild文件 [英] How to add item transform to VS2012 .proj msbuild file
问题描述
基于此答案描述了一项项目转换,将图像文件从jpg转换为png ,我进行了一项项目转换将.docx文件转换为.pdf.当我从projectname.proj构建文件中调用它时,出现以下错误消息:
Based off this answer describing an item transform to convert image files from jpg to png, I made an item transform that converts .docx file to .pdf. When I call it from my projectname.proj build file I get this error message:
Error 1 The condition " '%(Extension)' == '.docx' " on the
"WordToPdf" 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. [project path]\.build\WordToPdf.Tasks.target 7 9
我该如何进行这项工作?
How can I make this work?
这是我的目标:
<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- $Id$ -->
<Target Name="WordToPdf"
Inputs="@(Content)"
Outputs="@(Content -> '%(RootDir)%(Directory)%(Filename).pdf' )"
Condition=" '%(Extension)' == '.docx' ">
<ItemGroup>
<Sublist Include="@(Content)" Condition=" '%(Extension)' == '.docx' " />
</ItemGroup>
<PropertyGroup>
<PowerShellExe Condition=" '$(PowerShellExe)'=='' ">%WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe</PowerShellExe>
<ScriptLocation Condition=" '$(ScriptLocation)'=='' ">.\WordToPdf.ps1</ScriptLocation>
</PropertyGroup>
<Exec Command="$(PowerShellExe) -NonInteractive -executionpolicy Unrestricted -command "& {&'$(ScriptLocation)' -WordFilename '/Input:%(Sublist.FullPath)' }"" />
<Content Remove="@(Sublist)" />
<Content Include="@(Sublist -> '%(RootDir)%(Directory)%(Filename).pdf' )" />
</Target>
</Project>
然后从BeforeBuild目标的我的projectname.proj文件中调用它,如下所示:
And call it from my projectname.proj file from the BeforeBuild target, as follows:
<Import Project="$(MSBuildTasksPath)\WordToPdf.Tasks.target" />
....
<Target Name="BeforeBuild">
<CallTarget Targets="WordToPdf" />
</Target>
更新
除了使转换正常工作的解决方法外,此目标还有更多错误.为了将来参考,这是最终的工作代码:
There was a bit more wrong with this target beyond just the workaround to get the transform to work. For future reference, here's the final working code:
<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- $Id$ -->
<Target Name="WordToPdf"
Inputs="@(ContentFiltered)"
Outputs="@(ContentFiltered -> '%(RootDir)%(Directory)%(Filename).pdf' )"
DependsOnTargets="FilterContent">
<PropertyGroup>
<PowerShellExe Condition=" '$(PowerShellExe)'=='' ">%WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe</PowerShellExe>
<ScriptLocation Condition=" '$(ScriptLocation)'=='' ">.\WordToPdf.ps1</ScriptLocation>
</PropertyGroup>
<Exec Command="$(PowerShellExe) -NonInteractive -executionpolicy Unrestricted -command "& {&'$(ScriptLocation)' -WordFilename '%(ContentFiltered.FullPath)' }"" />
<ItemGroup>
<Content Remove="@(ContentFiltered)" />
<Content Include="@(ContentFiltered -> '%(RootDir)%(Directory)%(Filename).pdf' )" />
</ItemGroup>
</Target>
<!-- New target to pre-filter list -->
<Target Name="FilterContent">
<ItemGroup>
<ContentFiltered Include="@(Content)" Condition="'%(Extension)' == '.docx'" />
</ItemGroup>
</Target>
</Project>
而且,对于任何在这里搜索将msbuild字词转换为pdf项的人"的人 这是我的powershell脚本:
And, for anyone who got here searching for a "msbuild word to pdf item transform" here's my powershell script:
Param(
[string]$WordFilename
)
$Word = New-Object -comobject Word.Application
$Doc=$Word.Documents.Open($WordFilename)
$Doc.saveas([ref](($WordFilename).replace("docx","pdf")), [ref]17)
$Doc.close()
将.docx文件作为内容添加到您的项目中-如果较新则进行复制,如果docx文件较新则将创建pdf文件,然后将pdf文件复制到其输出位置.
Add the .docx file to your project, as content - copy if newer, and the pdf file will be created if the docx file is newer, and then the pdf file is copied to its output location.
推荐答案
您可以通过创建单独的目标来解决此问题,该目标根据您的条件预先过滤项目-实际上,会创建新列表.
You can workaround it by creating separate target, which pre-filters items based on your condition - actually, creates new list.
这里是示例-注意新的DependsOnTargets
标记并更改了ContentFiltered
名称:
Here is sample - notice new DependsOnTargets
tag and changed ContentFiltered
name:
<Target Name="WordToPdf"
Inputs="@(ContentFiltered)"
Outputs="@(ContentFiltered -> '%(RootDir)%(Directory)%(Filename).pdf' )"
DependsOnTargets="FilterContent">
<!--
...
your actual target body here
...
-->
</Target>
<!-- New target to pre-filter list -->
<Target Name="FilterContent">
<ItemGroup>
<ContentFiltered Include="@(Content)" Condition="'%(Extension)' == '.docx'" />
</ItemGroup>
</Target>
这篇关于如何将项目转换添加到VS2012 .proj msbuild文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!