My favorite build tool for personal projects is Gradle, so last year I wrote this article “How to Publish Open Source Projects to Maven’s central repository” about how Gradle projects configure open source projects to publish to Maven’s central repository. Corporate projects use Maven and are pushed not to maven’s central repository, but to nexus, a private repository. This article will show you how to package and deploy SDK projects to a private repository.

Deploying to Maven’s central repository is the same, just configured differently.

We will look at two configurations, one where a project has only one module, and the other where a project has multiple modules and we only need to push one module to a private repository.

A project has only one module

Usually common components.

Add the warehouse address configuration in pom.xml of the project (parent project) as follows

<distributionManagement>
    <repository>
        <! Maven setting.xml -->
        <id>nexus</id>
        <name>maven-releases</name>
        <! -- Address of private warehouse you set up -->
        <url>http://[your private nexus domain]/repository/maven-releases/</url>
    </repository>
</distributionManagement>
Copy the code

Add the server configuration in Maven settings.xml as follows


      
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <servers>
        <server>
            <id>nexus</id>
            <username>username</username>
            <password>password</password>
        </server>
    </servers>
</settings>
Copy the code

The account and password are the password used to log in to the Nexus and must have the upload permission.

Tip: The SERVER (settings.xml) ID must be the same as the Repository (pom.xml) ID.

Package the project and push it to Nexus:

Deploy only one of multiple modules in a project

These are usually business items, and modules that need to be deployed to a private repository are usually SDK modules that are packaged to facilitate other microservice calls.

Also add the warehouse address configuration to the project’s (parent project) pom.xml as follows

<distributionManagement>
    <repository>
        <! Maven setting.xml -->
        <id>nexus</id>
        <name>maven-releases</name>
        <! -- Address of private warehouse you set up -->
        <url>http://[your private nexus domain]/repository/maven-releases/</url>
    </repository>
</distributionManagement>
Copy the code

You also need to add the server configuration in Maven settings.xml, as follows


      
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <servers>
        <server>
            <id>nexus</id>
            <username>username</username>
            <password>password</password>
        </server>
    </servers>
</settings>
Copy the code

Push submodules to nexus by command as follows

MVN clean deploy -pl -dskiptestsCopy the code
  • -pl: specifies the submodule to be pushed. Multiple submodules are separated by ‘,’. If all submodules are pushed, -pl is not specified.
  • -dskiptests: skips tests.

For submodules that do not need to be pushed to a private server repository, it is best to add the following configuration under the submodule

<properties>
    <maven.deploy.skip>true</maven.deploy.skip>
</properties>
Copy the code

The maven configuration file settings. XML is not configured with the server or the account does not have permissions.

If using the Maven plugin for IDEA ->Lifecycle->deploy push

  • Maven override the default settings. XML configuration file by configuring the Preferences -> Build -> Maven.
  • 2. Add the server configuration to the new settings. XML configuration file.
  • 3. If only a submodule will be pushed Lifecycle->deploy for the submodule.

Tip: If the child module’s pom.xml depends on the parent module’s pom.xml, this method is not recommended. It does not push the parent module’s pom.xml to Maven.

Special note

If you use the deploy command, make sure that the settings. XML used by Maven specified in the system environment variable is the same as the settings. XML used by Maven configured with IDEA.

If you run into a situation where you have configured the server in settings.xml and the configuration doesn’t seem to work, you may be running into an inconsistent configuration file.

  • In MAC, the maven environment variables are configured in ~/. Base_profile as follows
#Maven environment variablesMAVEN_HOME = / Users/wjy/JavaApp/apache maven - 3.6.1 export PATH = $PATH: $MAVEN_HOME/binCopy the code

MAVEN_HOME/conf/settings.xml is used by default.

  • IDEA can be configured to use Maven and settings.xml.