- I could declare a property directly in my POM which has the substring of artifactId and then use it. But, the problem is we have so many projects and each of the projects have to declare this property. Also, we need to make sure that any new project in future declare this property too. Since, this is too manual, I was looking for an automated solution.
- I could write my own small maven plugin for this. This plugin would get substring of artifactId and then set a property. This property could then be used by another plugin downstream in the lifecycle. This option would work, but then I need to create a project for this and maintain it.
- I could use GMaven plugin to write groovy code and then get substring. At this point, I came across maven properties plugin. The properties plugin can read properties file and set them as maven properties. So, I was thinking about writing the substring to a properties file using groovy, which I could then read using properties plugin. Then, I could use this maven property downstream. Even though this would work, it seemed too much work just to get a substring.
- Then, I found that GMaven plugin is also capable of setting maven properties. Apart from writing groovy code, I could use project.properties.getProperty and project.properties.setProperty to get/set maven properties inside GMaven plugin. This was very handy. So, my solution was just to add this execution to my gmaven plugin:
String artifactIdSubStr = new String(project.artifactId).substring(10) project.properties.setProperty('myArtifactId', artifactIdSubStr)
Just two lines of groovy code and I was able to use myArtifactId property later on. GMaven plugin came in very handy here.
Using GMaven plugin, we could run an inline groovy script (like what I did above), run external groovy script and also run a remote groovy script. We could also call Java or Groovy classes and call them from here. Having the power of executing groovy and java code inside POM is great. But, we should be careful on how we use it though. If we need to do lot of things, then it makes sense to write a custom maven plugin than to go this way.
Some of the variables that could be used here are project, pom, session, settings, log, ant and fail. If we want to do some custom checks and if the check does not pass we may want to fail the build. In these cases we could implement the check in groovy and then use fail() to fail the build.
You can find more information about gmaven plugin here.