Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven Archetype: Validate artifactId or groupId

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).

like image 733
J Fabian Meier Avatar asked Sep 08 '17 13:09

J Fabian Meier


People also ask

What is difference between groupId and artifactId in Maven?

The main difference between groupId and artifactId in Maven is that the groupId specifies the id of the project group while the artifactId specifies the id of the project. Show activity on this post. groupId uniquely identifies your project across all projects. artifactId is the name of the jar without version.

What should be the artifactId in Maven?

artifactId is the name of the jar without version. If you created it, then you can choose whatever name you want with lowercase letters and no strange symbols. If it's a third party jar, you have to take the name of the jar as it's distributed.

What should be the groupId and artifactId in Maven?

Maven uses a set of identifiers, also called coordinates, to uniquely identify a project and specify how the project artifact should be packaged: groupId – a unique base name of the company or group that created the project. artifactId – a unique name of the project. version – a version of the project.


1 Answers

This:

  <requiredProperties>
    <requiredProperty key=.. >
      <defaultValue/>
      <validationRegex/>
    </requiredProperty>
  </requiredProperties>

... 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 requiredProperties but with this caveat: validationRegex only works for inputs supplied at the command line, so providing a defaultValue or defining a value via a command line parameter (-DgroupId=..., -DartifactId=... ) side steps validation. Here's a concrete example, given the following requiredProperties in archetype-descriptor.xml:

<requiredProperties>
  <requiredProperty key="artifactId">
    <validationRegex>^[a-z]*$</validationRegex>
  </requiredProperty>
  <requiredProperty key="groupId">
    <defaultValue>COM.XYZ.PQR</defaultValue>
    <validationRegex>^[a-z]*$</validationRegex>
  </requiredProperty>
</requiredProperties>

The following command: mvn archetype:generate -DarchetypeGroupId=... -DarchetypeArtifactId=... -DarchetypeVersion=... -DgroupId=com.foo.bar will result in com.foo.bar being used for groupId and the user will be prompted to supply an artifactId like so:

Define value for property 'username' (should match expression '^[a-z]*$'): Whatever

Value does not match the expression, please try again: whatever

Define value for property...

So far so good (sort of).

But the following command mvn archetype:generate -DarchetypeGroupId=... -DarchetypeArtifactId=... -DarchetypeVersion=... -DartifactId=whatever will result in COM.XYZ.PQR being used for groupId even though that does not conform to the validationRegex.

Similarly; the following command mvn archetype:generate -DarchetypeGroupId=... -DarchetypeArtifactId=... -DarchetypeVersion=... -DartifactId=WHATEVER will result in COM.XYZ.PQR being used for groupId and WHATEVER being used for artifactId even though those values do not conform to the validationRegex.

So, in summary: the validationRegex works 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 validationRegex you 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:

This rule can enforce that a declared property is set and optionally evaluate it against a regular expression.

Here's an example:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
      <execution>
        <id>enforce-property</id>
        <goals>
          <goal>enforce</goal>
        </goals>
        <configuration>
          <rules>
            <requireProperty>
              <property>project.artifactId</property>
              <message>"Project artifactId must match ...some naming convention..."</message>
              <regex>...naming convention regex...</regex>
              <regexMessage>"Project artifactId must ..."</regexMessage>
            </requireProperty>
          </rules>
          <fail>true</fail>
        </configuration>
      </execution>
    </executions>
  </plugin>
like image 107
glytching Avatar answered Sep 28 '22 16:09

glytching