Andrey Hihlovskiy

Professional blog on groovy, gradle, Java, Javascript and other stuff.

Tag Archives: programming

groovy script for running jetty server

The following script starts jetty server and opens the folder, specified on command line, for http access (read-only):

#!/usr/bin/env groovy

@Grab('javax.servlet:javax.servlet-api:3.0.1')
@Grab(group='org.eclipse.jetty', module='jetty-webapp', version='8.1.8.v20121106')
@Grab(group='org.eclipse.jetty', module='jetty-server', version='8.1.8.v20121106', transitive=false)
@Grab(group='org.eclipse.jetty', module='jetty-servlet', version='8.1.8.v20121106', transitive=false)
@GrabExclude('org.eclipse.jetty.orbit:javax.servlet')

import org.eclipse.jetty.server.Server
import org.eclipse.jetty.servlet.*
import groovy.servlet.*

def publishedFolder = args ? args[0] : '.'

def server = new Server(8080)
def context = new ServletContextHandler(server, '/', ServletContextHandler.SESSIONS)
def webappContext = new org.eclipse.jetty.webapp.WebAppContext(publishedFolder, '/jetty')
context.setHandler(webappContext)
server.start()
println 'Jetty server started. Press Ctrl+C to stop.'

Usage:

  1. Save this script to file “jetty.groovy”
  2. Invoke on command-line:
    groovy jetty.groovy /path/to/some/folder"
  3. Enter address in web-browser:
    http://localhost:8080/jetty

Expected result: you see the content of the folder “/path/to/some/folder” in the web-browser.

Groovy language, spaceship operator

x <=> y

Useful in comparisons:

  • returns -1 if x is smaller than y
  • return 0 if x equals to y
  • returns 1 if x is greater than y.

Isn’t it sweet?

Partial interface implementation in groovy

interface X {
  void a()
  void b()
}

class XAdapter implements X {
  void a() { println 'default implementation of a' }
  void b() { println 'default implementation of b' }
}

def o = [
  a: { println 'overridden implementation of a' }
] as XAdapter

o.a()
o.b()

will output:

overridden implementation of a
default implementation of b

Gradle script: multiproject git-gradle management

Suppose you build your software project from many open-source components, most of which are already available via git. How to automate clone/pull/build/install cycle, especially across projects from different git-repositories? How to establish high-level inter-project dependencies?

For that I wrote gradle script, which implements multiproject git-gradle management. It works as follows: you write configuration file, name it “config.gradle”, put it to the same folder as “build.gradle” (of multiproject git-gradle) and then run “gradle build”.

Full documentation and sources are available at:

https://github.com/akhikhl/multiproject-git-gradle

Script for batch installation of maven artifacts

I created gradle script that delivers batch installation of maven artifacts to local maven repository (source code here: https://github.com/akhikhl/contribs).

The script supports two tasks – installContribs and cleanContribs – but can be extended with additional tasks (for example, implementing deployment to corporate repo).

how it works:

1. you call it with the command-line:

gradle -b contribs.gradle

(or you rename script to “build.gradle” and just put it somewhere in the multi-project tree, it will be called by gradle automatically).

2. installContribs task iterates the current folder (where the script resides) and all it’s subfolders

3. in each folder it iterates files with extension “pom”

4. for each found pom-file it looks for “.jar”, “-sources.jar” and “-javadoc.jar” and install all found files (together with pom) to the local maven repository.

5. pom-file without jars will be installed as an artifact on it’s own. Typical use-case: installation of parent-poms and aggregator-poms.

The script accurately calculates inputs/outputs. If all files were not changed since the last installation, it does not install anything and shows “UP-TO-DATE” in the console.

cleanContribs task makes script “forget” about the time of the last artifact installation. As the result, running script with installContribs task will install all artifacts anew.

Cargo-Culting in Javascript

Cargo-Culting in Javascript

Nice article. Somewhat opinionated, but fun to read.