Sunday, 6 October 2019

Nexus: Maven proxy repository configuration


Add settings.xml file to .m2 folder
$cd ~/.m2
$
$ls
repository  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>

<servers>
  <server>
    <id>nexus</id>
      <username>admin</username>
      <password>password123</password>
  </server>
</servers>
Above statement configure the admin credentials of nexus application. Id of <server> element must match with mirror id.

Create new maven project
$mkdir app1
$
$cd app1
$touch pom.xml

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>
</project>

Create new maven proxy repository
Step 1: Start nexus.
Open terminal and execute the command ‘nexus run’.

Step 2: Create new maven proxy repository named ‘maven-proxy-test’.

Open the url 'http://localhost:8081/' in browser and login with admin credentials.


Click Administration (Settings Icon) in the top navigation menu.


Click on ‘Repositories’.

Click on ‘Create repository’ button.


Click on maven2 (proxy) recipe.

Add the following text in the required fields:
Name: maven-proxy-test
Remote storage URL: https://repo1.maven.org/maven2

Click on ‘Create repository’ button to create a proxy repository.

Build maven project

Open terminal, navigate to the directory where pom.xml file is located. Execute the command ‘mvn package’.

Once the build is successful.

Login to Nexus Repository Manager.
 Click Browse icon in the main toolbar and then select Browse from the left-side menu.

Click on ‘maven-proxy-test’ repository.

You can see gson library is cached to maven-proxy-test repository.

Previous                                                    Next                                                    Home

1 comment: