I want to build a Maven archetype that checks whether the supplied artifactId and groupId match a given regex. In this way, I want to enforce the naming conventions of our organisation, e.g. ear files having names ending with -app and all groupIds starting with de.companyname.
Is this possible?
I found that you can check against a regex for requiredProperty
https://maven.apache.org/archetype/archetype-models/archetype-descriptor/archetype-descriptor.html
but the given value is ignored when I build the archetype through eclipse, which could be due to an old version of the maven-archetype-plugin that is used in eclipse (and this is not applicable to "build-in" properties like groupId or artifactId).
This:
... is the way to define a required property (with defaults and validation). However, IIRC, it was introduced in v3.0.0 of the archetype plugin so perhaps you are using a prior version.
Edit 1: in response to this question "can validationRegex be applied to artifactId and groupId". Yes, it can. It can be applied to any entry in
requiredPropertiesbut with this caveat:validationRegexonly works for inputs supplied at the command line, so providing adefaultValueor defining a value via a command line parameter (-DgroupId=...,-DartifactId=...) side steps validation. Here's a concrete example, given the followingrequiredPropertiesinarchetype-descriptor.xml:The following command:
mvn archetype:generate -DarchetypeGroupId=... -DarchetypeArtifactId=... -DarchetypeVersion=... -DgroupId=com.foo.barwill result incom.foo.barbeing used for groupId and the user will be prompted to supply an artifactId like so:So far so good (sort of).
But the following command
mvn archetype:generate -DarchetypeGroupId=... -DarchetypeArtifactId=... -DarchetypeVersion=... -DartifactId=whateverwill result inCOM.XYZ.PQRbeing used for groupId even though that does not conform to thevalidationRegex.Similarly; the following command
mvn archetype:generate -DarchetypeGroupId=... -DarchetypeArtifactId=... -DarchetypeVersion=... -DartifactId=WHATEVERwill result inCOM.XYZ.PQRbeing used for groupId andWHATEVERbeing used for artifactId even though those values do not conform to thevalidationRegex.So, in summary: the
validationRegexworks for any requiredProperty (whether its a reserved property - such as artifactId - or a bespoke property) but it only applies to values which are provided interactively and hence setting a default value or supplying a value via a command line parameter side steps validation.Note: even if you do use
validationRegexyou might also want to consider using the Maven Enforcer Plugin's requireProperty rule because thes project properties you wan to enforce could be changed after the archetype has been used to create the project. From the docs:Here's an example: