基于这个答案describing an item transform to convert image files from jpg to png,我做了一个项目转换,将.docx文件转换为.pdf。当我把它从我的projectname.proj构建文件我收到此错误信息:如何添加项目转换到VS2012 .proj msbuild文件
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
我怎样才能使这项工作?
这里是我的目标:
<?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文件调用它,如下所示:
<Import Project="$(MSBuildTasksPath)\WordToPdf.Tasks.target" />
....
<Target Name="BeforeBuild">
<CallTarget Targets="WordToPdf" />
</Target>
更新
有一点这个目标不仅仅是解决变形问题的工作。对于未来的参考,下面是最终的工作代码:
<?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脚本:
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文件复制到其输出位置。
很难说没有看到'WordToPdf.Tasks.target' – KMoraz 2013-04-24 23:32:10
@KMoraz目标是错误消息后的第一个代码块。这是文件的全部内容。 – 2013-04-25 00:05:55