Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maven archetype: generate a default value for requiredProperty from a value of an existing property

I want to set a default value for a requiredProperty in archetype-metadata.xml such that it is based on another property passed from command line. Say

<requiredProperty key="customProperty">
        <defaultValue>${artifactId.toUpperCase()}</defaultValue>
</requiredProperty>

However, when I use the resulting archetype to generate a new project, it is not the project's artifactId that gets uppercased, but rather the archetype's artifactId. Whereas when I change it to

<requiredProperty key="customProperty">
        <defaultValue>${artifactId}</defaultValue>
</requiredProperty>

I get the project's artifactId as one would expect.

Is there a way to assign a default value for a custom property based on the value of another property?

Note: this happens only in interactive mode.

How to reproduce:

mvn -B archetype:generate -DartifactId=archet -DgroupId=com.example -Dversion=1.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-archetype

For some reason, archetype.xml gets generated. It is my understanding, it is an old format. Replaced it with archetype-metadata.xml (minimized for the sake of an example):

<?xml version="1.0" encoding="UTF-8"?>
<archetype-descriptor name="basic">

    <requiredProperties>
        <requiredProperty key="customPropertyUppercased">
            <defaultValue>${artifactId.toUpperCase()}</defaultValue>
        </requiredProperty>
        <requiredProperty key="customProperty">
            <defaultValue>${artifactId}</defaultValue>
        </requiredProperty>

        <!--JUnit version to use in generated project-->
        <requiredProperty key="junit-version">
            <defaultValue>4.12</defaultValue>
        </requiredProperty>
    </requiredProperties>

    <fileSets>
        <fileSet filtered="true" packaged="true">
            <directory>src/main/java</directory>
        </fileSet>
        <fileSet filtered="true" packaged="true">
            <directory>src/test/java</directory>
        </fileSet>
    </fileSets>

</archetype-descriptor>

A template at ./src/main/resources/archetype-resources/src/main/java/App.java:

package ${groupId}.${artifactId};

/**

${customPropertyUppercased}

${customProperty}

*/
public class App
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}

Generating a new project using the resulting archetype

mvn archetype:generate -DgroupId=com.example -DartifactId=stacktest -DarchetypeArtifactId=archet -DarchetypeGroupId=com.example -DarchetypeCatalog=local

with all properties set to default produces stacktest/src/main/java/com/example/App.java:

package com.example.stacktest;

/**

ARCHET

stacktest

*/
public class App
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}

So, the customPropertyUppercased is based on the archetype's artifactId, while customProperty is based on the project's artifactId.

like image 763
badbishop Avatar asked Oct 19 '16 15:10

badbishop


People also ask

What is the use of MVN archetype generate?

An archetype is made up of: an archetype descriptor ( archetype-metadata. xml in directory: src/main/resources/META-INF/maven/ ). It lists all the files that will be contained in the archetype and categorizes them so they can be processed correctly by the archetype generation mechanism.

How do you know the which archetype an existing Maven project is built on?

In the artifact's root directory, there is a pom. xml file of the archetype. You can see the packaging maven-archetype . You can customize build of your archetype here as with any other pom.

What is the basic Maven archetype?

maven-archetype-simple is an archetype which generates a simple Maven project: project. |-- pom. xml.

What is archetype in Maven for spring boot?

A Maven archetype is an abstraction of a kind of a project that can be instantiated into a concrete customized Maven project. In short, it's a template project template from which other projects are created.


1 Answers

This is a bug in the archetype plugin, and is documented here: https://issues.apache.org/jira/browse/ARCHETYPE-490

like image 194
Chris Snyder Avatar answered Sep 18 '22 14:09

Chris Snyder