home.


Tagged: gradle


Gradle: Bintray tutorial

We can use gradle to upload our project as a maven repository to bintray, and by extension jcenter and mavenCentral.

Sign up to at https://bintray.com. Create a repository, let’s say we call it wonderful.

Now let’s create the build.gradle file giving a buildscript repository, adding the bintray plugin, and applying the java, maven and maven-publish plugins.

buildscript {
  repositories {
    jcenter()
  }
}

plugins {
  id "com.jfrog.bintray" version "1.7"
}

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

Now let’s add the standard version, archivesBaseName, group and description. We’ll reference this as project.version etc later on.

archivesBaseName = 'jersey-json-wadl'
group = 'com.newfivefour'
version = '0.0.1'
description = 'Something, innit'

The maven-publish plugin allows us to define a publishing.publications maven block. This defines what we’re publishing (java components), the groupId, the artifactId and the version. The publication is called MyPublication.

publishing {
  publications {
      MyPublication(MavenPublication) {
          from components.java
          groupId = project.group
          artifactId = project.archivesBaseName
          version = project.version
      }
  }
}

Now let’s create the bintray block. We get our bintray user and api key from the system environment, and we reference the name of the publication created above, and we tell bintray to publish the artifact, instead of holding it on bintray waiting for us to press ‘publish’.

bintray {
    user = System.getenv('BINTRAY_USER')
    key = System.getenv('BINTRAY_KEY')
    publications = ['MyPublication']
    publish = true
    pkg ...
}

The pkg part is where things get interesting:

    pkg {
        repo = 'wondeful'
        name = 'somerandomname'
        licenses = ['Apache-2.0']
        vcsUrl = 'https://github.com/newfivefour/jerseyjsonwadl.git'
        version {
            name = project.version
            desc = project.description
            released  = new Date()
        }
    }

We define the repo name we created on bintray. We give it a name, or a package as it will appear on the bintray UI. We also specify the licence and VCS url. The version block reuses the version, desc from our project and we set the release date to now.

Running ./gradlew --no-daemon bintrayUpload (we’re using --no-daemon since the daemon is buggy and keeps old values) will upload our repository. Wait a few seconds, and our maven repository should start to appear at https://YOURUSERNAME.bintray.com/wonderful/. You should see in the bintray web interface, too.

Now, in another gradle project, if you define a repository via maven { url "http://dl.bintray.com/YOURUSERNAME/wonderful" } you can call a dependency called compile 'com.newfivefour:jersey-json-wadl:0.0.1'.

gradle bintray

Gradle: Generate POM file etc with the maven plugin

The maven plugin lets us install a maven package, the pom file etc, to a repository. The default repository is our local file system.

Let’s say we’re building a Java project, here’s the build script. It’s as normal, except for the archivesBaseName, group and version.

apply plugin: 'java'
apply plugin: 'maven'

repositories {
    mavenCentral()
}

dependencies {
  ...
}

archivesBaseName = 'jersey-json-wadl'
group = 'com.newfivefour'
version = '0.0.1'

Now if we do ./gradew install (I’m assuming you’re using the gradle wrapper), it will install this to ~/.m2/repository/com/newfivefour/jersey-json-wadl/0.0.1/.

Within that directory you’ll have your POM file and the generated JAR. In the directory above that, there’s a maven-metadata-local.xml file that documents the versions and last updated time amoung other things.

If you run ./gradlew install again with a new version attribute, you’ll get a new directory above, this time named 0.0.2 for example.

gradle maven pom

Scala: Using gradle to build your project

If you want to use gradle to compile scala, ensure you have gradle, either in your system or via the gradle wrapper.

Create this build.gradle file:

apply plugin: 'scala'

repositories {
    mavenCentral()
}

dependencies {
  compile 'org.scala-lang:scala-library:2.11.6'
  // Your other deps here
}

sourceSets.main.scala.srcDirs = ['.']

This includes the scala library needed to compile, 2.11.6 is the latest scala at time of writing.

It also changes the defautl scala source file location to the current directory, but if you’re using src/main/scala there’s no need for this line.

Finally you have run gradle build or ./gradlew build if you’re using the gradle wrapper.

scala gradle

Gradle: Copy all dependencies into local directory

Create a gradle build file, call it deps.gradle, as follows:

apply plugin: 'java'

repositories {
   mavenCentral()
}

dependencies {
   compile group: 'org.glassfish.jersey.bundles', name: 'jaxrs-ri', version: '2.23.1'
}

task copyDependencies(type: Copy) {
   from configurations.compile
   into 'dependencies'
}

Then call gradle -b deps.gradle copyDependencies and it’ll put all the dependencies into the dependencies/ directory.

gradle gradle-dependencies

Android: Sign your Android app from the command line with an external signing file and Gradle

I’m assuming in your in your app’s app directory, you’ve made a signing key and you’re using Gradle 2.10 or above.

In your gradle file, withing android, insert this.

signingConfigs {
    release {
        storeFile file(props['keystore'])
            storePassword props['keystore.password']
            keyAlias props['keyAlias']
            keyPassword props['keyPassword']
    }
  buildTypes {
      release {
          signingConfig signingConfigs.release
      }
  }
}

Then in the file referenced as keystore.password, insert this:

keystore=android.key
keystore.password=YOURSTOREPASSWORD
keyAlias=YOURKEYALIAS
keyPassword=YOURKEYPASSWORD

The android.key refers to the key file. And the rest is as you entered in your signing key.

Now you can run gradle assembleRelease and not upload your keystore.password file.

android gradle

Page 1 of 5
Next