Is there a way to post-process project generated f

2020-03-24 09:20发布

Say I have an archetype and I generate a project from it. But I would like to resolve placeholders in a property file of the project I generated on after generation time by passing the value for placeholder through command line.

For example having the following command line:

mvn archetype:create -DarchetypeGroupId=... -DarchetypeArtifactId=... -DarchetypeVersion=1.0 -DgroupId=... -DartifactId=my-project -Dversion=1.0-SNAPSHOT -Dhello=Hello!

say the archetype contains app.properties (as part of project which is being generated) with the following content:

greeting=${hello}

Is it possible to replace ${hello} with "Hello!" right after project has been generated as a result of mvn archetype:create command?

3条回答
The star\"
2楼-- · 2020-03-24 09:36

You could define additional properties in the archetype, following the format: https://maven.apache.org/archetype/maven-archetype-plugin/specification/archetype-metadata.html

For example:

define the file: src\main\resources\META-INF\maven\archetype-metadata.xml

<archetype-descriptor 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0 http://maven.apache.org/xsd/archetype-descriptor-1.0.0.xsd" 
  name="modelant.metamodel.api">

<requiredProperties>
  <requiredProperty key="package"><defaultValue>${groupId}.${artifactId}</defaultValue></requiredProperty>

  <requiredProperty key="parentGroupId"><defaultValue>${groupId}</defaultValue></requiredProperty>
  <requiredProperty key="parentArtifactId"><defaultValue>${artifactId}</defaultValue></requiredProperty>
  <requiredProperty key="parentVersion"><defaultValue>${version}</defaultValue></requiredProperty>

  <requiredProperty key="metamodelUrl"/>
 </requiredProperties>
</archetype-descriptor>

Here you see that it defines additional required properties, so they have to be mandatorily provided within the dialog, where:

  • some properties may have no value - see metamodelUrl
  • some properties may have default values either -- as static text -- or referring the values of the previously defined standard properties: groupId, artifactId, version
  • some poperties may override the values of the standard properties - the "package" property. Here it is redefined.

Please note:

查看更多
迷人小祖宗
3楼-- · 2020-03-24 09:42

Not sure I understood correctly. For post processing after project creation you could use the param -Dgoals and invoke your custom plugin.

Am not sure about your requirement, but why cant you do the same during the project generation itself ?

查看更多
一夜七次
4楼-- · 2020-03-24 09:52

Yes this is possible. From the advanced usage guide for maven archetypes:

If the user wants to customize the generated project even further, a groovy script named archetype-post-generate.groovy can be added in src/main/resources/META-INF/. This script will end up in the generated archetype's META-INF folder and will be executed upon creating a project from this archetype. This groovy script has access to the ArchetypeGenerationRequest object, as well as all the System.getProperties() and all the archetype generation properties the user has specified.

查看更多
登录 后发表回答