【问题标题】:Build project from MSBuild to different out directory从 MSBuild 构建项目到不同的输出目录
【发布时间】:2017-07-27 19:38:19
【问题描述】:

我花了很多时间解决 MSBuild 任务和输出目录的问题。 这是我在 msbuild 文件中的目标任务:

<Target Name="CompileService">
        <MSBuild Projects="$(ServiceProj)"
            Targets="Clean;Rebuild"
            Properties="Configuration=%(BuildConfig.Identity);
            OutputPath=..\..\..\..\..\$(OutServiceDirectory)\%(BuildConfig.AppServer)"/>
    </Target>

文件放在两个文件夹中,一些 dll 和 exe 文件放在一个目录,不同的 dll 放在第一个目录之上的不同目录。

当输出路径如下:

OutputPath=D:\SomeLocation\$(PackageTempBackServiceProj)\%(BuildConfig.AppServer)"/>

工作正常。所有文件都进入该文件夹。

问题:我不想硬编码路径。如何编写正确的输出目录。

【问题讨论】:

  • 您是否尝试过从命令行运行 msbuild 而不是将其写入 XML?然后您可以使用/P 参数指定属性。这也将允许您在运行 msbuild 命令之前扩展您尝试输出的路径。
  • 不,因为它是大型解决方案的一部分。休息工作正常。我需要为存储在参数文件中的 dev\test 环境构建 Windows 服务。我构建是因为每个环境都需要 application.cofig 转换。

标签: visual-studio-2015 build msbuild continuous-integration


【解决方案1】:

如果您查看 Microsoft.Common.targets,您会看到 Build 目标的默认定义如下:

<Target
    Name="Build"
    Condition=" '$(_InvalidConfigurationWarning)' != 'true' "
    DependsOnTargets="$(BuildDependsOn)"
    Outputs="$(TargetPath)"/>
</Target>

此目标的目的是定义它所依赖的目标并创建输出。所以如果你想让所有文件都到一个目录,你必须为/p:OutputPath=””指定一个绝对路径,因为相对路径将相对于每个项目文件。

作为您不想使用绝对路径且无法使用 /P 参数指定属性的替代解决方案,您可以尝试使用以下解决方法

(1) 使用Move Task 中的移动任务 将 MSBuild 输出文件合并到一个文件夹:

    <ItemGroup>
     <FilesToMove Include="..\..\..\..\..\ $(OutServiceDirectory)\%(BuildConfig.AppServer)\*.dll"/>
    </ItemGroup>

    <Target Name="CombineOutputFile" AfterTargets="Build">
     <Move SourceFiles="@(FilesToMove)" DestinationFolder="${DestinationFolder}" />
    </Target>

(2) 扩展正常构建的行为以创建正确的输出,请参阅How to get all generated outputs 了解更多详细信息:

<Target Name="Build"
    DependsOnTargets="$(BuildDependsOn)"
    Outputs="@(AllOutputs->'%(FullPath)')">

<CreateItem Include="$(OutputPath)\**\*">
  <Output ItemName="AllOutputs" TaskParameter="Include"/>
</CreateItem>

希望这些可以帮助你。

【讨论】:

  • 我可以从 Jenkins 读取工作目录并插入到输出中吗?
  • @Bartolini,据我所知,答案是肯定的,但我不是 Jenkins 专家。对于这个问题,我建议你可以提交一个新的帖子,你会得到一个更专业的答案。如果以上答案解决了您的问题?如果是,您可以将其标记为有利于其他有相同问题的社区的答案。如果没有,请告诉我有关此问题的最新信息吗?谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-01
  • 2021-07-29
  • 2017-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多