# Pub CLI Usage

This page describes the CLI commands to retrieve, publish, and upload packages after creating and configuring a Pub repository on Nexus.

## Retrieve Pub Content

After configuring Pub to use the Nexus repository, use Dart or Flutter commands from your workspace.

```
dart pub get
flutter pub get
dart pub upgrade
flutter pub upgrade
dart pub downgrade
```

Where,

- `dart pub get` \- downloads required packages through the Nexus Pub repository
- `flutter pub get` \- fetches packages referenced in the Flutter project's `pubspec.yaml`
- `dart pub upgrade` \- refreshes package versions when updates are available
- `flutter pub upgrade` \- updates packages to the latest compatible versions
- `dart pub downgrade` \- resolves packages to the oldest compatible versions

Example:

```
dart pub get
flutter pub get
```

## Publish Pub Packages

You can publish your packages by using the `dart pub publish` command, the Components REST API, or the Nexus Repository UI. Sonatype recommends using the following `dart pub publish` command.

```
dart pub publish --server https://[NEXUS_URL]/repository/<REPO_NAME>
```

Where,

- `[NEXUS_URL]` \- The URL of your Nexus instance, for example `example.sonatype.com`
- `<REPO_NAME>` \- The name of the target hosted repository in Nexus, for example `pub-hosted`

Example:

```
dart pub publish --server https://example.nexus.com/repository/pub-hosted
```

You can also publish using the Components REST API:

```
curl \
  -X POST \
  "https://[NEXUS_URL]/service/rest/v1/components?repository=<REPO_NAME>" \
  -u "USERNAME:PASSWORD" \
  -F "pub.asset=@{package-name}-{version}.tar.gz" \
  -F "pub.name={package-name}" \
  -F "pub.version={version}"
```

Where,

- `[NEXUS_URL]` \- The URL of your Nexus instance
- `<REPO_NAME>` \- The name of the target hosted repository in Nexus
- `{package-name}` \- Package name as defined in `pubspec.yaml`
- `{version}` \- Semantic version, for example `1.0.0`

Example:

```
curl \
  -X POST \
  "https://example.nexus.com/service/rest/v1/components?repository=pub-hosted" \
  -u "publisher:[PASSWORD]" \
  -F "pub.asset=@example_package-1.0.0.tar.gz" \
  -F "pub.name=example_package" \
  -F "pub.version=1.0.0"
```

You can also upload Pub packages from the Nexus Repository UI.
