Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the OutputPath of a project in a Nuke build

I'm adding a Nuke build project to my solution.

I need to create a target which copies the compiled files to a custom folder. You can think of it as a sort of deploy.

How do I get the output folder of a specific project?

For example, if the project is called "MyProject" and it's in the C:\git\test\MyProject folder, I need to get the output path based on the current configuration and platform, such as C:\git\test\MyProject\bin\x64\Release.

I tried this one, but it gives me the first value of the OutputPath property, not the one for the current configuration and platform:

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");
    });

I also tried this way, which honor the Configuration but not the Platform:

    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");
like image 525
Paolo Fulgoni Avatar asked Nov 17 '25 09:11

Paolo Fulgoni


2 Answers

This is the workaround I finally used.

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;
}

It's quite hardcodeded and it doesn't take the the default output path of netstandard projects into account. Hopefully it can be useful as starting point for those who are trying to solve the same issue.

like image 160
Paolo Fulgoni Avatar answered Nov 20 '25 00:11

Paolo Fulgoni


A simple way to do this is to construct the path to your output folder like this:

AbsolutePath OutputDirectory = RootDirectory / "MyProjectFolder" / "bin" / Configuration / "netstandard2.0";
like image 34
Sean Kearon Avatar answered Nov 19 '25 22:11

Sean Kearon