将Postbuild事件添加到项目

背景

当我生成一个C#项目(csproj文件),然后对其进行编译时,由于msbuild某种原因,它无法识别prebuild和postbuild事件中的变量$(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文件中以使其在结果中的Import元素之后结束的最佳方法是什么XML

显然,[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文件
  • 添加postbuildevent: copy "$(ProjectDir)app.config.$(ConfigurationName)" "$(TargetDir)\$(ProjectName).exe.config
  • 看到项目生成和正确的配置文件被应用
  • 使用记事本删除构建前和构建后事件(以免留下任何痕迹)
  • 运行示例项目
  • 重新加载并生成您创建的项目。
  • 输出窗口现在会说 The system cannot find the file specified.
伊利亚·科热夫尼科夫(Ilya Kozhevnikov)
var propertyGroupElement = project.Xml.CreatePropertyGroupElement();
project.Xml.AppendChild(propertyGroupElement);
propertyGroupElement.AddProperty(PreBuildEventFixture, PreBuildEvent);
propertyGroupElement.AddProperty(PostBuildEventFixture, PostBuildEvent);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章