简体   繁体   中英

How to create multiple files using one template in Maven Custom Archetype (Apache Velocity)?

What I want to achieve is use the code below from archetype-metadata.xml in order to create as many files as the items below, from one template .

<requiredProperty key="item_1">
    <defaultValue>item_1</defaultValue>
</requiredProperty>
.
.
.
<requiredProperty key="item_n">
    <defaultValue>item_n</defaultValue>
</requiredProperty>

I quit using Custom Maven Archetypes because it can't create multiple files from one template. Instead, I used the Velocity Engine and I was able to do all what I want with complete freedom.

The metadata file is part of the archetype, which you as the archetype developer provide, so your users won't be editing that file. If you need to modify file content, you can use Apache Velocity. Consider adding one property with comma-delimited values (or other appropriate char delimiter), where the default is a single value:

<requiredProperty key="itemList">
    <defaultValue>item_1</defaultValue>
</requiredProperty>

Then, use Apache Velocity to loop through the provided values in itemList, adding the appropriate code blocks. See this answer for the idea.

I am not sure if it's possible to use this technique to create separate files, but it's where I'd start exploring.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM