首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将项转换添加到VS2012 .proj msbuild文件中

如何将项转换添加到VS2012 .proj msbuild文件中
EN

Stack Overflow用户
提问于 2013-04-24 20:58:39
回答 1查看 641关注 0票数 1

基于这个答案describing an item transform to convert image files from jpg to png,我进行了一个项转换,将.docx文件转换为.pdf。当我从我的projectname.proj构建文件调用它时,我会收到以下错误消息:

代码语言:javascript
复制
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

我怎么才能把这事做好?

我的目标是:

代码语言:javascript
复制
    <?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>

并从我的projectname.proj文件中从BeforeBuild目标调用它,如下所示:

代码语言:javascript
复制
  <Import Project="$(MSBuildTasksPath)\WordToPdf.Tasks.target" />

  ....

  <Target Name="BeforeBuild">
    <CallTarget Targets="WordToPdf" />
  </Target>

更新

这个目标有更多的错误,不仅仅是为了实现转换。对于将来的参考,下面是最后的工作代码:

代码语言:javascript
复制
<?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脚本:

代码语言:javascript
复制
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文件复制到其输出位置。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-26 13:05:37

您可以通过创建单独的目标来解决这一问题,该目标根据您的情况对项目进行预筛选--实际上,创建新的列表。

下面是示例-注意新的DependsOnTargets标记和更改的ContentFiltered名称:

代码语言:javascript
复制
  <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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16202062

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档