的MSBuild:确保目标之前,任何其他构建步骤运行 [英] MSBuild: Ensure a target is run before any other build steps

查看:121
本文介绍了的MSBuild:确保目标之前,任何其他构建步骤运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有更新,以反映下一个发布项目的版本之前发生的步骤任何其他构建AssemblyInfo.cs文件。



在我的项目文件我年底前加入:



<预类=郎咸平的XML prettyprint-覆盖> <导入项目=$(PROJECTDIR)Properties\PublishVersion .proj/>



PublishVersion.proj看起来是这样的:



<预类=郎咸平的XML prettyprint-覆盖> <?XML版本=1.0编码=UTF-8>?;
<项目DefaultTargets =生成的xmlns =http://schemas.microsoft.com/developer/msbuild/2003>
<导入项目=$(MSBuildToolsPath)\Microsoft.CSharp.targets/>
<导入项目=$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets/>
<目标名称=BeforeBuild>
< FormatVersion版本=$(ApplicationVersion)版本=$(ApplicationRevision)>
<输出TaskParameter =OutputVersion属性名=SetVersion/>
< / FormatVersion>
< FileUpdate文件=$(PROJECTDIR)Properties\AssemblyInfo.cs
正则表达式=\d + \.\d + \.\d + \.\d +
ReplacementText =$(SetVersion)/>
< /目标>
< /项目>

现在建设完成,但肯定它开始之前没有之前它的地方执行,因为当我看着生成的EXE它那是在AssemblyInfo.cs中前为公司创造开始,但文件。应用和清单文件有新的版本不同的引用文件版本。



结果清单文件(1.0.0.0版本开始前,1.0.0.4版本后):



<预类=郎咸平的XML prettyprint-覆盖> < XML ?版本=1.0编码=UTF-8>
< asmv1:装配...>
< asmv1:assemblyIdentity名称=BHTechSupport.exe版本=1.0.0.4... />

<&为entryPoint GT;
< assemblyIdentity名称=BHTechSupport版本=1.0.0.0... />

< /为entryPoint>

<&依赖性GT;
< dependentAssembly ...的codebase =BHTechSupport.exe...>
< assemblyIdentity名称=BHTechSupport版本=1.0.0.0... />

< / dependentAssembly>
< /依赖性>

< / asmv1:装配>



让我怎么保证我的目标得到之前一切?

$ B $执行b

在更改PublishVersion.proj有时似乎没有生效,我需要清洗液和影响之前重新启动Visual Studio。


解决方案

也许你不得不使用 BeforeBuild 目标。




让我怎么保证我的目标得到的一切
,否则之前执行?




要检查你的目标执行,您可以更改 MSBuild项目生成输出的详细程度诊断。找到下工具>选项>项目和解决方案>生成和运行。然后生成该项目,并找到在输出窗口中你的目标。




在更改PublishVersion.proj有时似乎不采取
效果,我需要清洗液和影响之前重新启动Visual Studio




据我所知,在Visual Studio加载目标文件一次,所以你必须重新加载您的项目看到的结果。



更新我不知道你的版本控制系统,而这并不重要。反正,我试图为你提供一个简单的例子

 使用系统; 
:使用System.IO;使用Microsoft.Build.Utilities
;
使用Microsoft.Build.Framework;

命名空间Foo.Build.Tasks {

公共密封类GenerateVersion:任务{

[输出]
公共ITaskItem [] GeneratedFiles {搞定;私人集; }

公众覆盖BOOL执行(){
串objPath = Path.Combine(Path.GetDirectoryName(this.BuildEngine3.ProjectFileOfTaskNode)目标文件);
路径字符串= Path.Combine(objPathVersionInfo.cs);

File.WriteAllText(路径,@使用的System.Reflection;
[装配:的AssemblyVersion(2.0.0)]);

GeneratedFiles =新[] {新TaskItem(路径)};

返回真;
}

}

}

前面的任务生成包含的AssemblyVersion 元数据或你想要的一切。



最后,你有一个文件改变你的项目文件如下:

 < UsingTask TASKNAME =Foo.Build.Tasks.GenerateVersionAssemblyFile =富.Build.Tasks.dll/> 






 < ;目标名称=BeforeBuild> 
< GenerateVersion>
<输出TaskParameter =GeneratedFilesITEMNAME =编译/>
< / GenerateVersion>
< /目标>


I am trying to have the AssemblyInfo.cs file updated to reflect the next Publish version of the project BEFORE any other build steps occur.

in my project file i added before the end:

<Import Project="$(ProjectDir)Properties\PublishVersion.proj" />

PublishVersion.proj looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>
<Target Name="BeforeBuild">
    <FormatVersion Version="$(ApplicationVersion)" Revision="$(ApplicationRevision)">
        <Output TaskParameter="OutputVersion" PropertyName="SetVersion" />
    </FormatVersion>
    <FileUpdate Files="$(ProjectDir)Properties\AssemblyInfo.cs"
       Regex="\d+\.\d+\.\d+\.\d+"
       ReplacementText="$(SetVersion)" />
</Target>
</Project>

Now it does execute somewhere before building completes but definitely not before it starts, because when i look at the generated exe it has a file version that was in AssemblyInfo.cs BEFORE build started but the .application file and manifest file have various references to the new version.

resulting manifest file(1.0.0.0 before build start, 1.0.0.4 after build):

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly ...>
  <asmv1:assemblyIdentity name="BHTechSupport.exe" version="1.0.0.4" ... />
  ...
  <entryPoint>
    <assemblyIdentity name="BHTechSupport" version="1.0.0.0" ... />
    ...
  </entryPoint>
  ...
  <dependency>
    <dependentAssembly ... codebase="BHTechSupport.exe" ...>
      <assemblyIdentity name="BHTechSupport" version="1.0.0.0" ... />
      ...
    </dependentAssembly>
  </dependency>
  ...
</asmv1:assembly>

so how do i ensure that my target gets executed before EVERYTHING else?

making changes to PublishVersion.proj sometimes seem to not take effect and i need to clean the solution and restart visual studio before effecting.

解决方案

Maybe you have to use BeforeBuild target.

so how do i ensure that my target gets executed before EVERYTHING else?

To check your target execution, you can change the MSBuild project build output verbosity to Diagnostic. Find it under the Tools > Options > Projects and Solutions > Build and Run. Then build the project and find your target in the output window.

making changes to PublishVersion.proj sometimes seem to not take effect and i need to clean the solution and restart visual studio before effecting.

AFAIK, the Visual Studio loads target files once, so you have to reload your project to see the result.

Update I don't know your versioning system, whereas it's not important. Anyway, I tried to provide a simple example for you.

using System;
using System.IO;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;

namespace Foo.Build.Tasks {

    public sealed class GenerateVersion : Task {

        [Output]
        public ITaskItem[] GeneratedFiles { get; private set; }

        public override bool Execute() {
            string objPath = Path.Combine(Path.GetDirectoryName(this.BuildEngine3.ProjectFileOfTaskNode), "obj");
            string path = Path.Combine(objPath, "VersionInfo.cs");

            File.WriteAllText(path, @"using System.Reflection;
[assembly: AssemblyVersion(""2.0.0"")]");

            GeneratedFiles = new[] { new TaskItem(path) };

            return true;
        }

    }

}

The preceding task generates a file that contains AssemblyVersion metadata or everything you want.

At last, you have to change your project file as follows:

<UsingTask TaskName="Foo.Build.Tasks.GenerateVersion" AssemblyFile="Foo.Build.Tasks.dll" />


<Target Name="BeforeBuild">
  <GenerateVersion>
    <Output TaskParameter="GeneratedFiles" ItemName="Compile" />
  </GenerateVersion>
</Target>

这篇关于的MSBuild:确保目标之前,任何其他构建步骤运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆