基于这个答案describing an item transform to convert image files from jpg to png,我做了一个项目转换,将.docx文件转换为.pdf。当我从projectname.proj构建文件中调用它时,我收到以下错误消息:
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 word to pdf item transform”的人 这是我的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文件复制到其输出位置。
答案 0 :(得分:1)
您可以通过创建单独的目标来解决此问题,该目标会根据您的条件对项目进行预过滤 - 实际上,会创建新列表。
以下是示例 - 请注意新的DependsOnTargets
代码并更改了ContentFiltered
名称:
<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>