Cannot get Maven Archetype requiredProperty validationRegex to work

196 Views Asked by At

I have an archetype and I am trying to add a new requiredProperty which should only allow one of two possible values: "hibernate" and "hibernate-reactive". In the archetype-metadata.xml, I have defined the property as shown below:

<requiredProperty key="quarkus_orm_selection">
  <validationRegex><![CDATA[^(hibernate|hibernate-reactive)$]]></validationRegex>
</requiredProperty>

In jshell and in other Java programs, I have verified that the regular expression works properly, but in the archetype when I test using a value like hibernate-ree the archetype proceeds without an error!

I proved out the regex as follows in JShell:

jshell> String[] examples = {"hibernate", "hibernate-reactive", "hibernate-re", "hibernate-ree", "testing", "reactive"}
examples ==> String[6] { "hibernate", "hibernate-reactive", "h ... ", "testing", "reactive" }

jshell> Pattern regex = Pattern.compile("^(hibernate|hibernate-reactive)$")
regex ==> ^(hibernate|hibernate-reactive)$

jshell> Arrays.asList(examples).stream().filter(i -> regex.matcher(i).matches()).forEach(System.out::println)
hibernate
hibernate-reactive

Can anyone suggest what I might be doing wrong?

I am using Maven Archetype Plugin version 3.2.0

1

There are 1 best solutions below

0
Martin Lemnian On

As far as I can tell maven archetypes only validates reg-ex's if you pass in the property in interactive mode.

I created a archetype-post-generate.groovy script (see below)

src/main/resources/META-INF/archetype-post-generate.groovy:

String ormSelector = request.getProperties().get("quarkus_orm_selection")
def pattern = "^(hibernate|hibernate-reactive)\$" // the \$ is important!
final match = ormSelector.matches(pattern)

if (!match) {
    println "[ERROR] ormSelector: $ormSelector is not valid"
    println "[ERROR] please provide an ormSelector that follows this pattern: '$pattern'"
    throw new RuntimeException("OrmSelector: $ormSelector is not valid")
}