博客 / 詳情

返回

如何在 DotNet 中使用類似 golang 的 vendor 的編譯模式

作者:張富春(ahfuzhang),轉載時請註明作者和引用鏈接,謝謝!

  • cnblogs博客
  • zhihu
  • Github
  • 公眾號:一本正經的瞎扯

一個項目中,存在部分庫是公司內的,且需要配置個人的用户名和密碼連接到 NuGet 站點,才能下載這些庫。
而有時候需要直接把一個倉庫拉到某個環境上進行編譯,且不希望把帶了密碼的 NuGet.Config 文件提交到代碼倉庫中。
能不能把公司內的庫放到類似 golang 的 vendor 目錄下?然後編譯機器拉到倉庫後就能直接編譯,而不再依賴公司的 NuGet 站點了。

下面是我的做法:

下載所有依賴庫

在項目根目錄中定義一個配置完整的 NuGet.Config 文件,取名為: NuGet.Config.for.download

NuGet.Config.for.download

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear />
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
    <add key="Internal" value="https://my.company.com/repository/xxxx/" />
  </packageSources>

  <packageSourceMapping>
    <!-- Private packages should resolve from private feeds -->
    <packageSource key="Internal">
      <package pattern="MyCompany.*" />
    </packageSource>

    <!-- Public packages resolve from nuget.org -->
    <packageSource key="nuget.org">
      <package pattern="*" />
    </packageSource>
  </packageSourceMapping>

  <packageSourceCredentials>
    <Internal>
      <add key="Username" value="${USER}" />
      <add key="ClearTextPassword" value="${PASSWORD}" />
    </Internal>
  </packageSourceCredentials>
</configuration>

這個文件用於第一次使用時下載庫,這個文件不必提交到倉庫。

下載所有依賴庫

mkdir -p ./vendor/public/
dotnet restore --configfile ./NuGet.Config.for.download --packages ./vendor/public/

指定配置文件,把所有依賴項下載到 /vendor/public/ 目錄。

下載好後,把公司內部的庫移動到另一個目錄:

rm -fdr ./vendor/private/ ; mkdir -p ./vendor/private/ && \
	mv ./vendor/public/MyCompany.* ./vendor/private/

編譯

用於編譯的 NuGet.Config 配置

新增一個專門用於 vendor 模式的 NuGet.Config 配置文件,取名為:NuGet.Config.vendor

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear />
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
    <add key="Internal" value="./vendor/private/" />
  </packageSources>

  <packageSourceMapping>
    <!-- Private packages should resolve from private feeds -->
    <packageSource key="Internal">
      <package pattern="MyCompany.*" />
    </packageSource>

    <!-- Public packages resolve from nuget.org -->
    <packageSource key="nuget.org">
      <package pattern="*" />
    </packageSource>
  </packageSourceMapping>

</configuration>

基於 vendor 目錄的 restore

dotnet restore --configfile ./NuGet.Config.vendor -r linux-x64

編譯

dotnet publish MyProj.csproj \
		--no-restore \
		-c Debug -r linux-x64 \
		-o ./build/linux/amd64/

最關鍵的參數是 --no-restore

docker 中編譯

docker run --rm  \
	--platform linux/amd64 \
	-v "./":/src \
	-w /src/ \
	mcr.microsoft.com/dotnet/sdk:8.0 \
	bash -c ' \
		dotnet restore --configfile ./NuGet.Config.vendor -r linux-x64 && \
		dotnet publish MyProj.csproj \
			--no-restore \
			-c Debug -r linux-x64 \
			-p:PublishAot=false \
			-p:PublishSingleFile=false \
			-p:IncludeNativeLibrariesForSelfExtract=true \
			-p:EnableCompressionInSingleFile=true \
			-p:GenerateDocumentationFile=false \
			-p:StripSymbols=false \
			-p:DebugType=portable \
			-p:DebugSymbols=true \
			-p:EmbedUntrackedSources=true \
			-p:EmbedAllSources=true \
			-p:ContinuousIntegrationBuild=true \
			-p:Optimize=false \
			-o /src/build/linux/amd64/ \
	'

提交到 git

.gitignore 文件中增加一些配置:

NuGet.Config.for.download  # 用於下載的文件,不提交到倉庫,裏面有密碼
/vendor/public/  # public 文件太多了,不提交到倉庫
!/vendor/private/  # 説明這個目錄一定要提交
!/vendor/private/**/*nupkg  # .nupkg 類型的文件一定要提交

好了,在別的環境 git clone 後,使用上述提到的編譯命令行就可以編譯了。

希望對你有用, Have fun. 😃

user avatar
0 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.