Unconstant Conjunction latest posts

Logic in Maven Archetypes

Time for a real first post. Recently, I’ve been working on creating a Maven archetype for a certain open-source project, and discovered to my delight that archetypes actually support a limited kind of logic. If it’s not immediately obvious why that would be useful, consider that you can make use of this logic to, for example, optionally add a dependency based on a custom parameter.

Making use of the fact that Maven builds its archetypes using Apache’s Velocity template engine, you can embed statements reminiscent of C-style preprocessor directives in any file in the archetype. To add an optional dependency, try embedding the logic below in the pom.xml file

<dependencies>
	...
#if (${includeNetty} == 'true' || ${includeNetty} == 'yes' || ${includeNetty} == 'y')
	<dependency>
		<groupId>io.netty</groupId>
		<artifactId>netty</artifactId>
		<version>3.4.6.Final</version>
		<scope>compile</scope>
	</dependency>
#end
</dependencies>

Continue Reading →