【问题标题】:Adding a Postbuild event to a project将 Postbuild 事件添加到项目
【发布时间】:2014-08-29 12:04:44
【问题描述】:

当我生成一个 C# 项目(csproj 文件)然后编译它时,msbuild 不知何故无法识别 pre-和 post-build 事件中的变量 $(ConfigurationName)$(ProjectDir)(和其他)。

当我手动将生成的.csproj 文件中的构建前和构建后事件配置进一步向下移动时,msbuild 会正确识别这些变量。

将 buildevent 添加到项目是我在保存项目之前做的最后一件事。

这是我添加它的方式:

using Microsoft.Build.Construction;
using Microsoft.Build.Evaluation;

private const string PreBuildEventFixture = "PreBuildEvent";
private const string PostBuildEventFixture = "PostBuildEvent";
private const string PreBuildEvent = "attrib -R \"$(ProjectDir)app.config\"";
private const string PostBuildEvent = "copy \"$(ProjectDir)app.config.$(ConfigurationName)\" \"$(TargetDir)\\$(ProjectName).dll.config\" \r\n attrib -R \"$(ProjectPath)\"";

public void AddBuildEvents(Project project)
{
    ProjectPropertyGroupElement propertyGroupElement = project.Xml.AddPropertyGroup();
    propertyGroupElement.AddProperty(PreBuildEventFixture, PreBuildEvent);
    propertyGroupElement.AddProperty(PostBuildEventFixture, PostBuildEvent);
}

我通过 msbuild 运行生成的项目时遇到的错误是这样的:

The command "copy "app.config." "\.dll.config"" exited with code 1

然后当我手动编辑 .csproj 文件(使用记事本或其他文本编辑器),剪切 pre-和 postbuild 事件,并将其粘贴到 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> 元素下方,然后 msbuild 构建生成的 .csproj 文件正常.

将构建事件添加到.csproj 文件以使其在生成的XML 中的Import 元素之后结束的最佳方法是什么?

显然,我目前使用[ProjectPropertyGroupElement][1] 的方式是从Microsoft.Build.Evaluation.ProjectXml 属性的AddPropertyGroup 请求它,这不是。

示例项目:

using System.IO;
using Microsoft.Build.Construction;
using Microsoft.Build.Evaluation;

class Program
{
    private const string PreBuildEventFixture = "PreBuildEvent";
    private const string PostBuildEventFixture = "PostBuildEvent";
    private const string PreBuildEvent = "attrib -R \"$(ProjectDir)app.config\"";
    private const string PostBuildEvent = "copy \"$(ProjectDir)app.config.$(ConfigurationName)\" \"$(TargetDir)\\$(ProjectName).exe.config\" \r\n attrib -R \"$(ProjectPath)\"";

    private const string ProjectFile = @"C:\test\TestProject\TestProject.csproj";

    static void Main(string[] args)
    {
        if (!File.Exists(ProjectFile))
            throw new FileNotFoundException("ProjectFile not found");

        ProjectCollection collection = new ProjectCollection();
        Project project = collection.LoadProject(ProjectFile);

        ProjectPropertyGroupElement propertyGroupElement = project.Xml.AddPropertyGroup();
        propertyGroupElement.AddProperty(PreBuildEventFixture, PreBuildEvent);
        propertyGroupElement.AddProperty(PostBuildEventFixture, PostBuildEvent);
        project.Save();
        collection.UnloadAllProjects();
    }
}

复制步骤

  • 创建一个新项目
  • 手动添加与 app.debug 文件不同的 app.config.debug 文件
  • 添加构建后事件:copy "$(ProjectDir)app.config.$(ConfigurationName)" "$(TargetDir)\$(ProjectName).exe.config
  • 查看项目构建并应用了正确的配置文件
  • 使用记事本删除构建前和构建后事件(以免留下任何痕迹)
  • 运行示例项目
  • 重新加载并构建您创建的项目。
  • 现在输出窗口会显示The system cannot find the file specified.

【问题讨论】:

  • 文件app.config. 似乎不存在(它缺少ConfigurationName)。您的手动构建事件是什么?
  • no $(ConfigurationName) 没有被解析。如果我在项目文件中将 buildevents 向下移动。
  • 猜测其中一个 Imports 负责设置变量,我弄乱了我的一个项目文件。在此之后我必须进行构建后事件: 或变量为空。您可以添加构建事件以使其位于底部吗?
  • 也许先添加它们?
  • 您是否尝试过使用 $(Configuration) 而不是 $(ConfigurationName)?我很确定,MSBuild 使用的是 Configuration,而不是 ConfigurationName。

标签: c# msbuild msbuild-propertygroup microsoft.build


【解决方案1】:
var propertyGroupElement = project.Xml.CreatePropertyGroupElement();
project.Xml.AppendChild(propertyGroupElement);
propertyGroupElement.AddProperty(PreBuildEventFixture, PreBuildEvent);
propertyGroupElement.AddProperty(PostBuildEventFixture, PostBuildEvent);

【讨论】:

    【解决方案2】:

    如果在实际构建项目之前添加了项目相关的宏(构建项目包括添加引用),则不会对其进行解析。而不是使用$(ProjectName),可以使用解决方案变量(已经存在)来构造路径,如下所示:

    copy "$(SolutionDir)ProjectName\app.config.$(Configuration)" "$(SolutionDir)ProjectName\bin\$(Configuration)\ProjectName.dll.config"

    请注意,ProjectName 是硬编码的项目的实际名称,但由于您正在生成项目,因此应该很容易添加。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-15
      • 1970-01-01
      • 1970-01-01
      • 2018-12-31
      • 2014-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多