Using a local repo manager and OSSRH

The following was submitted by Eric Kolotyluk, a Maven and maven-deploy-plugin user. Thanks Eric!

You may be in a situation that you have a local or corporate repository manager that you use as part of your daily software development process, and you only want to use Sonatype OSSRH on occasion. You can can configure this in your settings.xml, usually placed in ~/.m2, like this:

<profile>
  <id>local-repository</id>
  <properties>
    <altReleaseDeploymentRepository>local-nexus::default::http://localhost:8081/nexus/content/repositories/releases/</altReleaseDeploymentRepository>
    <altSnapshotDeploymentRepository>local-nexus::default::http://localhost:8081/nexus/content/repositories/snapshots/</altSnapshotDeploymentRepository>
  </properties>
</profile>

<activeProfiles>
  <activeProfile>local-repository</activeProfile>
</activeProfiles>

<servers>
  <server>
    <id>local-nexus</id>
    <username>deployment</username>
    <password>/secret/</password>
  </server>
  <server>
    <id>sonatype-nexus-snapshots</id>
    <username>/username/</username>
    <password>/secret/</password>
  </server>
  <server>
    <id>sonatype-nexus-staging</id>
    <username>/username/</username>
    <password>/secret/</password>
  </server>
</servers>

Normally when you run deploy, your artifacts will go to local-nexus (or whatever you configure), but when you want to use Sonatype OSS, you can just use

mvn deploy -P!local-repository

and Maven will deploy according to the <distributionManagement> defined in

<parent>
  <groupId>org.sonatype.oss</groupId>
  <artifactId>oss-parent</artifactId>
  <version>7</version>
</parent>

The benefit of this method is that you do not have to put any local or unnecessary corporate information in the pom.xml you are distributing with your project.

Warning: some older versions of the maven-deploy-plugin do not support this properly, so use the latest version, for example, in your pom.xml:

<build>
  <pluginManagement>
    <plugins>
      <plugin>
        <!--
          Bugs in older versions prevent altReleaseDeploymentRepository
          and altSnapshotDeploymentRepository from working correctly
          https://maven.apache.org/plugins/maven-deploy-plugin/deploy-mojo.html
         -->
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.8.1</version>
      </plugin>
  </pluginManagement>
  . . .
</build>