我正在添加 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/