2013-04-24 43 views
1

基于这个答案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 &quot;&amp; {&amp;&apos;$(ScriptLocation)&apos; -WordFilename &apos;/Input:%(Sublist.FullPath)&apos; }&quot;" /> 

     <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 &quot;&amp; {&amp;&apos;$(ScriptLocation)&apos; -WordFilename &apos;%(ContentFiltered.FullPath)&apos; }&quot;" /> 

    <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文件复制到其输出位置。

+0

很难说没有看到'WordToPdf.Tasks.target' – KMoraz 2013-04-24 23:32:10

+0

@KMoraz目标是错误消息后的第一个代码块。这是文件的全部内容。 – 2013-04-25 00:05:55

回答

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> 
+0

这样做的窍门 - 我也修复了缺少的ItemGroup元素后。我用最终的工作代码更新了我的问题。你称之为'解决方法' - 那么这是一个错误吗? – 2013-04-26 16:39:58

+0

是的,我认为是 – Lanorkin 2013-04-26 18:41:46

相关问题