How to configure pom.xml file to use Nexus Repository manager

前提是你 提交于 2020-01-03 04:52:11

问题


I am using Redhat, java 1.7, maven 3.2.5, jenkins 1.6,git version 2.0.5 and nexus-2.12.0-01

I have created a Local Nexus Repository for my internal development. Now What I am trying to do is, to build maven project using jenkins with Nexus Repository Manger Oss. I am able to build my project without Nexus Repository Manger Oss.

Note : I am using parent pom since I have to sub projects.

Below are the steps that I followed.

  • Installed Nexus Repository Manger Oss. It is up and running
    here is screen-shot.

  • Installed ojdbc5 jar in nexus repository

  • Made necessary changes in setting.xml file of .m2 folder

here the content of setting.xml

<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">

    <mirrors>
        <mirror>
        <!--This sends everything else to /public -->
            <id>nexus</id>
            <mirrorOf>*</mirrorOf>
            <url>http://localhost:8081/nexus/content/groups/public</url>
        </mirror>
    </mirrors>

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

    <profiles>
        <profile>
            <id>nexus</id>
          <repositories>
          <repository>
          <id>central</id>
          <url>http://maven.apache.org</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
          </repository>
            </repositories>

            <pluginRepositories>
                <pluginRepository>
                    <id>central</id>
                    <url>http://maven.apache.org</url>
                    <releases><enabled>true</enabled></releases>
                    <snapshots><enabled>true</enabled></snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>

    <servers>
      <server>
       <id>releases</id>
       <username>deployment</username>
       <password>deployment123</password>
      </server>
      <server>
        <id>snapshots</id>
        <username>deployment</username>
        <password>deployment123</password>
      </server>
    </servers>

</settings> 
  • Added the details of nexus repository in my pom.xml

parent pom.xml content

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>parent</groupId>
  <artifactId>A</artifactId>
  <packaging>pom</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>Maven Webapp</name>
  <!--url>http://maven.apache.org</url-->

  <distributionManagement>
    <repository>
      <id>releases</id>
      <url>http://localhost:8081/nexus/content/repositories/thirdparty/</url>
    </repository>
    <snapshotRepository>
      <id>snapshots</id>
      <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
    </snapshotRepository>
  </distributionManagement>



  <dependencies>
    <dependency>
      <groupId>com.oracle</groupId>
      <artifactId>ojdbc5</artifactId>
      <version>11.2.0.1</version>
    </dependency>
  </dependencies>

  <modules>
    <module>subModule1</module>
    <module>subModule2</module>
  </modules>
</project>

subModule1's pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.a</groupId>
  <artifactId>a</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Maven Webapp</name>

  <distributionManagement>
    <repository>
      <id>releases</id>
      <!-- CHANGE HERE by your team nexus server -->
      <url>http://localhost:8081/nexus/content/repositories/thirdparty/</url>
    </repository>
    <snapshotRepository>
      <id>snapshots</id>
      <!-- CHANGE HERE by your team nexus server -->
      <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
    </snapshotRepository>
  </distributionManagement>


  <dependencies>

    // dependecies are defined here
  <dependencies>

In subModule1 and subModule2 I have added the distributionManagement section.

But when I build my project I get the below error.

[ERROR] Failed to execute goal on project submodule1: Could not resolve dependencies for project submodule1:war:1.0-SNAPSHOT: Failure to find com.oracle:ojdbc5:jar:11.2.0.1 in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced -> [Help 1]

I understand the error is caused by the com.oracle:ojdbc5:jar:11.2.0.1jar dependency not being resolvable.

But I am surprised why it is not picking it up from my local nexus repository.

Have I missed any configuration, or am I doing it in a wrong way?

How do I use local nexus repository for my projects?


回答1:


In your settings.xml, usually in your ~/.m2/ directory you need to specify your local Nexus. You only get maven central by default.

 <profile>
        <id>local</id>
        <repositories>
            <repository>
                <id>nexus</id>
                <name>libs-release</name>
                <url>http://url to your local Nexus/artifactory/libs-release</url>
            </repository>
        </repositories>
 </profile>

You can add as many repository elements as you like.

Make sure the correct profile is active, at the bottom of your settings.xml

  <activeProfiles>
    <activeProfile>local</activeProfile>
  </activeProfiles>

Also you will be left with a stub in your ~/.m2/repository/com/oracle ... folder. I always do a rm -r ~/.m2/repository/com/oracle to force it to download from the remote repo.




回答2:


Your settings.xml does not correctly override the central repository configuration. The correct setup can be found in the documentation.

There are also example projects available that have everything ready to go and the eval guide includes a step by step guide.




回答3:


This is my settings.xml file-

<localRepository>C:/Users/shuarya/.m2/repository</localRepository>
<interactiveMode>true</interactiveMode>
<offline>false</offline>
<pluginGroups>
</pluginGroups>
<proxies>
</proxies> 
<servers>  
</servers>
<mirrors>
    <mirror>
  <!--This sends everything else to /public -->
  <id>nexus</id>
  <mirrorOf>*</mirrorOf>
  <url>http://localhost:8081/nexus/content/repositories/com.shubham.TestNexus2</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>
</settings>

So, in my settings.xml file, I have specified the path of my local repository and also, at the bottom I have chosen my active profile as nexus. Inside the nexus node I have specified my locally hosted nexus repository's path.

And, I have not specified any URL in my pom.xml

    <modelVersion>4.0.0</modelVersion>
<groupId>com.shubham.example</groupId>
<artifactId>TestingNexusrepo</artifactId>
<version>0.0.1-SNAPSHOT</version>

<dependencies>

    <dependency>
        <groupId>com.shubham.TestNexus</groupId>
        <artifactId>TestNexus</artifactId>
        <version>1</version>
    </dependency>

    <dependency>
        <groupId>com.shubham.TestNexus2</groupId>
        <artifactId>TestNexus2</artifactId>
        <version>1</version>
    </dependency>

</dependencies>
</project>

When the project builds, it downloads the jar from the repository and keeps it in the local repository path. This way, in future when you specify this(same) dependency, it fetches the dependency from the local repository rather than downloading it again from the Nexus repository (caching).

This is working for me, please let me know if my understanding is wrong.

Also, Is there any way in which I could specify more than one URL(s) for the nexus repository.

Please help.



来源:https://stackoverflow.com/questions/38898703/how-to-configure-pom-xml-file-to-use-nexus-repository-manager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!