msbuild 脚本中的其他路径

发布于 2024-10-22 02:03:32 字数 727 浏览 2 评论 0原文

如何为 MSBuild 任务指定其他程序集引用路径?

到目前为止,我有以下脚本,但无法弄清楚如何指定其他搜索路径。

<ItemGroup>
 <ProjectsToBuild Include="..\Main\Main.sln" />
</ItemGroup>

<!-- The follwing paths should be added to reference search paths for the build tasks -->
<ItemGroup>
 <MyAddRefPath Include="$(MSBuildProjectDirectory)\..\..\Build\Lib1" />
 <MyAddRefPath Include="$(MSBuildProjectDirectory)\..\..\Build\Lib2" />
</ItemGroup>

<MSBuild
 Projects="@(ProjectsToBuild)"
 Properties="Configuration=Debug;OutputPath=$(BuildOutputPath)">
</MSBuild>

更新:

请显示一个完整的工作脚本,该脚本调用原始项目,例如具有多个附加参考路径的 SLN。

没有关于如何改进项目结构的建议。 我知道如何建造一个好的结构,但现在的任务是建造一个现有的垃圾。

How to specify additional assembly reference paths for the MSBuild tasks?

I have following script so far, but can't figure out how to specify additional search paths.

<ItemGroup>
 <ProjectsToBuild Include="..\Main\Main.sln" />
</ItemGroup>

<!-- The follwing paths should be added to reference search paths for the build tasks -->
<ItemGroup>
 <MyAddRefPath Include="$(MSBuildProjectDirectory)\..\..\Build\Lib1" />
 <MyAddRefPath Include="$(MSBuildProjectDirectory)\..\..\Build\Lib2" />
</ItemGroup>

<MSBuild
 Projects="@(ProjectsToBuild)"
 Properties="Configuration=Debug;OutputPath=$(BuildOutputPath)">
</MSBuild>

UPDATE:

Please show one complete working script which invokes original project, such as an SLN with multiple additional reference paths.

No suggestions on how to improve the project structure please.
I know how to build a good structure, but now it's the task of building an existing piece of crap.

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

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

发布评论

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

评论(4

最初的梦 2024-10-29 02:03:32

我终于想出了如何做到这一点:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<ItemGroup>
    <ProjectsToBuild Include="ConsoleApplication1\ConsoleApplication1.csproj" />
</ItemGroup>

<ItemGroup>
    <AdditionalReferencePaths Include="..\Build\ClassLibrary1" />
    <AdditionalReferencePaths Include="..\Build\ClassLibrary2" />
</ItemGroup>

<PropertyGroup>
    <BuildOutputPath>..\Build\ConsoleApplication1</BuildOutputPath>
</PropertyGroup>

<Target Name="MainBuild">
    <PropertyGroup>
        <AdditionalReferencePathsProp>@(AdditionalReferencePaths)</AdditionalReferencePathsProp>
    </PropertyGroup>
    <MSBuild
        Projects="ConsoleApplication1\ConsoleApplication1.csproj"
        Properties="ReferencePath=$(AdditionalReferencePathsProp);OutputPath=$(BuildOutputPath)"
    >
    </MSBuild>
</Target>

I have finaly figured out how to do it:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<ItemGroup>
    <ProjectsToBuild Include="ConsoleApplication1\ConsoleApplication1.csproj" />
</ItemGroup>

<ItemGroup>
    <AdditionalReferencePaths Include="..\Build\ClassLibrary1" />
    <AdditionalReferencePaths Include="..\Build\ClassLibrary2" />
</ItemGroup>

<PropertyGroup>
    <BuildOutputPath>..\Build\ConsoleApplication1</BuildOutputPath>
</PropertyGroup>

<Target Name="MainBuild">
    <PropertyGroup>
        <AdditionalReferencePathsProp>@(AdditionalReferencePaths)</AdditionalReferencePathsProp>
    </PropertyGroup>
    <MSBuild
        Projects="ConsoleApplication1\ConsoleApplication1.csproj"
        Properties="ReferencePath=$(AdditionalReferencePathsProp);OutputPath=$(BuildOutputPath)"
    >
    </MSBuild>
</Target>

七色彩虹 2024-10-29 02:03:32

您要修改的属性是AssemblySearchPaths。有关详细信息,请参阅ResolveAssemblyReference 任务

<Target Name="AddToSearchPaths">  
  <CreateProperty Value="x:\path\to\assemblies;$(AssemblySearchPaths)">  
    <Output PropertyName="AssemblySearchPaths" TaskParameter="Value" />  
  </CreateProperty>  
</Target>

使用项目组,如您的示例所示,它看起来像:

<Target Name="AddToSearchPaths">  
  <CreateProperty Value="@(MyAddRefPath);$(AssemblySearchPaths)">  
    <Output PropertyName="AssemblySearchPaths" TaskParameter="Value" />  
  </CreateProperty>  
</Target>

查看 %WINDIR%\Microsoft.NET\Framework\v2.0.50727\Microsoft.Common.targets,您可以看到 ResolveAssemblyReference 任务 作为 ResolveAssemblyReferences目标。因此,您希望新添加的目标在执行 ResolveAssemblyReferences 之前修改 AssemblySearchPaths 属性。

The property you want to modify is AssemblySearchPaths. See the ResolveAssemblyReference task more information.

<Target Name="AddToSearchPaths">  
  <CreateProperty Value="x:\path\to\assemblies;$(AssemblySearchPaths)">  
    <Output PropertyName="AssemblySearchPaths" TaskParameter="Value" />  
  </CreateProperty>  
</Target>

Making use of item groups, as in your example, it would look like:

<Target Name="AddToSearchPaths">  
  <CreateProperty Value="@(MyAddRefPath);$(AssemblySearchPaths)">  
    <Output PropertyName="AssemblySearchPaths" TaskParameter="Value" />  
  </CreateProperty>  
</Target>

Looking in %WINDIR%\Microsoft.NET\Framework\v2.0.50727\Microsoft.Common.targets, you can see that the ResolveAssemblyReference Task is executed as part of the ResolveAssemblyReferences target. Thus, you want the newly added target to modify the AssemblySearchPaths property before ResolveAssemblyReferences is executed.

诗酒趁年少 2024-10-29 02:03:32

您已经声明您希望能够修改程序集搜索路径而不直接修改项目文件。为了满足该要求,您需要设置一个将覆盖 AssemblySearchPaths 的环境变量。使用此技术,您将需要提供解决方案中所有项目使用的每个程序集引用路径。 (修改项目或项目副本会更容易。请参阅最终评论。)

一种技术是创建一个批处理文件,该批处理文件在设置环境变量时运行脚本:

set AssemblySearchPaths="C:\Tacos;C:\Burritos;C:\Chalupas"
msbuild whatever.msbuild

另一种方法是在自定义 msbuild 文件中定义 PropertyGroup(否则称为完成这项工作所需的“钩子”):

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <ProjectsToBuild Include="..\Main\Main.sln" />
    </ItemGroup>

    <PropertyGroup>
          <AssemblySearchPaths>$(MSBuildProjectDirectory)\..\..\Build\Lib1;$(MSBuildProjectDirectory)\..\..\Build\Lib2</AssemblySearchPaths>
    </PropertyGroup>

    <Target Name="Build">
        <MSBuild Projects="@(ProjectsToBuild)" Properties="AssemblySearchPaths=$(AssemblySearchPaths);Configuration=Debug;OutputPath=$(OutputPath)" />
    </Target>
</Project>

现在,如果是我,并且由于某种无法解释的原因,我无法修改项目文件以包含我将要构建的更新的引用,我会复制项目文件,将它们加载到 IDE 中,并更正副本中的引用。同步项目变成了一个简单的差异/合并操作,使用 Mercurial 等现代工具可以自动完成(我确信 Clearcase 也可以管理它)。

You've stated that you want to be able to modify the assembly search paths without modifying the project files directly. In order to accomplish that requirement you need to set an environment variable that will override the AssemblySearchPaths. With this technique you will need to provide every assembly reference path used by all the projects in the solutions. (Modifying the projects or copies of the projects would be easier. See final comments.)

One technique is to create a batch file that runs your script at sets the environment variable:

set AssemblySearchPaths="C:\Tacos;C:\Burritos;C:\Chalupas"
msbuild whatever.msbuild

Another way is to define a PropertyGroup in your custom msbuild file (otherwise known as the "hook" needed to make this work):

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <ProjectsToBuild Include="..\Main\Main.sln" />
    </ItemGroup>

    <PropertyGroup>
          <AssemblySearchPaths>$(MSBuildProjectDirectory)\..\..\Build\Lib1;$(MSBuildProjectDirectory)\..\..\Build\Lib2</AssemblySearchPaths>
    </PropertyGroup>

    <Target Name="Build">
        <MSBuild Projects="@(ProjectsToBuild)" Properties="AssemblySearchPaths=$(AssemblySearchPaths);Configuration=Debug;OutputPath=$(OutputPath)" />
    </Target>
</Project>

Now if it were me, and for whatever unexplained reason I couldn't modify the project files to include the updated references that I am going to build with, I would make copies of the project files, load them into the IDE, and correct the references in my copies. Synching the projects becomes a simple diff/merge operation which is automatic with modern tools like mercurial (heck I'm sure clearcase could manage it too).

趴在窗边数星星i 2024-10-29 02:03:32

...并且请记住,您不需要为此使用目标,您可以使用项目范围的属性或项目,因为...

<ItemGroup>
    <MyAddRefPath Include="$(MSBuildProjectDirectory)\..\..\Build\Lib1" />
    <MyAddRefPath Include="$(MSBuildProjectDirectory)\..\..\Build\Lib2" />    
</ItemGroup>
<PropertyGroup>
    <MyAddRefPath>$(MSBuildProjectDirectory)\..\..\Build\Lib3</MyAddRefPath>
    <!-- add in the property path -->
    <AssemblySearchPaths>$(MyAddRefPath);$(AssemblySearchPaths)</AssemblySearchPaths>
    <!-- add in the item paths -->
    <AssemblySearchPaths>@(MyAddRefPath);$(AssemblySearchPaths)</AssemblySearchPaths>
</PropertyGroup>

...如果您确实需要在目标中执行此操作来拾取路径从动态填充的项目组中,使用内联属性,而不是 CreateProperty 任务(如果您没有陷入 v2.0)

<Target Name="AddToSearchPaths">
    <PropertyGroup>
        <!-- add in the item paths -->
        <AssemblySearchPaths>@(MyDynamicAddRefPath);$(AssemblySearchPaths)</AssemblySearchPaths>
    </PropertyGroup>
</Target>

...and remember that you don't need to use a target for this, you can use project-scoped properties or items, as...

<ItemGroup>
    <MyAddRefPath Include="$(MSBuildProjectDirectory)\..\..\Build\Lib1" />
    <MyAddRefPath Include="$(MSBuildProjectDirectory)\..\..\Build\Lib2" />    
</ItemGroup>
<PropertyGroup>
    <MyAddRefPath>$(MSBuildProjectDirectory)\..\..\Build\Lib3</MyAddRefPath>
    <!-- add in the property path -->
    <AssemblySearchPaths>$(MyAddRefPath);$(AssemblySearchPaths)</AssemblySearchPaths>
    <!-- add in the item paths -->
    <AssemblySearchPaths>@(MyAddRefPath);$(AssemblySearchPaths)</AssemblySearchPaths>
</PropertyGroup>

...and if you do need to do this in a target to pick up paths from a dynamically populated item group, use inline properties, not the CreateProperty task (if you are not stuck in v2.0)

<Target Name="AddToSearchPaths">
    <PropertyGroup>
        <!-- add in the item paths -->
        <AssemblySearchPaths>@(MyDynamicAddRefPath);$(AssemblySearchPaths)</AssemblySearchPaths>
    </PropertyGroup>
</Target>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文