Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 3개 있습니다.)
(시리즈 글이 4개 있습니다.)
개발 환경 구성: 296. .NET Core 프로젝트를 NuGet Gallery에 배포하는 방법
; https://www.sysnet.pe.kr/2/0/11034

.NET Framework: 793. C# - REST API를 이용해 NuGet 저장소 제어
; https://www.sysnet.pe.kr/2/0/11696

개발 환경 구성: 423. NuGet 패키지 제작 - Native와 Managed DLL을 분리하는 방법
; https://www.sysnet.pe.kr/2/0/11793

개발 환경 구성: 494. NuGet - nuspec의 패키지 스키마 버전(네임스페이스) 업데이트 방법
; https://www.sysnet.pe.kr/2/0/12234




.NET Core 프로젝트를 NuGet Gallery에 배포하는 방법

이미 다른 글이 있지만. ^^

Nuget Gallery 서버에 내 패키지 등록하기 : Uploading package to nugget gallery
; http://funnygangstar.tistory.com/entry/Nuget-Gallery-%EC%84%9C%EB%B2%84%EC%97%90-%EB%82%B4-%ED%8C%A8%ED%82%A4%EC%A7%80-%EB%93%B1%EB%A1%9D%ED%95%98%EA%B8%B0-Uploading-package-to-nugget-gallery

.NET Core 프로젝트의 경우 "dotnet.exe pack" 명령어로 좀 더 쉽게 할 수 있는 방법이 제공됩니다. 이 글에서는 그 방법을 정리해 봤습니다.




우선, NuGet Gallery에 올릴 예제가 필요한데요. 대충 Visual Studio에서 .NET Core 라이브러리를 만들어줍니다. 그다음, 명령행에서 해당 프로젝트의 project.json 폴더가 있는 폴더로 이동해 "dotnet.exe pack" 명령을,

dotnet-pack
; https://learn.microsoft.com/en-us/dotnet/articles/core/tools/dotnet-pack

다음과 같이 실행해주기만 하면 됩니다. ^^

D:\MyNuGet\GosperCombination\src\GosperCombination>dotnet pack
Project GosperCombination (.NETStandard,Version=v1.6) was previously compiled. Skipping compilation.
Producing nuget package "GosperCombination.1.0.0" for GosperCombination
GosperCombination -> D:\MyNuGet\GosperCombination\src\GosperCombination\bin\Debug\GosperCombination.1.0.0.nupkg
Producing nuget package "GosperCombination.1.0.0.symbols" for GosperCombination
GosperCombination -> D:\MyNuGet\GosperCombination\src\GosperCombination\bin\Debug\GosperCombination.1.0.0.symbols.nupkg

보는 바와 같이 GosperCombination.1.0.0.nupkg 라는 파일이 생성되었는데요. 이것은 바이너리 파일로 NuGet Gallery에 올릴 .zip 파일이라고 보시면 됩니다. 실제로 확장자만 변경해서 zip 프로그램으로 내용을 볼 수 있습니다.

dotnet_pack_1.png

\
    [Content_Types].xml
    GosperCombination.nuspec

    \_rels
        .rels
		
    \lib
        \netstandard1.6
            GosperCombination.dll




이제 NuGet 패키지를 생성했으니, NuGet Gallery에 올리는 일만 남았습니다. 우선 nuget.exe 파일이 필요합니다.

NuGet - Installing
; https://docs.nuget.org/consume/command-line-reference
; https://dist.nuget.org/win-x86-commandline/latest/nuget.exe

위의 파일을 다운로드 받은 경로를 PATH 환경 변수에 등록해 주면 이후 편하게 사용할 수 있습니다.

그다음, NuGet 사이트에 방문해서 회원 가입을 한 후,

NuGet Gallery
; http://nuget.org

로그인을 하면 우측 상단에 자신의 ID에 해당하는 링크를 클릭해 "My NuGet.org Account" 화면으로 이동합니다. 거기서 API Key를 복사한 후 다음과 같이 실행해 줍니다.

D:\MyNuGet>nuget setApiKey ????????-????-????-????-???????????? -Source https://www.nuget.org/api/v2/package
The API Key '????????-????-????-????-????????????' was saved for 'https://www.nuget.org/api/v2/package'.

이렇게 한 번만 해주면, 이후 해당 컴퓨터에서는 API Key를 재차 입력할 필요가 없습니다. 따라서, 우리가 만든 GosperCombination.1.0.0.nupkg 패키지를 다음과 같은 명령어로 간단하게 업로드할 수 있습니다.

D:\MyNuGet\GosperCombination\src\GosperCombination\bin\Debug>nuget push GosperCombination.1.0.0.nupkg -Source https://www.nuget.org/api/v2/package
Pushing GosperCombination.1.0.0.nupkg to 'https://www.nuget.org/api/v2/package'...
  PUT https://www.nuget.org/api/v2/package/
  Created https://www.nuget.org/api/v2/package/ 7672ms
Your package was pushed.
Pushing GosperCombination.1.0.0.symbols.nupkg to the symbol server (https://nuget.smbsrc.net/)...
  PUT https://nuget.smbsrc.net/api/v2/package/
  OK https://nuget.smbsrc.net/api/v2/package/ 3056ms
Your package was pushed.

그럼, 이렇게 등록된 페이지를 볼 수 있습니다. ^^

GosperCombination 1.0.0 
; https://www.nuget.org/packages/GosperCombination/

등록도 했으니, Visual Studio에서 사용도 해봐야겠지요. ^^ 간단하게 .NET Core Console 프로젝트를 만들고 NuGet Package Manager를 이용해 "Install-Package GosperCombination" 명령을 실행한 후 다음과 같이 코딩을 해주면 됩니다.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Gosper;

namespace ConsoleApp1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string[] items = new string[] { "ant", "bug", "cat", "dog", "elk" };

            int choose = 3;
            Combination c = new Combination(items, choose);

            foreach (var elems in c.Successor())
            {
                // ...[생략]...
            }
        }
    }
}

NuGet 패키지가 재미있는 것은, 위의 소스 코드를 Visual Studio에서 빌드해도 대상 폴더에는 "ConsoleApp1.dll" 파일만 생성된다는 점입니다. 즉, GosperCombination.dll 파일이 출력 폴더에는 없다는 건데요. 그래도 Visual Studio에서 실행해 보면 정상적으로 구동됩니다. 어떻게 이것이 가능할까요?

살펴보면, 추가한 NuGet 패키지의 바이너리는 다음의 경로에서 찾을 수 있습니다.

%USERPROFILE%\.nuget\packages\GosperCombination\1.0.0\lib\netstandard1.6

그리고, dotnet.exe는 설치된 NuGet 로컬 폴더를 마치 기존의 GAC(Global Assembly Cache)처럼 인식해서 전역 어셈블리 저장소로써 다루고 있는 것입니다.

문제는, 해당 NuGet 패키지는 개발자의 PC에만 있기 때문에 그것을 사용하는 프로그램의 DLL 파일을 다른 PC에 배포해서 실행하면 당연히 오류가 발생합니다. 실제로 ConsoleApp1.dll 파일만 복사해서 실행하면 이런 오류가 발생합니다. (이 오류는 ConsoleApp1.runtimeconfig.json 파일을 함께 배포하지 않은 경우에 발생합니다.)

C:\temp> dotnet ConsoleApp1.dll
A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\temp'.

즉, 다른 컴퓨터에 배포하려면 모든 의존성을 해결해주는 작업이 필요한데 이럴 때 사용할 명령어가 바로 "dotnet publish" 입니다. (Visual Studio의 경우 솔루션 탐색기에서 프로젝트를 마우스 우 클릭 후 "Publish..." 메뉴를 선택하면 됩니다.)

C:\ConsoleApp1\src\ConsoleApp1>dotnet publish
Publishing ConsoleApp1 for .NETCoreApp,Version=v1.0
Project ConsoleApp1 (.NETCoreApp,Version=v1.0) will be compiled because inputs were modified
Compiling ConsoleApp1 for .NETCoreApp,Version=v1.0

Compilation succeeded.
    0 Warning(s)
    0 Error(s)

Time elapsed 00:00:01.0110965


publish: Published to C:\ConsoleApp1\src\ConsoleApp1\bin\Debug\netcoreapp1.0\publish
Published 1/1 projects successfully

위와 같이, project.json 파일이 있는 곳에서 "dotnet publish"를 실행해 주면 의존성까지 고려한 바이너리를 "C:\ConsoleApp1\src\ConsoleApp1\bin\Debug\netcoreapp1.0\publish" 폴더에 출력해 줍니다. 그리고, 이것을 다른 PC에 복사하면 끝!




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 12/22/2023]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2016-08-29 06시57분
[강준] 좋은 자료 감사합니다. api 키를 매번 입력했는데 저런 방법도 있군요 ^^
[guest]
2022-04-02 10시35분
모은 dll 파일을 NuGet Package로 만들기
; https://docs.google.com/document/d/e/2PACX-1vRPFq05IONENb0S7_-TNoTNr3bG5p6aeK2xWrpv7dSpXOujTMhAODfWW-hieJo09wb2-cSZ-wBbNS3M/pub

위의 글은 Microsoft Store에서 배포하는 NuGet Package Explorer를 활용하는 방법을 설명하고 있습니다.
정성태

1  2  3  4  5  [6]  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13481정성태12/13/20232279개발 환경 구성: 693. msbuild - .NET Core/5+ 프로젝트에서 resgen을 이용한 리소스 파일 생성 방법파일 다운로드1
13480정성태12/12/20232647개발 환경 구성: 692. Windows WSL 2 + Chrome 웹 브라우저 설치
13479정성태12/11/20232332개발 환경 구성: 691. WSL 2 (Ubuntu) + nginx 환경 설정
13477정성태12/8/20232523닷넷: 2182. C# - .NET 7부터 추가된 Int128, UInt128 [1]파일 다운로드1
13476정성태12/8/20232257닷넷: 2181. C# - .NET 8 JsonStringEnumConverter의 AOT를 위한 개선파일 다운로드1
13475정성태12/7/20232320닷넷: 2180. .NET 8 - 함수 포인터에 대한 Reflection 정보 조회파일 다운로드1
13474정성태12/6/20232169개발 환경 구성: 690. 닷넷 코어/5+ 버전의 ilasm/ildasm 실행 파일 구하는 방법 - 두 번째 이야기
13473정성태12/5/20232374닷넷: 2179. C# - 값 형식(Blittable)을 메모리 복사를 이용해 바이트 배열로 직렬화/역직렬화파일 다운로드1
13472정성태12/4/20232189C/C++: 164. Visual C++ - InterlockedCompareExchange128 사용 방법
13471정성태12/4/20232266Copilot - To enable GitHub Copilot, authorize this extension using GitHub's device flow
13470정성태12/2/20232560닷넷: 2178. C# - .NET 8부터 COM Interop에 대한 자동 소스 코드 생성 도입파일 다운로드1
13469정성태12/1/20232279닷넷: 2177. C# - (Interop DLL 없이) CoClass를 이용한 COM 개체 생성 방법파일 다운로드1
13468정성태12/1/20232219닷넷: 2176. C# - .NET Core/5+부터 달라진 RCW(Runtime Callable Wrapper) 대응 방식파일 다운로드1
13467정성태11/30/20232304오류 유형: 882. C# - Unhandled exception. System.Runtime.InteropServices.COMException (0x800080A5)파일 다운로드1
13466정성태11/29/20232486닷넷: 2175. C# - DllImport 메서드의 AOT 지원을 위한 LibraryImport 옵션
13465정성태11/28/20232239개발 환경 구성: 689. MSBuild - CopyToOutputDirectory가 "dotnet publish" 시에는 적용되지 않는 문제파일 다운로드1
13464정성태11/28/20232371닷넷: 2174. C# - .NET 7부터 UnmanagedCallersOnly 함수 export 기능을 AOT 빌드에 통합파일 다운로드1
13463정성태11/27/20232301오류 유형: 881. Visual Studio - NU1605: Warning As Error: Detected package downgrade
13462정성태11/27/20232340오류 유형: 880. Visual Studio - error CS0246: The type or namespace name '...' could not be found
13461정성태11/26/20232373닷넷: 2173. .NET Core 3/5+ 기반의 COM Server를 registry 등록 없이 사용하는 방법파일 다운로드1
13460정성태11/26/20232324닷넷: 2172. .NET 6+ 기반의 COM Server 내에 Type Library를 내장하는 방법파일 다운로드1
13459정성태11/26/20232295닷넷: 2171. .NET Core 3/5+ 기반의 COM Server를 기존의 regasm처럼 등록하는 방법파일 다운로드1
13458정성태11/26/20232305닷넷: 2170. .NET Core/5+ 기반의 COM Server를 tlb 파일을 생성하는 방법(tlbexp)
13457정성태11/25/20232253VS.NET IDE: 187. Visual Studio - 16.9 버전부터 추가된 "Display inline type hints" 옵션
13456정성태11/25/20232550닷넷: 2169. C# - OpenAI를 사용해 PDF 데이터를 대상으로 OpenAI 챗봇 작성 [1]파일 다운로드1
13455정성태11/25/20232452닷넷: 2168. C# - Azure.AI.OpenAI 패키지로 OpenAI 사용파일 다운로드1
1  2  3  4  5  [6]  7  8  9  10  11  12  13  14  15  ...