Modern Maven Pom Templates
-
08-07-2021 - |
Question
Every time I make a new proper project using Maven hosted on Github I have to go look at either one of my own old projects and copy the pom file or I go find a project that I think does a good job and copy there POM file. Then I have to go search and replace things like project name... etc.
Now Maven has a solution to this through archetypes but I have yet to see one that is modern enough such that it:
- Uses the release plugin and deploys to SonaType Central Maven reop.
- Connects to Github (meaning the scm connections and release plugin work do the right thing)
- Makes all three jar artifacts (sources, javadoc, and regular jar)
I have contemplated make some giter8 templates but was hoping somebody already did something like this (most of the g8 templates are for sbt
).
Solution 3
It turns out its incredible easy to create your own maven archetype.
- Just make a generic project with stuff you like to use
- In the project directory run
mvn archetype:create-from-project
- Generally Maven guesses the right things to make variable but if not you just edit the Velocity templates.
- Install your archetype locally with
mvn install
- To use your new archetype:
mvn archetype:generate -DarchetypeGroupId=com.mygroup -DarchetypeArtifactId=my-archetype
Now the only caveat is that there is not very good doc on the web that I could find on the archetype system. Like its unclear what variables you have available to you for Velocity (although most of them are obvious).
http://maven.apache.org/archetype/maven-archetype-plugin/create-from-project-mojo.html
OTHER TIPS
You can use com.jcabi:parent:pom
which does exactly what you need and many more. It deploys to Sonatype, defines common dependencies with versions, pre-configures most popular plugins, and defines a few useful profiles.
This article explains more: Don't Repeat Yourself in Maven POMs; Use Jcabi-Parent
You could have a look at the parent pom released by Sonatype. It's intended to be used as a parent pom for projects that deploy to oss.sonatype.org
(which may or may not be promoted to Maven Central).
When the sonatype-oss-release profile is enabled, it will ensure that sources and javadocs are built. It also includes an example of the <scm>
pom element.