Artifactory with Multi-Module Gradle Project

Prerequisites

1. Oracle JDK8

Environment Details

1. OS – Ubuntu 16.04 x64 Server
2. Artifactory – oss-5.3.0

Problem

Publish your jar from Multi-module gradle project and use in other projects.

Steps

Install Artifactory

Start Artifactory

  • Goto artifactory home. (to extracted location)
  • run – bin/artifactory.sh

Setup repository

Your framework project parent/root build.gradle


buildscript {
  repositories {
    jcenter {
      url 'http://localhost:8081/artifactory/jcenter/'
    }
  }

  dependencies {
    //Check for the latest version here: http://plugins.gradle.org/plugin/com.jfrog.artifactory
    classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4+"
  }
}

allprojects {
  apply plugin: 'maven'

  group = 'com.example'
  version = '1.0.0-SNAPSHOT'

  ext {
    springBootVersion = '1.5.2.RELEASE'
  }
}

subprojects {
  apply plugin: 'java'
  apply plugin: 'maven-publish'
  apply plugin: "com.jfrog.artifactory"

  sourceCompatibility = 1.8
  targetCompatibility = 1.8
  tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
  }

  repositories {
    jcenter {
      url 'http://localhost:8081/artifactory/jcenter/'
    }
  }

  dependencies {
    testCompile group: 'junit', name: 'junit', version:'4.12'
  }

  artifactory {
    contextUrl = "${artifactory_contextUrl}" //The base Artifactory URL if not overridden by the publisher/resolver
    publish {
      repository {
        repoKey = 'example-framework-snapshot-local'
        username = "${artifactory_user}"
        password = "${artifactory_password}"
        maven = true
      }
      defaults {
        publications ('mavenJava')
      }
    }

    resolve {
      repository {
        repoKey = 'example-framework-snapshot'
        username = "${artifactory_user}"
        password = "${artifactory_password}"
        maven = true
      }
    }
  }

  publishing {
    publications {
      mavenJava(MavenPublication) {
        from components.java
      }
    }
  }
}

build.gradle for user of your Framework Code

Assuming your virtual repository is ‘example-framework-snapshot’

buildscript {
  ext {
    springBootVersion = '1.5.2.RELEASE'
  }
  repositories {
    maven {
      // Your virtual repository
      url 'http://localhost:8081/artifactory/example-framework-snapshot'
    }
  }
  dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
  }
}

apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'war'
apply plugin: 'org.springframework.boot'

group = 'com.security.sample'
version = '0.0.1-SNAPSHOT'

description = """sample-security Webapp"""

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
  maven {
    url 'http://localhost:8081/artifactory/example-framework-snapshot'
  }
}

dependencies {
  compile ('com.example:example-security-extras:0.0.1-SNAPSHOT')
  compile ('org.springframework.boot:spring-boot-starter-web')
  compile ('redis.clients:jedis')
  testCompile ('junit:junit')
}