Sunday, 6 October 2019

Maven: Hosted Repositories


What is hosted repository?
In my previous post, I explained about Proxy repository. Proxy repository is the one which acts as a proxy to remote repository. Nexus also support hosted repositories to store organizational internal snapshots and releases.

Default installation of Nexus repository manager comes with two hosted repositories.
a.   maven-releases repository
b.   maven-snapshots repository


All the internal releases of your organization will keep in releases repository. The artefacts that are published to release repository are considered as stable artefacts. You can also use this repository to store the third-party artefacts that are not available in external repositories and can’t be retrieved using a configured proxy repository.

SNAPSHOTS repository is used for staging environment, where the development is still going on. Snapshot versions end with -SNAPSHOT in the POM file.

Example
<groupId>com.sample.app</groupId>
<artifactId>chatbot</artifactId>
<version>1.0.3-SNAPSHOT</version>
<packaging>jar</packaging>

Add server settings to maven config file (~/.m2/settings.xml)
settings.xml
<settings>
  <mirrors>
    <mirror>
      <!--This sends everything else to /public -->
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <url>http://localhost:8081/repository/maven-proxy-test/</url>
    </mirror>
  </mirrors>

  <profiles>
    <profile>
      <id>nexus</id>
      <!--Enable snapshots for the built in central repo to direct -->
      <!--all requests to nexus via the mirror -->
      <repositories>
        <repository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
     <pluginRepositories>
        <pluginRepository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  </profiles>

  <activeProfiles>
    <!--make the profile active all the time -->
    <activeProfile>nexus</activeProfile>
  </activeProfiles>

 <servers>
    <server>
      <id>nexus</id>
      <username>admin</username>
      <password>password123</password>
    </server>
  </servers>
  
</settings>

As you see above snippet, I added nexus admin credentials in <server> element and given the id as ‘nexus’.

Update pom.xml to link with snapshot and release repository

pom.xml
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sample.app</groupId>
    <artifactId>nexus-proxy-test</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <dependencies>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>
    </dependencies>
    
    <distributionManagement>
        <repository>
            <id>nexus</id>
            <name>maven-releases</name>
            <url>http://localhost:8081/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
            <id>nexus</id>
            <name>maven-snapshots</name>
            <url>http://localhost:8081/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
</project>

Open terminal and navigate to the directory where pom.xml is located. Execute the command ‘mvn clean deploy’.

Once deployment is successful, the artefact is deployed to ‘maven-snapshots’ repository (Since version number ‘1.0-SNAPSHOT’ contains SNAPSHOT, it deploys to snapshot repository).

Login to Nexus Repository Manager.

 Click Browse icon in the main toolbar and then select Browse from the left-side menu.


Click on ‘maven-snapshots’ repository.


You can see that generated artefact is deployed to Nexus.

How to publish to maven-releases repository?
Remove SNAPSHOT tag from <version> element of pom.xml.

<version>1.0</version>

Save pom.xml with updated version. Execute the command ‘mvn clean deploy’.

Once the mvn command executed successfully, you can see that artefact is deployed to maven-releases repository.
Previous                                                    Next                                                    Home

No comments:

Post a Comment