목차
개요
Flutter
로 앱을 개발하다보면, 공통 라이브러리 또는 오픈소스화를 위해 패키지(Package)를 만들어서 https://pub.dev/에 배포해야할 때가 있습니다.
이번 블로그 포스트에서는 Flutter
로 앱 개발을 할 때 필요한 패키지를 pub.dev
에 배포하는 방법에 대해서 알아보도록 하겠습니다.
pub.dev 배포 준비
pub.dev
에 패키지를 배포하기 위해서는 pub.dev
에 회원 가입 및 배포자(Publisher)
를 등록할 필요가 있습니다.
우선 https://pub.dev/ 사이트로 이동한 후, 회원 가입 및 로그인을 합니다.

그 다음, 오른쪽 상단의 My pub.dev
> Create publisher
메뉴를 선택합니다.

pub.dev
에 패키지를 배포하기 위해서는 Google Search Console
에 등록된 도메인을 가지고 있어야 합니다. 도메인을 가지고 있다면, Domain Name
에 해당 도메인을 입력한 후, START VERIFICATION
버튼을 선택합니다.

그럼 도메인을 확인하기 위한 절차가 진행됩니다. 모든 절차를 무사히 통과하면 다음과 같이 배포자(Publisher)
페이지로 이동하는 것을 확인할 수 있습니다.

패키지 프로젝트 생성
그럼 이제 pub.dev
에 배포할 패키지 프로젝트를 생성할 필요가 있습니다. 다음 명령어를 실행하여 패키지 프로젝트를 생성합니다.
# flutter create --template=package [package_name]
flutter create --template=package deku_publish_example
이제 lib/[package_name].dart
파일을 수정하여 패키지 내용을 작성합니다. 이번 블로그 포스트에서는 lib/round_button.dart
파일을 생성하고 다음과 같이 수정하였습니다.
import 'package:flutter/material.dart';
class RoundButton extends StatelessWidget {
final String label;
final Color? backgroundColor;
final VoidCallback? onPressed;
final bool? isLoading;
const RoundButton({
required this.label,
this.onPressed,
this.backgroundColor,
this.isLoading,
super.key,
});
Widget _renderLabel() {
if (isLoading == true) {
return const CircularProgressIndicator(
color: Colors.white,
strokeWidth: 3,
);
}
return Text(label);
}
@override
Widget build(BuildContext context) {
return SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: isLoading == true ? null : onPressed,
style: ElevatedButton.styleFrom(
backgroundColor: backgroundColor,
padding: const EdgeInsets.symmetric(vertical: 12.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(24.0),
),
),
child: _renderLabel(),
),
);
}
}
그런 다음, lib/deku_publish_example.dart
파일을 다음과 같이 수정하였습니다.
library deku_publish_example;
export 'round_button.dart';
물론, test/deku_publish_example_test.dart
파일을 수정하여 테스트가 잘 동작하도록 수정하였습니다.
Example 생성
그럼 이제 패키지를 사용하는 예제 프로젝트를 생성할 필요가 있습니다. 다음 명령어를 사용하여 Example
프로젝트를 생성하였습니다.
# cd deku_publish_example
flutter create example
그런 다음 example/pubspec.yaml
파일을 열고 다음과 같이 앞서 생성한 패키지를 추가합니다.
...
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
deku_publish_example:
path: ../
...
이렇게 추가한 패키지를 사용하기 위해, lib/main.dart
파일을 열고 다음과 같이 수정합니다.
import 'package:deku_publish_example/deku_publish_example.dart';
...
children: <Widget>[
...
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
RoundButton(
onPressed: _incrementCounter,
label: 'Increment',
),
],
...
패키지를 사용하도록 수정한 후, 시뮬레이터를 실행해 보면 다음과 같이 패키지에 내용이 잘 표시되는 것을 확인할 수 있습니다.

pubspec.yaml 파일 수정
패키지를 배포하기 위해서는 pubspec.yaml
에서 다음과 같은 내용을 수정, 추가할 필요가 있습니다.
name: [Package Name]
description: [Description]
version: 0.0.1
homepage: [Homepage URL]
repository: [Repository URL]
Changelog 파일
버전의 변경 사항을 기록하기 위해 Changelog
파일을 생성할 필요가 있습니다. CHANGELOG.md
파일을 생성하고 다음과 같이 수정합니다.
# CHANGELOG
## 0.0.1
License 파일
해당 패키지의 Lincese
를 작성할 필요가 있습니다. LICENSE
파일을 열고 다음과 같이 수정합니다.
MIT License
Copyright (c) [Project Owner]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
해당 내용은 GitHub
에서 기본적으로 제공하는 MIT
라이센스입니다.
Readme 파일
README.md
파일도 업데이트할 필요가 있습니다. 이번 블로그 포스트에서는 다음과 같은 내용을 작성하였습니다.
# Deku publish example
This is an example project for deployment.
배포 테스트
이제 패키지의 배포 준비가 끝났습니다. 다음 명령어를 실행하여 배포 테스트를 해 봅니다.
flutter packages pub publish --dry-run
특별한 문제가 없다면, 다음과 같은 결과를 확인할 수 있습니다.
...
Package has 0 warnings.
The server may enforce additional checks.
배포
이제 다음 명령어를 실행하여 실제로 패키지를 pub.dev
에 배포해 봅니다.
flutter packages pub publish
그럼 다음과 같이 배포를 할지 물어보는 화면을 확인할 수 있습니다.
...
Publishing is forever; packages cannot be unpublished.
Policy details are available at https://pub.dev/policy
Do you want to publish deku_publish_example 0.0.1 to https://pub.dev (y/N)?
y
키를 눌러 배포를 실행합니다. 그럼 다음과 같은 결과를 확인할 수 있습니다.
...
Uploading...
Successfully uploaded https://pub.dev/packages/deku_publish_example version 0.0.1.
결과에 표시된 https://pub.dev/packages/deku_publish_example로 이동하면 다음과 같이 잘 배포된 것을 확인할 수 있습니다.

문제없이 배포하기 위해서는 CHANGELOG.md
파일의 버전과 pubspec.yaml
의 버전이 서로 같아야 하며, pub.dev
에 배포된 적이 없는 버전이어야 한다.
배포 자동화
이렇게 패키지를 한번 배포하고 나면, GitHub Actions
를 사용하여 배포를 자동화할 수 있습니다. 배포를 자동화하는 방법에 대해서는 다음 링크를 참고하시기 바랍니다.
완료
이것으로 Flutter
에서 사용하기 위한 패키지를 만들고 pub.dev
에 배포하는 방법에 대해서 알아보았습니다. 여러분도 이 블로그 포스트를 참고하여 공통 위젯 라이브러리나 오픈소스를 만들어 배포해 보시길 바랍니다.
제 블로그가 도움이 되셨나요? 하단의 댓글을 달아주시면 저에게 큰 힘이 됩니다!
앱 홍보
Deku
가 개발한 앱을 한번 사용해보세요.Deku
가 개발한 앱은 Flutter로 개발되었습니다.관심있으신 분들은 앱을 다운로드하여 사용해 주시면 정말 감사하겠습니다.