≡ Menu

A New Maven Archetype for Starter Project

I’ve recently put together a new Maven archetype, based on something I saw in Freenode’s ##java channel a few weeks ago. Basically, someone had built their own archetype for “standard projects,” with a few sensible dependencies and defaults, and while I thought it was a worthwhile effort, it didn’t fit what I found myself typically doing.

So I built my own, at https://github.com/jottinger/starter-archetype.

Primary features are:

  • Java 8 as a default Java version
  • Typical dependencies
  • Maven Shade

Java 8

Look, Java 7 and older has been end-of-lifed; you can pay Oracle for support and fixes, but you shouldn’t unless you have a real reason to do so. Java 8 is the current Java version. You should be using it, and so should I. Therefore, I do.

It’s an unfortunate aspect of Maven that it defaults to an older version of the Java specification. My starter archetype presumes you want to live in the current year.

Typical Dependencies

My starter archetype has seven dependencies; they are the ones I either include without thinking about it (because I know I’m going to want or need them), or they’re the dependencies that are so common that few projects would blink at their inclusion.

The dependencies are organized into three groups: runtime dependencies, one compile-time dependency, and testing dependencies.

The compile-time dependency is Lombok; it helps remove boilerplate from Java code, so I can build a Java object with mutators, accessors, toString(), hashCode(), and equals() very simply:

@Data
public class Thing {
    // public String getName() and setString(String name) are built for me through Lombok
    String name;
}

The runtime dependencies are Guava, Logback, and Apache’s commons-lang3. Guava and commons-lang3 have a lot of overlap, but both are very common; logback is a logging library that leverages slf4j, so it’s a workable default logging library that doesn’t force you to stick with it if you don’t like it.

All together, they use up roughly 3.5MB of disk space for a starting classpath, with Guava using 2.3MB of it. Given how useful Guava, et al, are, I think this is entirely worthwhile; most projects will have these libraries (or something nearly like them), so it’s acceptable.

The testing libraries are TestNG, assertj, and H2.

It’s arguable that JUnit 5 might have caught up to TestNG in a lot of ways, but there are still some features I really like from TestNG that JUnit doesn’t have built-in support for (namely, data providers – although note that there is a project that provides data provider support for JUnit, unsurprisingly called junit-dataprovider).

AssertJ is a set of fluent assertions for Java. It’s not necessary – for example, I’ve used TestNG’s innate assertions for years without a problem – but the fluent assertion style is rather nice. The actual dependency is assertj-guava – which includes the base library for assertj – but I chose assertj-guava because of the inclusion of Guava as a default runtime dependency.

H2 is an embedded database. I use embedded databases so much for first-level integration tests that it seemed silly not to include it; I have a lot of sandbox projects that don’t use H2, but as soon as I do anything with a database, this gets included, so it makes sense as a trivial “default testing library.”

Maven Shade

I wanted to be able to generate an executable jar by default, not because I do that very much, but because it seemed to be a sane default. (Usually, my starter projects exist to support a test that demonstrates a feature, as opposed to being an independently useful project.)

Because I wanted others to be able to see more use out of my starter project, I added Maven Shade to create a viable entry point and an executable jar.

Using the archetype

Right now, you’d need to run the following sequence at least once to use the archetype:

git clone https://github.com/jottinger/starter-archetype.git
cd starter-archetype
mvn install

Then, to build a project with the archetype, you’d run:

mvn archetype:generate \
    -DarchetypeGroupId=com.autumncode \
    -DarchetypeArtifactId=starter-archetype \
    -DarchetypeVersion=1.0-SNAPSHOT

Future Enhancements

It’s still very much a work in progress. Things I’d like to do:

  • Migrate into the main Maven repositories (publish, in other words)
  • Add publishing support to the archetype itself
  • Include better throwaway demonstrations of the dependencies (a difficult task, as the default classes are meant to be thrown away en masse)
  • Figure out better default libraries, if possible

You’re welcome to fork the project, create issues, comment, use with wild abandon, as you like. It’s licensed under the Apache Source License, 2.0.

{ 0 comments… add one }

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.