Quantcast
Channel: SCN : Blog List - All Communities
Viewing all articles
Browse latest Browse all 2548

Quick introduction: gradle meets HCP

$
0
0

this weekend I was playing around a little with gradle as an alternative to maven. I always found those tons of XML very annoying and hardly readable. So the first time I stumbled upon gradle I was fascinated by it. I won't start a discussion how/if/when gradle is better, I just wanted to show you quickly how easy it can be for your daily HCP development. For maven we have plugins, for gradle not, but it was pretty easy to "rebuild" them. For a general introduction to gradle visit their homepage https://gradle.org/

 

So here is the script:

import org.gradle.internal.os.OperatingSystem
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'war'
group = 'com.yourCompany.hcp-project'
version = '0.0.1-SNAPSHOT'
description = """"""
sourceCompatibility = 1.7
targetCompatibility = 1.7
project.buildDir = 'target'
project.webAppDirName = 'WebContent'
project.ext {  localServer = "${buildDir}" + File.separator + "server"  sdk = "${buildDir}" + File.separator +"sdk"  neo = { ->        if(OperatingSystem.current().isWindows()) return "${sdk}" + File.separator + "tools" + File.separator + "neo.bat"        if(OperatingSystem.current().isLinux()) return "${sdk}" + File.separator + "tools" + File.separator + "neo.sh"  }  httpPort = "8083"  waitUrl = "http://localhost:${httpPort}/" + war.archiveName.replace(".war", "/")  account = "abcdef"  application = "abc"  host = "hana.ondemand.com"  //Shoudl go into local .gradle/gradle.properties which should be excluded fom SCM (e.g. Git)  password = "secret"  user = "p12345678"
}
repositories {     maven { url "http://repo.maven.apache.org/maven2" }
}
dependencies {    compile group: 'commons-io', name: 'commons-io', version:'2.4'    compile group: 'org.apache.maven.plugins', name: 'maven-enforcer-plugin', version:'1.0.1'    compile group: 'org.apache.olingo', name: 'olingo-odata2-api', version:'2.0.6'    compile group: 'org.apache.olingo', name: 'olingo-odata2-core', version:'2.0.6'    compile group: 'javax.servlet', name: 'servlet-api', version:'3.0-alpha-1'    compile group: 'org.apache.cxf', name: 'cxf-rt-frontend-jaxrs', version:'2.7.5'    compile group: 'org.apache.olingo', name: 'olingo-odata2-jpa-processor-api', version:'2.0.6'    compile group: 'org.apache.olingo', name: 'olingo-odata2-jpa-processor-core', version:'2.0.6'    compile group: 'org.slf4j', name: 'slf4j-log4j12', version:'1.7.1'    compile group: 'org.eclipse.persistence', name: 'eclipselink', version:'2.6.1-RC1'    testCompile group: 'junit', name: 'junit', version:'4.12'    testCompile group: 'com.sap.cloud', name: 'neo-javaee6-wp-maven-plugin', version:'2.78.13'    testCompile group: 'org.mockito', name: 'mockito-core', version:'2.0.31-beta'    providedCompile 'com.sap.cloud:neo-javaee6-wp-sdk:2.78.13@zip'    providedCompile group: 'com.sap.cloud', name: 'neo-javaee6-wp-api', version:'2.78.13'
}
task installSdk(type:Copy){    description 'Task will install Neo SDK'    def outputDir = file(project.sdk)    def a = file(findJar('neo-javaee6-wp-sdk'))    from zipTree(a)    into(outputDir)
}
task installServer(type: Exec){    description 'Task will install local server and the SDK in case it is missing'    doFirst{        if(!file(project.sdk).exists()) tasks.installSdk.execute()    }        commandLine neo(), 'install-local', '--location', localServer, '--http-port', httpPort
}
task startServer(type: Exec){    description 'Task will start local server'    commandLine neo(), 'start-local', '--location', localServer, '--wait-url', waitUrl
}
task deploy(type:Exec, dependsOn: war){    description 'Task will deploy war to local server'    commandLine neo(), 'deploy-local', '--location', localServer, '--source', war.archivePath 
}
task cloudDeploy(type:Exec, dependsOn: war){    description "Task will deploy war in hana Cloud Platform Account"    commandLine neo(), 'deploy', '--account', account, '--application', application, '--host', host, '--password', password, '--user', user, '--source', war.archivePath 
}
def findJar(prefix) {    configurations.providedCompile.files.find { it.name.contains(prefix) }
}

So what will this script do:

Line 1 - 16

General project setup the only thing noticeable here is project.webAppDirName, because it is defaulted to webapp but my current project uses WebContent instead

Line 17 - 33

Here we declare project specific "variables" which we will need later, everything is straight forward except line 20-23. Here I am using some internal gradle API to determine the OS to know which executable I should take (neo.sh on Linux/Mac neo.bat on Windows)

Line 33 -53

Nothing special here only repository and dependency declaration, please consult the documentation of gradle

Line 54-60

Here the HCP specific stuff is starting. The task installSdk will locate the downloaded neo-javaee6-wp-sdk.zip (dependency from line 51) from your local repo(either maven or gradle) and extract it to target/sdk

Line 61-67

This task will execute a shell command (neo) to install a local server, I played around a bit and added an if statement, which basically checks if the sdk is already installed and if not the task installSdk will be called before the server installation

Lines 68 - til end

Basically what I explained already is just repeated again, nothing ne or exciting here just plain old neo cli commands, task deploy depnds on a task called war, which assembles the war file

 

The nice thing about gradle is the wrapper, which can provide you with a gradle executable so your team members don't even have to install gradle to run your builds, again please consult the documentation of gradle.

 

To see all available Tasks just run gradle tasks.

A common buidl would include command:

gradle build -> to build gradle artifacts

gradle installServer -> SDK and Server

gradle deploy -> deploy application to local server

gradle startServer -> to start the server


after that only deploy is needed when changes occure in your sources. You could easily write a task which would include all the above tasks to type less

 

Conclusion:

Compared to the time it took me to get to know maven this was a walk on the beach (remember it was only a few hours this weekend). A 79 line small build file with the basic stuff I need, one less dependency (maven-neo-plugin) and to patch in the rest of neo command is a piece of cake. obviously there is room for improvement here, you could externalize some properties or even the tasks, to have them available in all your projects. In case neo does return an error you could provide options not to stop the build or you could call neo stop-local before installing a server and and and... But nevertheless I am quite impressed with gradle and will continue my journey.

 

Please share your thoughts and don't hesitate to ask question

 

Hope you too have fun with gradle

 

Till the next time

Cheers

Mathias


Viewing all articles
Browse latest Browse all 2548

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>