6

假设我有一个原型并从中生成一个项目。但是我想通过命令行传递占位符的值来解析我在生成时间之后生成的项目的属性文件中的占位符。

例如具有以下命令行:

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

假设原型包含具有以下内容的 app.properties(作为正在生成的项目的一部分):

问候=${你好}

是否可以将 ${hello} 替换为“Hello!” 在由于 mvn archetype:create 命令生成项目之后?

4

3 回答 3

6

是的,这是可能的。来自Maven 原型的高级使用指南:

如果用户想进一步自定义生成的项目,可以在 src/main/resources/META-INF/ 中添加一个名为 archetype-post-generate.groovy 的 groovy 脚本。该脚本将最终在生成的原型的 META-INF 文件夹中,并将在从该原型创建项目时执行。这个 groovy 脚本可以访问 ArchetypeGenerationRequest 对象,以及所有 System.getProperties() 和用户指定的所有原型生成属性。

于 2017-03-02T19:34:47.160 回答
0

您可以按照以下格式在原型中定义其他属性: https ://maven.apache.org/archetype/maven-archetype-plugin/specification/archetype-metadata.html

例如:

定义文件: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>

在这里,您会看到它定义了额外的必需属性,因此必须在对话框中强制提供它们,其中:

  • 某些属性可能没有价值 - 请参阅 metamodelUrl
  • 某些属性可能具有默认值——作为静态文本——或引用先前定义的标准属性的值:groupId、artifactId、version
  • 一些属性可能会覆盖标准属性的值 - “包”属性。在这里重新定义。

请注意:

于 2017-08-16T19:30:49.467 回答
-1

不确定我理解正确。对于项目创建后的后期处理,您可以使用参数 -Dgoals 并调用您的自定义插件。

不确定您的要求,但为什么在项目生成过程中不能这样做?

于 2013-12-03T07:29:22.857 回答