# Leiningen Repositories

> Use a CloudRepo Maven repository with Leiningen: a project.clj entry, credentials from the environment, lein deploy, and what a 401 or 409 means.

Leiningen deploys to and resolves from a CloudRepo Maven repository. A CloudRepo Maven repository is an ordinary one, so Leiningen needs the repository’s URL in `project.clj` and a credential.

## Before you start

- A Maven repository to deploy to and resolve from. [Creating a Repository](/docs/manage/repositories.html#creating-a-repository) shows how to make one.
- A repository token with access to the repository, and **Download and publish packages** access if you will deploy. [Repository Tokens: Create One and Authenticate](/docs/authenticate/repository-tokens.html) shows how to create one. Leiningen sends the email address of the account that created the token as the username, and the token as the password.
- [Leiningen](https://leiningen.org/#install), installed from its own instructions.

> **Note:** Authenticate with a token.

## Connect Leiningen

Add CloudRepo to `:repositories` in `project.clj`. One entry handles both reading and deploying. The URL is the repository’s address without a trailing slash, and the credential comes from the environment:

`project.clj`

```clojure
(defproject com.example/my-library "1.0.0"
  :dependencies [[org.clojure/clojure "1.12.0"]
                 [com.example/internal-lib "1.2.3"]]
  :repositories [["cloudrepo-maven" {:url "https://your-org.mycloudrepo.io/repositories/your-repo"
                               :username :env/cloudrepo_username
                               :password :env/cloudrepo_token
                               :sign-releases false}]])
```

An `:env/name` value is read from the environment variable of that name in upper case, so this entry reads `CLOUDREPO_USERNAME` and `CLOUDREPO_TOKEN`. Set both from your secret store, the first to the email address of the account that minted the token and the second to the token.

## Deploy a library

Deploy by the repository’s id from `project.clj`:

**Terminal**

```bash
lein deploy cloudrepo-maven
```

Expected: Leiningen prints `Sending` and the path of the jar, the pom and the metadata, each followed by `to https://your-org.mycloudrepo.io/repositories/your-repo/`, and exits without an error. The deployed files show in the repository’s file list in the [admin portal](https://admin.cloudrepo.io).

Leiningen signs a release with GPG unless the repository entry says `:sign-releases false`. Without it, `lein deploy` of a release version stops before it sends anything, with `Could not sign` and the jar’s path if gpg has no key for you. A `-SNAPSHOT` version is not signed. CloudRepo does not ask for a signature, so if you do not sign your releases, keep `:sign-releases false` in the entry, as above.

If the version already exists in a repository with Overwrite Protection on, which is the default, CloudRepo refuses the deploy. See [Maven Overwrite Protection](/docs/formats/maven.html#maven-overwrite-protection).

> **Note:** Each file you deploy to a Maven repository can be up to 50 GB (50,000,000,000 bytes). A larger file is refused with `413`.

## Use a private dependency

Add the library to `:dependencies` and let Leiningen fetch it using the same `:repositories` entry:

**Terminal**

```bash
lein deps
```

Expected: Leiningen prints `Retrieving` and the path of the library’s pom and jar, `from cloudrepo-maven`.

## Keep the credential out of project.clj and the shell

An environment variable is the simplest place for the credential. Where one is awkward, Leiningen reads credentials from a file of your own, `~/.lein/profiles.clj`, in an `:auth` profile whose key is a regular expression of the repository URL. The file holds the token in plain text, so keep it readable by you alone (`chmod 600 ~/.lein/profiles.clj`):

`~/.lein/profiles.clj`

```clojure
{:auth {:repository-auth {#"https://your-org\.mycloudrepo\.io/.*"
                          {:username "you@example.com"
                           :password "YOUR_REPOSITORY_TOKEN"}}}}
```

Leave `:username` and `:password` out of the `:repositories` entry when you use this file.

## When Leiningen answers 401, 403, 404 or 409

- **`401 Unauthorized`.** The credential was refused or never sent. Leiningen must send the email address of the account that minted the token, and the token itself as the password. A token that has expired or been revoked fails the same way, and the **Repository Tokens** page in the admin portal shows each token’s status. Check that both environment variables are set in the shell that runs `lein`. If `CLOUDREPO_TOKEN` is unset, `lein deps` sends no credential and prints this same message, and `lein deploy` prints `No credentials found for` and the repository’s id, then asks for a username and a password at the terminal.
- **`403 Forbidden`.** The token does not have access to this repository, or it has **Download packages** access and you tried to deploy. Each token is limited to the repositories selected when it was created.
- **`Could not find artifact ...`.** The repository holds no such coordinate, or the URL names the wrong organization or repository. A proxy repository that the token cannot reach also answers `404`, never `403`.
- **`409 Conflict`, on deploy.** The repository has Overwrite Protection on, and the version already exists. Leiningen prints the status and discards the explanation CloudRepo sends with it. Deploy a new version, or use a `-SNAPSHOT` version while you iterate. See [Maven Overwrite Protection](/docs/formats/maven.html#maven-overwrite-protection).

See also [Repository tokens](/docs/authenticate/repository-tokens.html), which covers the credential each client uses, and [Maven Repositories](/docs/formats/maven.html), which covers Maven and the Maven repository settings.

---

The page: https://www.cloudrepo.io/docs/formats/leiningen.html
