> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/artifact-registry/use-artifact-registry/overview/maven-plugin-overview.md).

# Maven Plugin

{% @harness-package-selector/package-selector platforms="%5B%7B%22label%22%3A%22Gradle%20Plugin%22%2C%22slug%22%3A%22gradle-plugin%22%2C%22path%22%3A%22artifact-registry%2Fuse-artifact-registry%2Foverview%2Fgradle-plugin-overview%22%7D%2C%7B%22label%22%3A%22Maven%20Plugin%22%2C%22slug%22%3A%22maven-plugin%22%2C%22path%22%3A%22artifact-registry%2Fuse-artifact-registry%2Foverview%2Fmaven-plugin-overview%22%7D%5D" selectedPlatform="maven-plugin" %}

The Harness Maven Plugin simplifies Maven project integration with Harness Artifact Registry by automating artifact deployment and dependency management directly from your Maven build lifecycle.

The plugin provides two main capabilities:

1. **Deploy Artifacts**: Automatically upload your Maven artifacts (JARs, WARs, POMs, etc.) to Harness Artifact Registry during the Maven build process
2. **Enforce Dependency Resolution**: Ensure all Maven dependencies are fetched through your Harness upstream proxy registries, providing centralized control over your project's dependencies

### Installation <a href="#installation" id="installation"></a>

Add the plugin to your project's `pom.xml`:

```xml
<build>
    <plugins>
        <plugin>
            <groupId>io.harness.maven</groupId>
            <artifactId>harness-maven-plugin</artifactId>
            <version>1.0.0</version>
            <executions>
                <execution>
                    <id>deploy-artifacts</id>
                    <goals>
                        <goal>deploy</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
```

#### Using SNAPSHOT Versions <a href="#using-snapshot-versions" id="using-snapshot-versions"></a>

If you are using a SNAPSHOT version of the plugin (e.g., `1.0.0-SNAPSHOT`), add the snapshot repository to resolve it:

```xml
<pluginRepositories>
    <pluginRepository>
        <id>sonatype-snapshots</id>
        <url>https://central.sonatype.com/repository/maven-snapshots/</url>
    </pluginRepository>
</pluginRepositories>
```

### Configuration <a href="#configuration" id="configuration"></a>

The plugin uses environment variables for configuration. Set these variables before running Maven commands.

#### Deployment Configuration <a href="#deployment-configuration" id="deployment-configuration"></a>

Configure deployment to Harness Artifact Registry:

```bash
export DEPLOY_REPO_URL="https://pkg.harness.io/pkg/<ACCOUNT_ID>/<REGISTRY_NAME>/maven"
export DEPLOY_USERNAME="your-username"
export DEPLOY_TOKEN="your-token"
```

#### Resolution Configuration (Optional) <a href="#resolution-configuration-optional" id="resolution-configuration-optional"></a>

To enforce dependency resolution through Harness upstream proxy, configure these variables:

{% hint style="info" %}
**UPSTREAM PROXY REQUIRED**

The enforce resolution feature requires an upstream proxy registry configured in Harness. Learn how to [create an upstream proxy registry](/artifact-registry/use-artifact-registry/manage-registries/upstream-proxy.md).
{% endhint %}

```bash
export RESOLVER_REPO_URL="https://pkg.harness.io/pkg/<ACCOUNT_ID>/<REGISTRY_NAME>/maven"
export RESOLVER_USERNAME="your-username"
export RESOLVER_TOKEN="your-token"
```

#### Configuration Reference <a href="#configuration-reference" id="configuration-reference"></a>

| Environment Variable  | Description                                                    | Required                     |
| --------------------- | -------------------------------------------------------------- | ---------------------------- |
| `DEPLOY_REPO_URL`     | Repository URL for deployment                                  | Yes (for deploy)             |
| `DEPLOY_USERNAME`     | Username for deployment                                        | Yes (for deploy)             |
| `DEPLOY_TOKEN`        | Token/password for deployment                                  | Yes (for deploy)             |
| `DEPLOY_THREAD_COUNT` | Number of threads for parallel deployment (default: CPU cores) | No                           |
| `RESOLVER_REPO_URL`   | Repository URL for dependency resolution                       | Yes (for enforce-resolution) |
| `RESOLVER_USERNAME`   | Username for resolution                                        | Yes (for enforce-resolution) |
| `RESOLVER_TOKEN`      | Token/password for resolution                                  | Yes (for enforce-resolution) |

### Usage <a href="#usage" id="usage"></a>

#### Deploy Artifacts <a href="#deploy-artifacts" id="deploy-artifacts"></a>

Deploy your Maven project artifacts to Harness Artifact Registry:

```bash
mvn deploy
```

The plugin will:

1. Build your project artifacts
2. Upload artifacts to the configured Harness registry
3. Use parallel threads for faster deployment (configurable via `DEPLOY_THREAD_COUNT`)

**Configure Deployment Thread Count (Optional)**

By default, the plugin uses the number of CPU cores for parallel deployment. You can override this:

```bash
export DEPLOY_THREAD_COUNT=4
mvn deploy
```

#### Enforce Dependency Resolution <a href="#enforce-dependency-resolution" id="enforce-dependency-resolution"></a>

Force all Maven dependencies to resolve from Harness upstream proxy:

```bash
mvn harness:enforce-resolution
```

This ensures all dependencies are fetched through your Harness upstream proxy registry, providing centralized control over your project's dependencies.

### Skip Deployment for Specific Modules <a href="#skip-deployment-for-specific-modules" id="skip-deployment-for-specific-modules"></a>

If you have modules that should NOT be deployed (e.g., documentation modules, test modules, parent POMs), configure them individually:

```xml

<build>
    <plugins>
        <plugin>
            <artifactId>maven-deploy-plugin</artifactId>
            <configuration>
                <skip>true</skip>
            </configuration>
        </plugin>
    </plugins>
</build>
```

The Harness Maven Plugin automatically respects this configuration and skips those modules during deployment.

### Example Workflow <a href="#example-workflow" id="example-workflow"></a>

Here's a complete example of using the Harness Maven Plugin in a CI/CD pipeline:

```bash
# Set deployment credentials <a href="#set-deployment-credentials" id="set-deployment-credentials"></a>
export DEPLOY_REPO_URL="https://pkg.harness.io/pkg/abc123/my-maven-registry/maven"
export DEPLOY_USERNAME="harness-user"
export DEPLOY_TOKEN="${HARNESS_TOKEN}"

# Set resolution credentials (optional) <a href="#set-resolution-credentials-optional" id="set-resolution-credentials-optional"></a>
export RESOLVER_REPO_URL="https://pkg.harness.io/pkg/abc123/maven-proxy/maven"
export RESOLVER_USERNAME="harness-user"
export RESOLVER_TOKEN="${HARNESS_TOKEN}"

# Configure parallel deployment <a href="#configure-parallel-deployment" id="configure-parallel-deployment"></a>
export DEPLOY_THREAD_COUNT=8

# Enforce dependency resolution through Harness <a href="#enforce-dependency-resolution-through-harness" id="enforce-dependency-resolution-through-harness"></a>
mvn harness:enforce-resolution

# Build and deploy artifacts <a href="#build-and-deploy-artifacts" id="build-and-deploy-artifacts"></a>
mvn clean deploy
```
