First ensure you have installed Docker & Maven on your computer.

  • docker pull sonatype/nexus3
  • Create a directory which you can use as volume for the docker container, for example mkdir /opt/nexus-data && chown -R 200 /opt/nexus-data
  • Run the container by docker run -d -p 8081:8081 --name nexus -v /opt/nexus-data:/nexus-data sonatype/nexus3
  • Go to http://localhost:8081 & login with admin admin123
  • Default repositories are yet created for maven; maven-central is the proxy where artifacts of maven central can be cached, maven-releases & maven-snapshots will contain deployed cq. releases & snapshots of your custom defined artifacts, while maven-public is the group that collects all repositories defined in it (here all).
  • Go to settings to configure other users, roles, etc…
  • Find the maven configuration directory on your computer & go to your settings.xml in your maven directory & open with editor.
  • First, we will need to add our authentication to communicate with the nexus server; add in <servers></servers>:
    <server>
      <id>name-to-use-for-server</id>
      <username>username-you-configured</username>
      <password>password-you-configured</password>
    </server>

    Repository to your local nexus

  • Put the following in your pom.xml:
     <repositories>
       <repository>
         <id>name-to-use-for-server</id>
         <url>http://localhost:8081/repository/maven-public/</url>
       </repository>
     </repositories>
  • Run maven just your usual way and see it will download dependencies from your local repo.

    Deploying with maven to your repository

  • To deploy to your repository, add this in your pom.xml:
     <distributionManagement>
      <snapshotRepository>
       <id>name-to-use-for-server</id>
       <url>http://localhost:8081/repository/maven-snapshots/</url>
      </snapshotRepository>
      <repository>
       <id>name-to-use-for-server</id>
       <url>http://localhost:8081/repository/maven-releases/</url>
      </repository>
     </distributionManagement>
  • And:
    <plugins>
     <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-deploy-plugin</artifactId>
     <version>2.8.2</version>
       <configuration>
      <skip>true</skip>
      </configuration>
     </plugin>
     <plugin>
      <groupId>org.sonatype.plugins</groupId>
      <artifactId>nexus-staging-maven-plugin</artifactId>
       <version>1.6.8</version>
      <executions>
      <execution>
      <id>default-deploy</id>
      <phase>deploy</phase>
      <goals>
      <goal>deploy</goal>
      </goals>
      </execution>
      </executions>
      <configuration>
      <serverId>nexus</serverId>
      <nexusUrl>http://localhost:8081/nexus/</nexusUrl>
      <skipStaging>true</skipStaging>
      </configuration>
     </plugin>
    </plugins>
  • Run mvn clean deploy -Dmaven.test.skip=true in your Maven project and see it will be uploaded to your local nexus repo.

    Nexus repository as default mirror

  • To use your nexus repo as a mirror, insert in <mirrors></mirrors>:
    <mirror>
     <id>nexus</id>
     <name>nexus</name>
     <url>http://localhost:8081/repository/maven-public/</url>
     <mirrorOf>*</mirrorOf><!--mirrorOf * means it will always look in the mirror-->
    </mirror>
  • Furthermore, add a profile & make it active (ensure that profile id is the same as mirror id!):
    <profiles>
     <profile>
     <id>nexus</id>
     <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>
  • All maven projects will now proxy through your local nexus repo.

Now you are all set!

References:

  • https://dzone.com/refcardz/getting-started-repository
  • http://codeheaven.io/using-nexus-3-as-your-repository-part-1-maven-artifacts/

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *