使用 MsBuild 复制文件

发布于 2024-10-07 06:31:26 字数 304 浏览 0 评论 0原文

我有两个项目的解决方案。其中之一是一个简单的项目,不依赖于任何特殊的程序集来构建。但是,应用程序可以启动外部可执行文件(我也拥有它——这是我的解决方案的第二个项目)。当我构建项目时,我希望将外部项目的可执行文件(+依赖项,例如 app.config)复制到我的项目的目标目录下。

我如何使用 MsBuild 实现这一目标?我的第一个想法是添加对可执行文件的引用(即使我不需要它来构建)来复制文件。这对我来说似乎有点老套。

谢谢!

PS 另一个问题:假设我可以告诉 MsBuild 复制可执行文件。如果可执行文件未构建怎么办?我可以强制它构建吗?

I have a solution with two projects. One of them is a simple project that doesn't depend on any special assemblies to build. However, the application is allowed to launch an external executable (that I also own -- this is the second project of my solution). When I build the project, I would like the executable (+ dependencies such as app.config) of the external project to be copied under the target directory of my project.

How can I achieve this with MsBuild? My first idea was to add a reference to the executable (even though I don't need it to build) to copy the file. This seem a bit hacky to me.

Thanks!

P.S. Another question: Let's say that I can tell MsBuild to copy the executable. What if the executable isn't build? Can I force it to build?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

分开我的手 2024-10-14 06:31:26

在您的情况下,您应该让 MSBuild 脚本替换您的解决方案并在那里声明所需的依赖项:

<Project DefaultTargets="Executable_A">
    <Target Name="Executable_A" DependsOnTargets="Executable_B">
        <MSBuild Projects="Executable_A.proj" Targets="Build" />
    </Target>

    <Target Name="Executable_B">
        <MSBuild Projects="Executable_B.proj" Targets="Build">
            <Output TaskParameter="TargetOutputs" ItemName="AssembliesBuilt" />
        </MSBuild>
        <Copy SourceFiles="@(AssembliesBuilt)" DestinationFolder="C:\My_Target_Path\" SkipUnchangedFiles="true" />
    </Target>
</Project>

...并且您可以指示第二个项目的目标将其输出复制到所需的位置。您甚至可以指示第一个项目将其输出路径存储到一个变量中,以便在复制第二个项目的输出(DestinationFolder)时使用。

In your case you should make your MSBuild script replace your solution and declare the desired dependency there:

<Project DefaultTargets="Executable_A">
    <Target Name="Executable_A" DependsOnTargets="Executable_B">
        <MSBuild Projects="Executable_A.proj" Targets="Build" />
    </Target>

    <Target Name="Executable_B">
        <MSBuild Projects="Executable_B.proj" Targets="Build">
            <Output TaskParameter="TargetOutputs" ItemName="AssembliesBuilt" />
        </MSBuild>
        <Copy SourceFiles="@(AssembliesBuilt)" DestinationFolder="C:\My_Target_Path\" SkipUnchangedFiles="true" />
    </Target>
</Project>

... and you can instruct the target of your second project to copy its output to the desired location. You could even instruct your first project to store its output path into a variable to use when copying the output of your second project (DestinationFolder).

怎樣才叫好 2024-10-14 06:31:26

您是否查看过 MSBuild 复制任务:复制任务

Have you looked the MSBuild Copy Task: Copy Task ?

恏ㄋ傷疤忘ㄋ疼 2024-10-14 06:31:26

如果您对@Filburt 的回答感到满意,我可以建议改进这个解决方案。

如果您查看默认构建目标的工作原理:

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

您会看到构建或重建目标的唯一输出$(TargetPath)。
$(TargetPath) 是构建的程序集的完整路径。要更改此行为,您可以破解构建过程。但在项目B中定义自己的目标会更简单:

<Target
    Name="BuildWithConfig"
    DependsOnTargets="Clean;Build"
    Returns="$(TargetPath);$(TargetPath).config" />

A项目中只需定义AfterBuild目标:

<Target Name="AfterBuild">
    <MSBuild Projects="Executable_B.proj" Targets="BuildWithConfig">
        <Output TaskParameter="TargetOutputs" ItemName="AssembliesBuilt" />
    </MSBuild>
    <Copy SourceFiles="@(AssembliesBuilt)" DestinationFolder="$(OutDir)\SubDirForB" SkipUnchangedFiles="true" />   
</Target>

此解决方案不需要创建自定义构建脚本。直接在VS中得到结果。

If you will be satisfied with @Filburt answer I can suggest to improve this solution.

If you'll take a look how default Build target works:

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

You'll see that the only output from build or rebuild target is $(TargetPath).
$(TargetPath) is a full path to the built assembly. To change this behaviour you can hack build process. But it will be match simpler to define your own target in project B:

<Target
    Name="BuildWithConfig"
    DependsOnTargets="Clean;Build"
    Returns="$(TargetPath);$(TargetPath).config" />

In A project just define AfterBuild target:

<Target Name="AfterBuild">
    <MSBuild Projects="Executable_B.proj" Targets="BuildWithConfig">
        <Output TaskParameter="TargetOutputs" ItemName="AssembliesBuilt" />
    </MSBuild>
    <Copy SourceFiles="@(AssembliesBuilt)" DestinationFolder="$(OutDir)\SubDirForB" SkipUnchangedFiles="true" />   
</Target>

This solution doesn't require to create custom build script. You will get the result directly in VS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文