c# - 如何在 Nuke 构建中获取项目的 OutputPath

标签 c# build nuke-build

我正在添加 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");

最佳答案

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

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 项目的默认输出路径。希望它可以作为那些试图解决相同问题的人的起点。

关于c# - 如何在 Nuke 构建中获取项目的 OutputPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60651242/

相关文章:

c# - 列表按字段包含

c# - 由于未初始化的静态属性,单元测试失败

msbuild - 如何使用 msBuild 在 nuke-build 中发布解决方案

c# - 如何在 Nuke.Build 6.2.1 中查找和使用 DotNetRestore、DotNetBuild 等方法

c# - 引用和修改 Game1 是不好的做法吗?

c# - 自托管 Wcf 服务于 wsdl 但在调用时为 404

java - Ant 仍然是 Java 构建工具的最佳选择吗?

java - R.java 构建时出错

java - 如何使用复合构建构建 3 个单独的 gradle 项目