3

几天来我一直在与 NuGet 搏斗,现在我正沮丧地转向 StackOverflow——希望这里的某个人能善意地指出我正确的方向。

我曾多次将 NuGet 用于简单的单人宠物项目,但这是我第一次将它用于我真正关心并希望进行完全连续构建等的事情。我正在尝试创建一个简单的NAnt 构建脚本以获取 Git 的源代码,确保已关闭外部依赖项,编译并运行测试 - vanilla CI。

我最初尝试让解决方案恢复工作,但它没有工作,或者我没有工作。Visual Studio 不在构建服务器上,也不会安装在那里 - 这不是一个选项。顺便说一句,我无法仅与两个开发人员一起使用解决方案恢复工作(一个试图降低源代码并干净地构建)。我假设这是因为必须在任何地方打开“允许解决方案还原”(默认情况下不是)。在深入了解它之前,我曾尝试过这种方法 - 坦率地说,让我的包管理器与 IDE 如此紧密地耦合让我感到不舒服,并希望我能以另一种方式做到这一点。我习惯使用的包管理器是简单的命令行工具——CI 构建脚本在构建时调用它,开发人员按需执行。我' 在 NuGet 源代码的最后 30 分钟内,我花了两个小时试图让它工作。我觉得我正在与该工具作斗争,需要重新启动。

有没有人有任何在多开发人员 + CI 场景中使用 NuGet 的最佳示例?这就是我要的:

  1. 任何和所有开发人员都可以通过 3 次或更少的点击(最好是 1 次)获得源代码并运行测试。如果二进制文件在本地不存在,则将通过 JIT 获取。如果它们在那里,它们将在必要时进行更新等。理想情况下,这甚至不需要安装 NuGet(即 NuGet.exe 需要在我的存储库中)。
  2. 通过 Jenkins、TeamCity 等 CI 服务器执行 #1(最好使用相同的脚本)
  3. 如果它没有过度使用该工具,我希望使用单个 packages.config 文件将所有这些与 Visual Studio 断开连接,并将所有二进制文件转储到存储库根目录中的单个 Lib 文件夹中。

任何指针将不胜感激。

4

3 回答 3

0

TeamCity 有一个构建步骤,称为“ NuGet Installer ”,它从 .sln 文件中获取所需的包并下载到本地。它不需要运行 Visual Studio。

在此处阅读更多信息:http: //confluence.jetbrains.com/display/TCD7/NuGet+Installer

于 2013-05-20T18:36:52.293 回答
0

There are several different solutions for integrating NuGet into your build process depending on how much integration you require. In our case we wanted to use NuGet as package manager and allow developers to build their solutions even if they haven't got NuGet installed on their machine. For that to work we enabled package restore which adds the NuGet binaries to your solution folder and updates the project files. Note that NuGet doesn't always do the update of the project files correctly. In our case we found that some project files got updated but others didn't. To verify that the project was updated you will need to open the project file as XML file. To achieve this load the solution and right click the project in question and select unload project. Then right click the project again and select edit [PROJECT_NAME]. In the project file you should see

  • A RestorePackages property in the first propertygroup. This property should have the value true
  • An import statement at the very end of the project file. This import statement should point to the 'NuGet.targets file that accompanies the NuGet binary.

Below is an example of one of our project files (heavily edited)

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <SolutionDir Condition="'$(SolutionDir)' == '' or '$(SolutionDir)' == '*undefined*'">$(MSBuildProjectDirectory)\..</SolutionDir>
    <ProjectGuid>{8B467882-7574-41B2-B3A8-2F34DA84BE82}</ProjectGuid>
    <OutputType>Library</OutputType>
    <RootNamespace>MyCompany.MyNamespace</RootNamespace>
    <AssemblyName>MyCompany.MyNamespace</AssemblyName>

    <!-- Allow NuGet to restore the packages if they are missing -->
    <RestorePackages>true</RestorePackages>
  </PropertyGroup>
  <Import Project="$(SolutionDir)\BaseConfiguration.targets" />
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Core" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="MyClass.cs" />
    <!--
        .... MANY MORE FILES HERE
    -->
  </ItemGroup>

  <!-- Import the Nuget.targets file which integrates NuGet in the build process -->
  <Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
</Project>

The next step you'll need to take is to provide a solution level NuGet configuration file in which you'll indicate where the packages need to be 'installed' and what the URL of the package repository is. In our case the solution directory structure looks like:

(D) root
    (D) build
    (D) packages
    (D) source
        (D) .nuget
            NuGet.config
            NuGet.exe
            NuGet.targets
        (D) MyCoolProject
            MyCoolProject.csproj
        MyCoolProject.sln
    (D) templates
    NuGet.Config

Where (D) indicates a directory.

The NuGet.config file contains the following configuration settings.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageRestore>
    <add key="enabled" value="True" />
  </packageRestore>
  <config>
    <add key="repositorypath" value="packages" />
  </config>
  <packageSources>
    <add key="OurPackageServer" value="PACKAGE_SERVER_ADDRESS" />
  </packageSources>
  <activePackageSource>
    <add key="All" value="(Aggregate source)" />
  </activePackageSource>
</configuration>

This configuration file indicates that package restore is enabled, that the repository path (where the packages are placed) is the packages directory and which package sources are active.

By placing a NuGet.config file in the root directory we can use the hierarchical configuration option with NuGet. This allows the individual solutions to override computer specific configurations. The other benefit is that this way we don't need to have NuGet installed on the build server (because the executable and the configurations are in the repository).

With this setup developers can build the solution from Visual Studio. The build should work fine on developers machines even if they don't have NuGet installed. Note however that they won't be able to add packages to a project without having NuGet installed in visual studio. On the build server you can simply use MsBuild to build the solution which will automatically download the packages from your package repository. Visual Studio is not required to be installed on the build machine for that (just the .NET framework of your choice).

于 2013-05-21T02:08:08.377 回答
0

下面,我认为你可以如何实现你的每一个要求:

  1. 您需要在解决方案中“启用 NuGet 包还原”:http: //docs.nuget.org/docs/workflows/using-nuget-without-committing-packages
  2. 正如@alexander-doroshenko 为 TeamCity 提到的,您可以使用 Nuget 安装程序:http ://confluence.jetbrains.com/display/TCD7/NuGet+Installer ,但如果您想在 Jenkins 中运行脚本,请尝试这个(在 TC 也可以使用) ,作为命令行步骤)每个项目:

    nuget.exe 安装“[项目文件夹]/packages.config”-source“”-solutionDir“”-OutputDirectory“packages”

  3. 此要求将由第 1 项和第 2 项完成。

于 2013-05-21T01:18:51.197 回答