Digital Magpie

Ooh, ooh, look - shiny things!

Installing to the Local Maven Repo With Gradle

I’ve been playing around with different build tools for my Java projects recently, having never been very happy with Maven. Probably the best that I’ve found is Gradle: it has an easy to use build file format, and seems pretty flexible if you need to do something a little differently.

Unfortunately the documentation isn’t as comprehensive as it could be, and one of the areas where it’s not too great is in it’s interaction with the Maven repository system. So, here’s the magic incantation that you have to add to your build file in order to have gradle install install things correctly to your local repository:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
apply plugin 'maven'
configure(install.repositories.mavenInstaller) {
    pom.project {
        groupId 'com.example'
        artifactId 'project-name'
        inceptionYear '2011'
        packaging 'jar'
        licenses {
            license {
                name 'Eclipse Public License (Version 1.0)'
                url 'http://www.eclipse.org/legal/epl-v10.html'
                distribution 'repo'
            }
        }
    }
}

this will install the project binaries, to also install source and JavaDocs (which every project should really do) then you’ll also need to add:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
task sourcesJar(type: Jar, dependsOn:classes) {
     classifier = 'sources'
     from sourceSets.main.allSource
}

task javadocJar(type: Jar, dependsOn:javadoc) {
     classifier = 'javadoc'
     from javadoc.destinationDir
}

artifacts {
     archives sourcesJar
     archives javadocJar
}

How to Enter Straight Quotes in Word

When I’m writing technical docs in MS Word (an unfortunate day-job-related requirement) one of the things that can be annoying it the fact that it ‘helpfully’ converts straight quotes into curly quotes, even in code samples and other places that it shouldn’t. The easy way to get rid of this is to hit undo (⌘-z) immediately after typing the quote, this will undo the auto-correction but leave the actual quote character in place.

How to End the Deficit

> I could end the deficit in 5 minutes. You just pass a law that says
that anytime there is a deficit of more than 3% of GDP all sitting
members of congress are ineligible for reelection.

He’s talking about the US Congress, but it would work here as well.

Handling Flag Arguments

Martin Fowler has a new bliki entry talking about flag arguments, defined as:

A flag argument is a kind of function argument that tells the function to carry out a different operation depending on its value.

And, as an example of this API style:

1
2
3
Class Concert {
  public Booking book(Customer aCustomer, boolean isPremium);
}

And his preferred API design:

1
2
3
4
Class Concert {
  public Booking bookRegular(Customer aCustomer);
  public Booking bookPremium(Customer aCustomer);
}

The problem with this, as Mr. Fowler points out, is that it can lead to problems with the implementation. His preferred solution is to have a private implementation method exactly like the original problematic API:

1
private Booking bookImpl(Customer aCustomer, boolean isPremium)

But if we think about the problem for a little longer we can see that there is a better option available to us. The real problem with flag arguments is that they lose information at the call site, so the original example method would be called like this:

1
2
3
4
. . .
myConcert.book(poorCustomer, false);
myConcert.book(richCustomer, true);
. . .

There’s nothing to say what those true and false arguments actually mean. We can just define a type-safe enum to use instead of the boolean, that way the information is still present at the call site. This was our API becomes:

1
2
3
Class Concert public {
  enum TicketType { REGULAR, PREMIUM }
  public Booking book(Customer aCustomer, TicketType ticketType)

And at the call site:

1
2
3
4
. . .
myConcert.book(poorCustomer, TicketType.REGULAR);
myConcert.book(richCustomer, TicketType.PREMIUM);
. . .

This is easier to implement, and works for multi valued (e.g. integer) flags as well.

New York Post Blocks iPad Users

It seems that the New York Post have decided to block all access to their site from iPads, telling users to download their (subscription only) app instead. As Scripting News said…

I wonder how Apple feels about this? I can’t imagine they like it. I can see the ads now. ‘Get an Android tablet to read the web.’ Hmmm… I wonder how long said app will remain in the store after pulling a trick like this?

Writing Smart Code

This quote has been doing the rounds lately, at least I’ve seen it on two or three different blogs, most recently here

> Debugging is twice as hard as writing the code in the first place.
> Therefore, if you write the code as cleverly as possible, you are, by
> definition, not smart enough to debug it.

I rather like that.

How to Convert PDFs to Postscript

It’s easy using pdftops, part of the Poppler suite of programs. First make sure it’s installed, or install it (using Homebrew on OS X):

1
2
$ brew update
$ brew install poppler

Then the command pdftops infile.pdf outfile.ps can be scripted as per usual, something like this:

1
2
3
4
for page in pages/*.pdf
do
  pdftops $page postscript/`basename -s .pdf $page`.ps
done

Recorded in the interest of helping Google organise my brain.