33

We are using maven. I want to set up infrastructure, so that automatically built artifacts would go to Nexus repository. And then they could be used by developers.

I have already set up Jenkins with 1 job for our project. And I set up Nexus to on the same server.

On developers' PCs I copied default maven setting to C:\Users{user}.m2\settings.xml adding this section. References:

Configuring Maven to Use a Single Nexus

Maven Settings Reference

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

(I just follow Repository Management with Nexus book)

What are my next steps should be? Should Jenkins job have mvn install? How to create Nexus repository for company artifacts?

Paul Verest
  • 60,022
  • 51
  • 208
  • 332

5 Answers5

18

To deploy artifacts to Nexus, you'll need to include a distributionManagement section in your pom. Nexus ships with specific repositories already set up for both snapshots and releases. You should give the correct path to each of those so that maven will deploy snapshot and release artifacts to the correct repos. Then any time you deploy artifacts--typically with mvn deploy or using the maven release plugin, the artifacts will be deployed there. Nexus has write authentication on by default, so you'll need to make sure to add a server section with the correct credentials to the settings.xml of anyone who will be deploying artifacts. Jenkins can be treated pretty much like any other user. If you have it do a deploy as its build, then every build will deploy to Nexus. There's also a post-build action for deploying artifacts in case you want it to happen later in the Jenkins job.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • While this will work, it forces your project to know the location of your artifact repository. Take a look at [this answer](http://stackoverflow.com/a/11952832/235179) to have Jenkins handle artifact deployment exclusively. – Josh Johnson Jun 06 '13 at 15:26
17

I didn't need to make any changes to my projects pom.xml. Instead, in the jenkins "Post-build Actions" I selected "Deploy artifacts to Maven repository" then selected "Advanced" and set the Repository URL to http://nexusserver:8081/nexus/content/repositories/releases and the Repository ID to deploymentRepo.

In the ~/.m2/settings.xml on the jenkins machine I added

<settings>
  <servers>
    <server>
      <id>deploymentRepo</id>
      <username>deployment</username>
      <password>deployment123</password>
    </server>
  </servers>
   ...

</settings>
Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
Steve Brown
  • 195
  • 1
  • 8
  • Nexus default setting for release repositories is to allow 1 version to be deployed only once. Do you have second job only for release (i.e. deploying to Nexus)? – Paul Verest Jan 21 '13 at 01:32
  • 1
    @PaulVerest You could add a Pre Step to Execute Shell and run `mvn versions:set -DnewVersion=1.${BUILD_NUMBER} versions:commit`, naming the version after the build number. Then when this Post-build Action happens it will deploy it as that version. – Josh Johnson Jun 06 '13 at 15:17
  • @JoshJohnson This was answer by Steve Brown. Personally I would recommend use maven-release-plugin and not versions-maven-plugin (versions:set) – Paul Verest Jun 10 '13 at 02:48
  • @PaulVerest Sorry, I misunderstood your question in the comments. For the record, I wasn't able to use the release plugin since it introduced a manual step to my build pipeline. – Josh Johnson Jun 10 '13 at 17:23
  • yep, release-plugin is for manual automated release, and of course there must be continuous delivery pipeline. – Paul Verest Jun 10 '13 at 20:07
  • 2
    It has been a while and I am hazy on some of the details. I made each version a maven release with jenkins incrementing the release version number each time. I used ideas from [this article](http://devopsnet.com/2011/07/07/what-is-in-a-name-usually-a-version-number-actually/). As one of the commenters to that blog post mentions, there is a "hammering a square peg into a round hole" feeling about maven and continuous integration – Steve Brown Jul 24 '13 at 02:11
12

update pom.xml

 <distributionManagement>
    ...
    <repository>
      <id>deploymentRepo</id>
      <name>Internal Releases</name>
      <url>http://nexusserver:8081/nexus/content/repositories/releases</url>
    </repository>
    ...
  </distributionManagement>

then for maven ~/.m2/settings.xml add (this is default deployment user in Nexus)

<server>
  <id>deploymentRepo</id>
  <username>deployment</username>
  <password>deployment123</password>
 </server>

then mvn deploy

Then it is possible to use deployed artifact in any project, just as standard artifacts. In this case add to pom.xml

<!-- company repositories -->
    <repository>
        <id>deploymentRepoReleases</id>
        <name>Releases (Nexus)</name>
        <url>http://nexusserver:8081/nexus/content/repositories/releases/</url>
    </repository>
    <repository>
        <id>deploymentRepoSnapshots</id>
        <name>Snapshots (Nexus)</name>
        <url>http://nexusserver:8081/nexus/content/repositories/snapshots/</url>
    </repository>

UPDATE: Later we went away from Snapshot repositories and were using maven-release-plugin that only needs repositories of release type.

Paul Verest
  • 60,022
  • 51
  • 208
  • 332
1

If this is a Jenkins question rather than a Maven question, I'd recommend using the inbuilt Jenkins "Deploy Artifacts to Maven repository" Post build Action for most cases.

Advantages in my opinion:

  • More portable (we use different repositories for different stages of the life cycle), and each Jenkins instance knows it's own repository
  • More obvious to anyone reading the Jenkins job
  • More in line with the Jenkins way of doing things
ISparkes
  • 1,695
  • 15
  • 15
1

Following maven goal arguments for the maven-release-plugin can be used to pass the nexus repository authentication

-Dusername=<> -Dpassword=<>

use with jenkins project, maven command line, for nexus repo authentication

Amit Kaneria
  • 5,466
  • 2
  • 35
  • 38