Adding an optional internal artifactory repository to Gradle to speed up builds

For Asgard 2, we’re developing heavily with Spring Boot, Groovy and Gradle.

For our jenkins jobs, we found that our internal artifactory repository is much faster than Bintray’s jcenter. Due to the large number of dependencies in the project, a project that takes 30 minutes downloading and building with jcenter only takes 3 minutes using an internal artifactory.

Since our internal repository is only available to us within our network, we don’t really want to expose those credentials in the project we eventually want to open source.

In this post, I will show you how we add an optional internal artifactory repository to our open source builds.

To resolve this, we can simply add an optional internalArtifactory flag to our build.gradle file that will the internal repository when this flag is present. On developer machines, we just add this property to ~/.gradle/gradle.properties. On build boxes, we use the Jenkins Build Secrets plugin to get the same effect.

buildscript {
    repositories {
      if( project.hasProperty('internalArtifactory') ){
          maven { url project.internalArtifactory }
      } else {
          jcenter()
      }    
    }
    dependencies {
      classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.0.2.RELEASE'
    }
}

repositories {
    if( project.hasProperty('internalArtifactory') ){
        maven { url project.internalArtifactory }
    } else {
        jcenter()
    }
}

Notice here that we use two repositories blocks. The first block ensures that all your buildscripts resolve with your internal artifactory, the second block ensures all your local dependencies resolve.

If you have a multi-project build, the change is the same for the buildscript, but you want to use reference the parent property in your second repositories block and use it in within your subprojects block.

subprojects {  
  repositories {
    if( parent.hasProperty('internalArtifactory') ){
      maven { url parent.internalArtifactory }
    } else {
      jcenter()
    }
  }
  ...
}

2 thoughts on “Adding an optional internal artifactory repository to Gradle to speed up builds

  1. Pingback: Adding an optional internal artifactory repository to Gradle to speed up builds

Leave a comment