maven-archetype icon indicating copy to clipboard operation
maven-archetype copied to clipboard

[ARCHETYPE-613] Different gererating result with user defined parameters

Open jira-importer opened this issue 5 years ago • 1 comments

Liu Shilong opened ARCHETYPE-613 and commented

Happend in two different maven version 3.6.1 and 3.6.3,

I defined my archetype-metadata.xml as following:

//代码占位符
<requiredProperties>
    <requiredProperty key="groupId"/>
    <requiredProperty key="artifactId"/>
    <requiredProperty key="version">
        <defaultValue>1.0.0-SNAPSHOT</defaultValue>
    </requiredProperty>
    <requiredProperty key="package"/>
    <requiredProperty key="projectName"/>
    <requiredProperty key="xxxx1"/> //user defined parameter
    <requiredProperty key="xxxx2"> // user defined parameter
   <defaultValue>${package.getClass().forName("org.apache.velocity.util.StringUtils").nullTrim("")}</defaultValue> //default is null;
    </requiredProperty>
    

 and following are some others parameter tansformed by abover parameters

//代码占位符
<requiredProperty key="xxxx3" >
    <defaultValue>${xxxx1.toUpperCase()}</defaultValue>
</requiredProperty>
<requiredProperty key="xxxx4" >
    <defaultValue>${xxxx1.toLowerCase().substring(0,1).toUpperCase()}${xxxx1.toLowerCase().substring(1)}</defaultValue>
</requiredProperty>

<requiredProperty key="xxxx5" >
    <defaultValue>${package.getClass().forName("org.apache.velocity.util.StringUtils").nullTrim("$xxxx2").toUpperCase()}</defaultValue>
</requiredProperty>
<requiredProperty key="xxxx6" >
    <defaultValue>${package.getClass().forName("org.apache.velocity.util.StringUtils").firstLetterCaps("${xxxx2}_").replaceAll("_","")}</defaultValue>
</requiredProperty>
<requiredProperty key="xxxx7" >
    <defaultValue>${xxxx4}${xxxx6}</defaultValue>
</requiredProperty>
....

when I use command "mvn archetype : generate ...." in maven 3.6.3, i got the right generated project ,and all the parameters are as my expected including parameters which need to be evaluated. But in maven 3.6.1(In fact , I'm not sure whether the version cause the difference"), it's different, some parameters are not evaluatedd correctly like following:

//代码占位符
...
Parameter: xxxx3, Value: ${xxxx1.toUpperCase()}
[INFO] Parameter: xxxxx6, Value: _${xxxx}_
[INFO] Parameter: xxxxx,  Value: _xxxx_
...

as you see, some parameters are evaluated, but some are not.  In fact , I found this happened also in 'mvn command' and 'maven archetype generating process in IDEA'. And I found following source code in maven-archetype plugin:

//代码占位符
private Context prepareVelocityContext(ArchetypeGenerationRequest request) {
    Context context = new VelocityContext();
    context.put(Constants.GROUP_ID, request.getGroupId());
    context.put(Constants.ARTIFACT_ID, request.getArtifactId());
    context.put(Constants.VERSION, request.getVersion());
    context.put(Constants.PACKAGE, request.getPackage());
    final String packageInPathFormat = getPackageInPathFormat(request.getPackage());
    context.put(Constants.PACKAGE_IN_PATH_FORMAT, packageInPathFormat);

    if (getLogger().isInfoEnabled()) {
        getLogger().info("----------------------------------------------------------------------------");

        getLogger().info(
                "Using following parameters for creating project from Archetype: " + request
                        .getArchetypeArtifactId()
                        + ":" + request.getArchetypeVersion());

        getLogger().info("----------------------------------------------------------------------------");
        getLogger().info("Parameter: " + Constants.GROUP_ID + ", Value: " + request.getGroupId());
        getLogger().info("Parameter: " + Constants.ARTIFACT_ID + ", Value: " + request.getArtifactId());
        getLogger().info("Parameter: " + Constants.VERSION + ", Value: " + request.getVersion());
        getLogger().info("Parameter: " + Constants.PACKAGE + ", Value: " + request.getPackage());
        getLogger().info("Parameter: " + Constants.PACKAGE_IN_PATH_FORMAT + ", Value: " + packageInPathFormat);
    }

    for (Iterator<?> iterator = request.getProperties().keySet().iterator(); iterator.hasNext(); ) { // here,the result of request.getProperties() is a hashtable
        String key = (String) iterator.next();

        String value = request.getProperties().getProperty(key);

        if (maybeVelocityExpression(value)) {
            value = evaluateExpression(context, key, value);
        }

        context.put(key, value);

        if (getLogger().isInfoEnabled()) {
            getLogger().info("Parameter: " + key + ", Value: " + value);
        }
    }
    return context;
}

As above comment, the result of request.getProperties() is a HashTable, the fact is that the parameter order is not what we input but hash value, so it cause that some parameters are not evaluated because some parameters are relied on others.

But the question is "how difference happens?" . In fact ,once the parameter are determined, the result will be determined, why it will be different in some case?

IDEA use the java command to start the process of generating like:

//代码占位符
java -Dxxx=xxx org.apache.maven.plugins:maven-archetype-plugin:RELEASE:generate

also, some parameters are not evaluated correctly.


Affects: 3.2.0

jira-importer avatar Oct 30 '20 02:10 jira-importer

Liu Shilong commented

I solve the problem, In fact ,it occurs not because of the maven version but the jdk version, it needs jdk11 or 14 rather than jdk8. But it is not because of hashtable, I did not find the factor that make it occur. So anyone can find the reason?

jira-importer avatar Oct 30 '20 05:10 jira-importer