【问题标题】:Maven equivalent in .NET C#.NET C# 中的 Maven 等效项
【发布时间】:2018-07-13 18:38:59
【问题描述】:

假设有两个 Maven Java 项目,A 和 B。A 依赖于 B。B 放置在远程 Maven 存储库和 GitHub 上。

在 IntelliJ Idea IDE 中,我将在两个单独的窗口中打开项目 A 和 B(B 是从 GitHub 克隆的)。

现在,B 有一个名为 Car 的类。当我在项目 A 中的类 Car 上“Ctrl+鼠标左键单击”时,IDE 会自动将我切换到打开的项目 B 中的源代码。因此我可以轻松地在 A 和 B 上一起工作。

如何使用 .NET C# 和 Visual Studio 实现相同的行为?

【问题讨论】:

    标签: java c# visual-studio maven intellij-idea


    【解决方案1】:

    .NET 生态系统中 Maven 的大致等价物是NuGet。 NuGet 文件可以是 created 使用 IDE 或命令行工具,例如 dotnet packnuget pack。 NuGet 文件只是带有.nupkg 扩展名的常规.zip 文件。这些文件可以托管在http://www.nuget.org 上供公众使用,也可以托管在http://www.myget.org 等网站或private hosted NuGet server 上。

    NuGet 工具还能够create debug symbol packages 包含源代码文件。调试符号包可以托管在公共或私有符号服务器上,used in Visual Studio 可以单步执行代码,并在 Visual Studio 中启用配置选项。

    如果您在 Visual Studio 中打开项目 A 并且启用了 automatic package restore 并且该项目具有指向项目 B 的 package reference,则在构建项目时,它将自动下载项目 B 的 NuGet 文件,将其解压缩到本地NuGet 缓存,并在您的项目中使用项目 B 的程序集。

    如果 Visual Studio 是configured correctly to find a debug symbols package 对应于项目 B 的确切版本,它将允许调试器单步执行项目 B 的代码。

    AFAIK,打开项目 B 的代码文件,然后设置断点是不可能的(如果这是错误的,请留下评论),您需要在项目 A 中设置一个断点,然后当您进入该行时调用/实例化 Car 类,Visual Studio 将打开代码文件,以便您可以单步执行。

    【讨论】:

      【解决方案2】:

      嗯,实际上是有可能的。但是一些 MSBuild 修改是必要的。

      前提条件:

      • 使用 VS 2017 附带的新 MSBuild 格式

      一步一步:

      • 创建解决方案并添加两个项目
      • 我假设项目 A 具有对项目 B 的包引用,并且 nuget 包名称与项目名称相同
      • 在两个项目的根目录(可能不属于任何源代码控制系统)中,创建一个名为 Directory.Build.targets 的文件,其内容如下:

        真的

          <PropertyGroup>
            <SolutionFileContent>$([System.IO.File]::ReadAllText($(SolutionPath)))</SolutionFileContent>
            <SmartSolutionDir>$([System.IO.Path]::GetDirectoryName( $(SolutionPath) ))</SmartSolutionDir>
            <RegexPattern>(?&lt;="[PackageName]", ")(.*)(?=", ")</RegexPattern>
          </PropertyGroup>
        
          <ItemGroup>
        
            <!-- Keep the identity of the  packagereference -->
            <SmartPackageReference Include="@(PackageReference)">
              <PackageName>%(Identity)</PackageName>
              <InSolution>$(SolutionFileContent.Contains('\%(Identity).csproj'))</InSolution>
            </SmartPackageReference>
        
            <!-- Filter them by mapping them to another itemGroup using the WithMetadataValue item function -->
            <PackageInSolution Include="@(SmartPackageReference -> WithMetadataValue('InSolution', True) )">
              <Pattern>$(RegexPattern.Replace('[PackageName]','%(PackageName)') )</Pattern>
              <SmartPath>$([System.Text.RegularExpressions.Regex]::Match( '$(SolutionFileContent)', '%(Pattern)' ))</SmartPath>
            </PackageInSolution>
        
            <ProjectReference  Include="@(PackageInSolution -> '$(SmartSolutionDir)\%(SmartPath)' )"/>
        
            <!-- Remove the package references that are now referenced as projects -->
            <PackageReference Remove="@(PackageInSolution -> '%(PackageName)' )"/>
          </ItemGroup>
        
        </When>
        

      这里发生的情况是,当所有包引用包含在当前打开的解决方案中时,它们会自动动态替换为项目引用。

      有关更多信息,我还在 MSBuild github 问题中发布了该解决方案: https://github.com/dotnet/sdk/issues/1151

      【讨论】:

        猜你喜欢
        • 2013-02-15
        • 1970-01-01
        • 2010-09-20
        • 2010-10-19
        • 2011-12-22
        • 2013-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多