将属性添加到 Maven 原型

JF *_*ier 1 java maven maven-archetype

我有一个 Maven 原型项目,想要进行一些进一步的配置。我试过:

  1. ${servicelocatorhost}$${servicelocatorport}放入我的.tomcatplugin文件中。
  2. 添加.tomcatplugin到原型元数据文件中的过滤文件。
  3. 添加

    <requiredProperties>
      <requiredProperty key="servicelocatorhost">
        <defaultValue>localhost</defaultValue>
      </requiredProperty>
      <requiredProperty key="servicelocatorport">
        <defaultValue>2809</defaultValue>
      </requiredProperty>
    </requiredProperties>
    
    Run Code Online (Sandbox Code Playgroud)

到原型元数据文件。当我构建这个时,我得到

Archetype IT 'basic' failed: Missing required properties in archetype.properties: servicelocatorhost, servicelocatorport
Run Code Online (Sandbox Code Playgroud)

到目前为止,我认为该archetype.properties文件仅用于create-from-project但不用于构建原型。我应该将此类文件中的属性和默认值信息加倍吗?

我究竟需要什么才能使这项工作成功?

use*_*849 5

创建原型时,还会为其创建集成测试。运行集成测试(使用 maven-invoker-plugin)时将使用 archetype.properties 文件中的值。

举个例子:我有一个过滤 POM 的简单原型。除了 GAV 坐标之外,我还希望原型用户提供其 Subversion 存储库的名称和应用程序根目录,以便我可以<scm>正确填充该块。所以我在 archetype-metadata.xml 文件中创建了变量${scmRepo}${applicationRootDir}、 和:${artifactId}

<requiredProperties>
    <requiredProperty key="svnRepo" />
    <requiredProperty key="applicationRootDir" />
    <requiredProperty key="artifactId">
    ....
</requiredProperties>
Run Code Online (Sandbox Code Playgroud)

然后在 POM ( src/main/resouces/archetype-resources/pom.xml) 中使用它们:

<scm>
  <connection>scm:svn:https://host.company.com/svn/${svnRepo}/trunk/${applicationRootDir}/${artifactId}</connection>
</scm>
Run Code Online (Sandbox Code Playgroud)

最后一步是在 archetype.properties 文件中提供默认值。仅当您作为开发人员构建原型时才使用它,而不是当您的社区使用原型创建项目时使用。它可以让您确保原型符合您的要求。所以在我的示例中 archetype.properties 文件包含

svnRepo=maven
applicationRootDir=basic
artifactId=test
Run Code Online (Sandbox Code Playgroud)

在同一目录中,我有一个 verify.groovy 文件,其中包含用于验证原型是否正常工作的代码:

def xmlFile = new File(basedir, "pomFileDir/pom.xml")
assert xmlFile.exists()

def project = new XmlSlurper().parse(xmlFile)
....
assert "scm:svn:https://host.company.com/svn/maven/trunk/basic/test" == project.scm.connection.text()
Run Code Online (Sandbox Code Playgroud)

我从这篇博文、这个原型文档以及我自己使用 Maven 调用程序插件的知识中学到了这一点。