【问题标题】:How to get the OutputPath of a project in a Nuke build如何在 Nuke 构建中获取项目的 OutputPath
【发布时间】:2020-06-24 08:02:22
【问题描述】:

我正在向我的解决方案添加一个Nuke 构建项目。

我需要创建一个将编译文件复制到自定义文件夹的目标。您可以将其视为一种部署。

如何获取特定项目的输出文件夹?

比如项目名为“MyProject”,在C:\git\test\MyProject文件夹下,我需要根据当前配置和平台获取输出路径,如C:\git\test\MyProject\bin\x64\Release

我试过这个,但它给了我OutputPath 属性的第一个值,而不是当前配置和平台的值:

readonly Configuration Configuration = Configuration.Release;
readonly MSBuildTargetPlatform Platform = MSBuildTargetPlatform.x64;

// ...

Target LocalDeploy => _ => _
    .DependsOn(Compile)
    .Executes(() =>
    {
        var myProject = Solution.GetProject("MyProject");
        var outputPath = myProject.GetProperty("OutputPath"); // this returns bin\Debug
        var fullOutputPath = myProject.Directory / outputPath;
        CopyDirectoryRecursively(fullOutputPath, @"C:\DeployPath");
    });

我也尝试过这种方式,它尊重配置而不是平台:

    var myProject = Solution.GetProject("MyProject");
    var myMSBuildProject = visionTools3Project.GetMSBuildProject(Configuration);
    var outputPath = myProject.GetProperty("OutputPath"); // this returns bin\Release
    var fullOutputPath = myProject.Directory / outputPath;
    CopyDirectoryRecursively(fullOutputPath, @"C:\DeployPath");

【问题讨论】:

    标签: c# build nuke-build


    【解决方案1】:

    这是我最终使用的解决方法。

    readonly Configuration Configuration;
    readonly MSBuildTargetPlatform Platform;
    
    //...
    
    Target Example => _ => _
        .DependsOn(Compile)
        .Executes(() =>
        {
            var project = Solution.GetProject("MyProject");
            var outputPath = GetOutputPath(project);
            // ...
        });
    
    private AbsolutePath GetOutputPath(Project project)
    {
        var relativeOutputPath = (RelativePath)"bin";
    
        if (Platform == MSBuildTargetPlatform.x64)
        {
            relativeOutputPath = relativeOutputPath / "x64";
        }
    
        relativeOutputPath = relativeOutputPath / Configuration;
    
        return project.Directory / relativeOutputPath;
    }
    
    

    它是硬编码的,它没有考虑 netstandard 项目的默认输出路径。希望它可以作为那些试图解决相同问题的人的起点。

    【讨论】:

      【解决方案2】:

      一个简单的方法是构建输出文件夹的路径,如下所示:

      AbsolutePath OutputDirectory = RootDirectory / "MyProjectFolder" / "bin" / Configuration / "netstandard2.0";
      

      【讨论】:

      • 谢谢,我最终得到了类似的解决方案,但它不是最好的,因为它是硬编码的。
      • 没问题。也许你可以分享你的方法,@PaoloFulgoni?这对尝试这样做的其他人很有用。
      • 好主意,我刚刚分享了它。如您所见,与您的方法非常相似
      猜你喜欢
      • 1970-01-01
      • 2020-12-02
      • 2017-04-07
      • 2018-04-29
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多