3

我正在向我的解决方案中添加一个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");
4

3 回答 3

2

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

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

于 2020-04-14T09:23:39.447 回答
1

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

AbsolutePath OutputDirectory = RootDirectory / "MyProjectFolder" / "bin" / Configuration / "netstandard2.0";
于 2020-04-13T09:20:46.150 回答
0

这是一个非硬编码版本因为输出路径可能取决于配置等。如果需要,必须使用配置和目标框架配置解决方案

Solution.GetProject("MyProject")
.GetMSBuildProject(Configuration) // optional parameter with target Framework exists
.GetPropertyValue("OutputPath")
于 2022-02-17T10:39:45.020 回答