Over a million developers have joined DZone.
Gold Partner

Replace your Scripts with Gradle Tasks

I really like Maven, and I really like the declarative build style, but recently I finally came to understand why Gradle is better.

· DevOps Zone

The DevOps Zone is brought to you in partnership with Go Continuous Delivery. Learn the 5 key patterns to setting up a successful deployment pipeline, including designing parallel workflows, running tests in parallel, and more.

I really like Maven, and I really like the declarative build style, but recently I finally came to understand why Gradle is better.

For small projects that produce a common library JAR, you can still use Maven, but real-life, complex software projects always contain a lot of support scripts for deployment, copying artifacts, and so on. For some of those tasks you can find Maven plug-ins, for most of them you can write Maven plugins, but in real life you have shell scripts to do the job.

The problem with such scripts is that they contain paths and file names, which are part of the build. And if you change your pom.xml, you should change those scripts as well.

Gradle solves the problem. Your scripts are now part of the build; they can use build paths and artifact names, and they can depend on build stages.

I will show a small example: deploying WAR using SSH to a remote server. The example uses the Gradle SSH plugin.

Notice that we use the WAR name from the build and that the deployment task depends on the test, so we can't deploy if the tests fail

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.hidetake:gradle-ssh-plugin:0.1.7'
    }
}

apply plugin: 'ssh'
apply plugin: 'war'


war {
    archiveName = 'rest.war'
}

dependencies {
  //...


}


ssh {
    config(StrictHostKeyChecking: 'no')
}
remotes {
    testserver {
        host = 'myserver.com'
        user = sshuser
        identity = file("${System.properties['user.home']}/.ssh/id_dsa")
    }


}

task deployTestServer << {
    deploy(remotes.testserver)
}



deployTestServer.dependsOn build


def deploy(def server) {
    logger.lifecycle("Deploying to $server")
    logger.lifecycle("Copying ${war.archivePath.absolutePath} to $server ... Be patient .. takes time ...")
    sshexec {
        session(server) {
            put(war.archivePath.absolutePath, war.archiveName)
        }
    }
    sshexecute(server, '/usr/share/tomcat7/bin/tomcat7 stop')
    sshexecute(server, 'rm -rf /var/lib/tomcat7/webapps/rest*')
    sshexecute(server, "cp ${war.archiveName}  /var/lib/tomcat7/webapps")
    sshexecute(server, '/usr/share/tomcat7/bin/tomcat7 start')
    sshexecute(server, '/usr/share/tomcat7/bin/tomcat7 status')
}



def sshexecute(def server, def cmd) {
    logger.lifecycle("Executing '$cmd'  ...")
    sshexec {
        session(server) {
            execute(cmd, pty: true)
        }
    }
 

}  


The DevOps Zone is brought to you in partnership with Go Continuous Delivery. Discover why "Hardly Anyone Knows Continuous Delivery."

Topics:
java,devops,maven,jar,build,tips and tricks,gradle,war

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}