Using Jenkins Local Maven Repository as An Internal Private Maven Repository

DZone 's Guide to

Using Jenkins Local Maven Repository as An Internal Private Maven Repository

An alternative option to using commercial repository management software is to use a Jenkins instance of Maven instead.

· DevOps Zone ·
Free Resource

Organizational software projects use their own private (organizational-wide) Maven repositories. This approach allows them to store their own or other non-publically available artifacts in these repositories. Additionally, it has the advantage of using this repository as a proxy for the Maven central repository.

It is a recommended practice to use repository management software for this purpose. More details on this topic can be found here.

As an alternative to those repository management products, using an AWS S3 bucket as a Maven repository has become a common practice, at least with organizations that use AWS cloud. This article provides details of setting up such a repository.

There are occasions where developers simply need to store and share their JAR files among the projects of their organization. Developers use local private Maven repositories on their development environments in order to achieve this purpose. But the issue is, how do the projects get access to those artifacts in the project’s test or production build environments?

The good news is, if you are using Jenkins, we can use Jenkin’s Maven repository as local internal Maven repository in our Jenkins build environment.

Jenkins is a very popular and widely use continuous integration tool. Jenkins uses Maven internally as a build tool. You can see the Maven repository location inside the Jenkins administrator interface.

Got to the Jenkins homepage and click on “Manage Jenkins” on the left-hand side menu. Then click “Configure System.” Under the “Maven Project Configuration,” you can see the Maven repository directory location as shown in this image:

Image title

The purpose of this article is to show the necessary steps involved in using an internal Maven repository with requirements as explained above. Steps shown here will use both popular built streams Gradle and Maven.

Suppose your development team needed to develop and maintain a utility software program and use it as a library in other software projects.

Once you are happy with the implementation and testing of this program, then you may need to make it available to your other projects. So you will need to upload it to your local Maven repository.

Here is a Gradle build script which has necessary steps to package artifacts of the program together as a jar file and to upload it to local Maven repository.

Note: This script will bundle all dependencies as well, since the intention is to use this as a software library in other projects.

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'maven-publish'

sourceCompatibility = 1.5
version = '1.0'
def title = 'test-publish'

jar {
    manifest {
        attributes 'Implementation-Title': 'Gradle Quickstart',
                   'Implementation-Version': version

repositories {
dependencies {
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
    compile group: 'org.slf4j', name:'slf4j-api', version: '1.7.12'
    testCompile group: 'junit', name: 'junit', version: '4.+'

//create a single Jar with all dependencies
task createJar(type: Jar) {

println 'Going to create the jar..'
manifest {
        attributes 'Implementation-Title': 'Test Package',  
        'Implementation-Version': version,
    baseName = title
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
     with jar

publishing {
publications {
            maven(MavenPublication) {
                groupId 'com.myorg.testpublish'
                artifactId 'test-publish'
                version version
                artifact "$buildDir"+'/libs/'+"${title}"+'-'+"$version"+'.jar'

//publich to local maven repo
publishToMavenLocal.dependsOn clean, test, createJar

Now, on the command prmompt, go to the folder where your “build.gradle” file is available and execute the following command:

 gradle publishToMavenLocal

Once the above build is successful, you can see “test-publish-1.0.0.jar” is available in your local Maven repository.

If your favourite build tool is Maven, then here is the Maven build script to package and upload the project’s artifacts to local Maven repository.

<project xmlns="http://maven.apache.org/POM/4.0.0" 

<!--Following is needed to bundle dependency jars. -->
            <id>make-assembly</id> <!-- this is used for inheritance merges -->
            <phase>package</phase> <!-- bind to the packaging phase -->


Now, on the command prompt, go to the folder where your pom.xml is available and execute the following command:

 mvn clean install 

Note: When the install  command is executed, the package  command will also be executed, so there is no need to give package  command here. More information about the Maven life cycle is availble here.

As you can see, using one of above scripts (depending on our build stream), we can upload our library jar file to our local Maven repository.

We can now create a Jenkins build job and use any of the above scripts, in order to upload our jar file to Jenkins local Maven repository.

Following are the steps to create a Jenkins build job.

Creating a Jenkins Build Job

Go to the Jenkins dashboard and click on "Manage Jenkins" and then select "Manage Plugins." Select the plugins your project may need on Available tab. If you grabbing your source code from Git, you may need to install “Git plugin," “GitHub plugin” and “GitHub API Plugin." Click on Download now and install after restart button. Restart Jenkins.

Go to the Jenkins dashboard again and click on New Item. Select Freestyle project check box and provide a suitable name for the Item name. Then click OK.

Then go to the configuration page and configure Source Code Management and provide a path to the source code of the library projects created.

Source Code Management sections of Jenkins configuration

Then go to the Build section and select Gradle as the build tool. Provide the task name as in our Gradle built script as shown at the previous steps.

Note: If you are going to use Maven as your build tool, you need to select Maven here.

Build sections of Jenkins configuration

Now save the configuration and build the projects. With that step, your jar file is available in Jenkins Local Maven repository.

Now other projects (in the same Jenkins build environment) which need to use this jar file as a dependency, can access it from their corresponding build files.

This  is a section of a Gradle build script which shows how to use our new library as a dependency:

dependencies {
compile (
'com.myorg.testpublish:test-publish:1.0.0 '

This is a section of a Maven pom.xml which shows how to use our new library as a dependency:

      <groupId>com.myorg.testpublish </groupId>
      <artifactId>test-publish </artifactId>
      <version>1.0.0 </version>


As explained in the above steps, we can use the Jenkins local Maven repository as an internal private Maven repository in our build environments. This becomes handy when you want to share some project artifacts among other projects within your organization but still do not want to use repository management software like Nexus or Apache Archiva.

gradle, jenkins, maven, repository management

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}