如何在MSBuild中覆盖ApplicationVersion for Publish目标

时间:2016-09-02 10:20:56

标签: msbuild clickonce

是否可以在ClickOnce上下文中覆盖Publish目标的ApplicationVersion?

我创建了一个这样的自定义构建:

<PropertyGroup>
    ...
    <ApplicationRevision>0</ApplicationRevision>
    <ApplicationVersion>1.0.0.0</ApplicationVersion>
    ...
    <CustomBuildDependsOn>
        Build;
        CustomBeforePublish;
        PublishOnly;
    </CustomBuildDependsOn>
</PropertyGroup>
<Target Name="CustomBuild" DependsOnTargets="$(CustomBuildDependsOn)" />
<Target Name="CustomBeforePublish">
    <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
        <Output TaskParameter="Assemblies" ItemName="CurrentAssembly" />
    </GetAssemblyIdentity>
    <PropertyGroup>
        <PublishDir>$(OutDir)\publish\$(TargetName)\</PublishDir>
        <ApplicationVersion>%(CurrentAssembly.Version)</ApplicationVersion>
    </PropertyGroup>
    <Message Text="BeforePublish PublishDir = $(PublishDir)" />
    <Message Text="BeforePublish ApplicationVersion = $(ApplicationVersion)" />
</Target>

属性PublishDir被正确覆盖并由PublishOnly目标使用,而属性ApplicationVersion不正确:应用程序清单始终使用版本1.0.0.0。 并显示日志:

  

BeforePublish ApplicationVersion = 3.5.0.51263

将ApplicationVersion传递给目标PublishOnly或Publish的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

我发现这样做的唯一方法就是用属性重新启动MSbuild:

use CAM::PDF;
my $doc = new CAM::PDF ( shift ) or die $CAM::PDF::errstr;

# delete image XObjects among resources
# but keep their names

my @names;

foreach my $objnum ( sort { $a <=> $b } keys %{ $doc->{xref} } ) {
  my $obj = $doc->dereference( $objnum );
  next unless $obj->{value}->{type} eq 'dictionary';

  my $n = $obj->{value}->{value};

  my $resources = $doc->getValue ( $n->{Resources}       ) or next;
  my $resource  = $doc->getValue ( $resources->{XObject} ) or next;

  foreach my $name ( sort keys $resource ) {
    my $im = $doc->getValue ( $resource->{$name} ) or next;

    next unless defined $im->{Type}
            and defined $im->{Subtype}
            and $doc->getValue ( $im->{Type}    ) eq 'XObject'
            and $doc->getValue ( $im->{Subtype} ) eq 'Image';

    delete $resource->{$name};                                                                                                           
    push @names, $name;                                                                                                                  
  }                                                                                                                                      
}                                                                                                                                        


# delete the corresponding Do operators                                                                                                                        

if ( @names ) {                                                                                                                                                               
  foreach my $p ( 1 .. $doc->numPages ) {                                                                                                                                     
    my $content = $doc->getPageContent ( $p );
    my $s;
    foreach my $name ( @names ) {
      ++$s if $content =~ s{( / \Q$name\E \s+ Do \b )} { ' ' x length $1 }xeg;
    }
    $doc->setPageContent ( $p, $content ) if $s;
  }
}

$doc->cleanse;
$doc->cleanoutput;

我不确定这是正确的方法,但它有效