Merge pull request #5 from aasaru/artifactory

FINCN-148 build with travis-ci.com and upload artifacts to Artifactory
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..164f3b3
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,30 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+language: java
+sudo: false
+jdk:
+- openjdk8
+install: true
+script: "./travis.sh"
+env:
+  global:
+    - BUILD_SNAPSHOTS_BRANCH=develop
+    - ARTIFACTORY_URL=https://mifos.jfrog.io/mifos
+    - ARTIFACTORY_USER=travis-ci
+    - secure: "r6tDcGVTLtQPDHDF+4uk5/+U50reTxu6uc5LvpMjHrVsEIzK9NXZ4MbrtgcAaqzghzL3JsWjNRgxkiw6bEiiscroiGk5eUKw25E99I4HJrD4JNKGNLmYsJ22h4X+YENz/PYSqrUK/JKkK7Hw4BcVXAJYKE+WxWph22FAD0oK2rVk14zk2nnp+b8lvXIPRS8VRWJIGQPJrAx7lDtluO0vUhzyAjLUcHlhWdpjGjWXvYJrIJBGpATwIhK6oMXxvTLzicYB1LE0YOHaPCG+2cf1TYZ0HXVRBu1pMyZlM8Brn6jXlvxLOZdLH4v3cJCPg/hAJVyfcbk2a2jA1zuwR/RBgvGWWRZhlUnwrOC1U/PvchP8TvSx+0zx/HgqD1Wz8d+n6cyJ016jF/957N7zKTobx9GCvjtqOTen4iKhbfLyrQ4hX3k5z8oIHaoRNX5XDo/kkaQqnYQX8xaB9ntP/0fbULL6rBOHCpcj+YBXtyTnhecFZ0yCi/8YQ0/lit6uhghTarG4rujboCFHGieQKyK6FI/DkcLkGjZ2O7NDgQskHZnKdA9vEk29b1Ny+TSPExHoPNvjK+S+N4+uzEzijTPPfmNYKvqWdltODmKp2GaSe46ScK40vTKkHu9laaawxDDUrev27Wuh37noDNSP/6euq++DURKF9GE76jy98PrXYmc="
diff --git a/README.md b/README.md
index 6df26df..76716d0 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,4 @@
-# Apache Fineract CN Authorization Library
+# Apache Fineract CN Authorization Library [![Build Status](https://api.travis-ci.com/apache/fineract-cn-anubis.svg?branch=develop)](https://travis-ci.com/apache/fineract-cn-anubis)
 
 This project provides authorization for Apache Fineract CN services.
 
diff --git a/api/build.gradle b/api/build.gradle
index 76c2248..1cf7f0b 100644
--- a/api/build.gradle
+++ b/api/build.gradle
@@ -29,6 +29,7 @@
 plugins {
     id 'com.github.hierynomus.license' version '0.13.1'
     id("org.nosphere.apache.rat") version "0.3.1"
+    id "com.jfrog.artifactory" version "4.9.5"
 }
 
 
@@ -55,7 +56,7 @@
             from components.java
             groupId project.group
             artifactId project.name
-            version project.version
+            version project.findProperty('externalVersion') ?: project.version
         }
     }
 }
diff --git a/build.gradle b/build.gradle
index 08ae5f8..94fbbc4 100644
--- a/build.gradle
+++ b/build.gradle
@@ -46,6 +46,13 @@
     dependsOn publishComponentTestToMavenLocal
 }
 
+task artifactoryPublish {
+    group 'all'
+    dependsOn gradle.includedBuild('api').task(':artifactoryPublish')
+    dependsOn gradle.includedBuild('library').task(':artifactoryPublish')
+    dependsOn gradle.includedBuild('test').task(':artifactoryPublish')
+}
+
 task licenseFormat {
     group 'all'
     dependsOn gradle.includedBuild('api').task(':licenseFormat')
diff --git a/component-test/build.gradle b/component-test/build.gradle
index ba597d2..b0152c0 100644
--- a/component-test/build.gradle
+++ b/component-test/build.gradle
@@ -32,6 +32,7 @@
 plugins {
     id 'com.github.hierynomus.license' version '0.13.1'
     id("org.nosphere.apache.rat") version "0.3.1"
+    id "com.jfrog.artifactory" version "4.9.5"
 }
 
 apply from: '../shared.gradle'
diff --git a/library/build.gradle b/library/build.gradle
index a2f9f90..8a96742 100644
--- a/library/build.gradle
+++ b/library/build.gradle
@@ -28,6 +28,7 @@
 plugins {
     id 'com.github.hierynomus.license' version '0.13.1'
     id("org.nosphere.apache.rat") version "0.3.1"
+    id "com.jfrog.artifactory" version "4.9.5"
 }
 
 apply from: '../shared.gradle'
@@ -65,7 +66,7 @@
             from components.java
             groupId project.group
             artifactId project.name
-            version project.version
+            version project.findProperty('externalVersion') ?: project.version
         }
     }
 }
diff --git a/shared.gradle b/shared.gradle
index 2dc220c..4cc4eb0 100644
--- a/shared.gradle
+++ b/shared.gradle
@@ -41,6 +41,7 @@
 repositories {
     jcenter()
     mavenLocal()
+    maven { url 'https://mifos.jfrog.io/mifos/libs-snapshot/' }
 }
 
 configurations {
@@ -77,6 +78,22 @@
     duplicatesStrategy = DuplicatesStrategy.EXCLUDE
 }
 
+artifactory {
+    contextUrl = System.getenv("ARTIFACTORY_URL")
+    publish {
+        repository {
+            repoKey = project.findProperty('artifactoryRepoKey')
+            username = System.getenv("ARTIFACTORY_USER")
+            password = System.getenv("ARTIFACTORY_PASSWORD")
+        }
+
+        defaults {
+            publications ('apiPublication', 'libraryPublication', 'testPublication')
+        }
+    }
+}
+artifactoryPublish.dependsOn('clean','publishToMavenLocal')
+
 license {
     header rootProject.file('../HEADER')
     strictCheck true
diff --git a/test/build.gradle b/test/build.gradle
index 3957b79..ae74863 100644
--- a/test/build.gradle
+++ b/test/build.gradle
@@ -28,6 +28,7 @@
 plugins {
     id 'com.github.hierynomus.license' version '0.13.1'
     id("org.nosphere.apache.rat") version "0.3.1"
+    id "com.jfrog.artifactory" version "4.9.5"
 }
 
 apply from: '../shared.gradle'
@@ -49,7 +50,7 @@
             from components.java
             groupId project.group
             artifactId project.name
-            version project.version
+            version project.findProperty('externalVersion') ?: project.version
         }
     }
 }
diff --git a/travis.sh b/travis.sh
new file mode 100755
index 0000000..03a75ee
--- /dev/null
+++ b/travis.sh
@@ -0,0 +1,71 @@
+#!/usr/bin/env bash
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+# Documentation: https://cwiki.apache.org/confluence/display/FINERACT/Fineract-CN+Artifactory
+
+#Exit immediately if a command exits with a non-zero status.
+set -e
+EXIT_STATUS=0
+
+# Builds and Publishes a SNAPSHOT
+function build_snapshot() {
+  echo -e "Building and publishing a snapshot out of branch [$TRAVIS_BRANCH]"
+  ./gradlew -PartifactoryRepoKey=libs-snapshot-local -DbuildInfo.build.number=${TRAVIS_COMMIT::7} artifactoryPublish --stacktrace || EXIT_STATUS=$?
+}
+
+# Builds a Pull Request
+function build_pullrequest() {
+  echo -e "Building pull request #$TRAVIS_PULL_REQUEST of branch [$TRAVIS_BRANCH]. Won't publish anything to Artifactory."
+  ./gradlew publishToMavenLocal rat || EXIT_STATUS=$?
+}
+
+# For other branches we need to add branch name as prefix
+function build_otherbranch() {
+  echo -e "Building a snapshot out of branch [$TRAVIS_BRANCH] and publishing it with prefix '${TRAVIS_BRANCH}-SNAPSHOT'"
+  ./gradlew -PartifactoryRepoKey=libs-snapshot-local -DbuildInfo.build.number=${TRAVIS_COMMIT::7} -PexternalVersion=${TRAVIS_BRANCH}-SNAPSHOT artifactoryPublish --stacktrace || EXIT_STATUS=$?
+}
+
+# Builds and Publishes a Tag
+function build_tag() {
+  echo -e "Building tag [$TRAVIS_TAG] and publishing it as a release"
+  ./gradlew -PartifactoryRepoKey=libs-release-local -PexternalVersion=$TRAVIS_TAG artifactoryPublish --stacktrace || EXIT_STATUS=$?
+
+}
+
+echo -e "TRAVIS_BRANCH=$TRAVIS_BRANCH"
+echo -e "TRAVIS_TAG=$TRAVIS_TAG"
+echo -e "TRAVIS_COMMIT=${TRAVIS_COMMIT::7}"
+echo -e "TRAVIS_PULL_REQUEST=$TRAVIS_PULL_REQUEST"
+
+# Build Logic
+if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then
+  build_pullrequest
+elif [ "$TRAVIS_PULL_REQUEST" == "false" ] && [ "$TRAVIS_BRANCH" != "$BUILD_SNAPSHOTS_BRANCH" ] && [ "$TRAVIS_TAG" == "" ]  ; then
+  build_otherbranch
+elif [ "$TRAVIS_PULL_REQUEST" == "false" ] && [ "$TRAVIS_BRANCH" == "$BUILD_SNAPSHOTS_BRANCH" ] && [ "$TRAVIS_TAG" == "" ] ; then
+  build_snapshot
+elif [ "$TRAVIS_PULL_REQUEST" == "false" ] && [ "$TRAVIS_TAG" != "" ]; then
+  build_tag
+else
+  echo -e "WARN: Unexpected env variable values => Branch [$TRAVIS_BRANCH], Tag [$TRAVIS_TAG], Pull Request [#$TRAVIS_PULL_REQUEST]"
+  ./gradlew clean build
+fi
+
+exit ${EXIT_STATUS}