如何在maven-archetype中将变量扩展到嵌套目录 [英] How to expand variable into nested directory in maven-archetype

查看:639
本文介绍了如何在maven-archetype中将变量扩展到嵌套目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我已经将groupId设置为com.example,并且将artifactId设置为fancy.project,现在我想创建一个原型,以便在创建时将其扩展为以下结构:

Suppose I have set groupId to com.example, and artifactId to fancy.project, and now I want to create a archetype, such that when created, expands into the following structure:

|--src
  |--main
    |--com
      |--example
        |-fancy
          |-project
            |-App.java

也就是说,我想知道如何将变量扩展到嵌套目录中.

That is, I wonder how to expand a variable into a nested directory.

我知道双下划线包装的变量将替换为文件/目录名称,但是我只能使用__groupId__来获取以下内容.

I understand that the dual-underscore-wrapped variables will be substituted in file/directory names, but I can only get the following with __groupId__.

|--src
  |--main
    |--com.example
      |-fancy.project
        |-App.java

推荐答案

作为原型输入,您可以指定package选项(如果需要,它会跟在您的输入,groupId和artifactId串联之后,即使它是并非总是如此,因此可以提供更大的灵活性).

As input to the archetype you can specify the package option (which would then follow your input, the groupId and artifactId concatenation if you want, even though it is not always the case and hence provide even more flexibility).

然后,在原型中,您可以使用packageInPathFormat选项(自原型 2.2 起可用),该选项会将任何点.替换为斜线\,并将其转换为路径进入生成的项目.

Then, in your archetype you can use the packageInPathFormat option (available since archetype 2.2) which would replace any dot . into slash \ and as such transforming it to a path into the generated project.

但是,该选项即使受到支持也未得到正式记录(可惜),并且在这种情况下可以正常工作.

However, the option is not officially documented (pity) even though supported and works fine for such a scenario.

查看代码DefaultFilesetArchetypeGenerator及其getPackageInPathFormat提供了相关内容从package选项转换为路径,而

Looking at the code, the DefaultFilesetArchetypeGenerator and its getPackageInPathFormat provide the concerned transformation from the package option to a path, while the org.apache.maven.archetype.common.Constants.PACKAGE_IN_PATH_FORMAT is the official entry point for this option.

一些使用此选项的外部指针:

Some externals pointers on the usage of this option:

  • http://geekofficedog.blogspot.be/2013/08/creating-maven-archetypes-tutorial.html
  • http://www.theotherian.com/2012/05/maven-archetypes-part-2-how-do-i-create.html

进一步说明:

  • 例如,您可以在src/main/java下拥有__packageInPathFormat__文件夹
  • 然后将__packageInPathFormat替换为package选项,将点转换为斜线
  • package选项的默认值为groupId,因此,如果未指定,则对于值为com.samplegroupId,路径将为com/sample
  • 因此,您可以在调用时通过-Dpackage=your.package重复-DgroupId-DartifactId的值(虽然有点冗长和容易出错)来指定所需的包,但最终结果实际上将是您期望的(转换为正确的路径).
  • 您可以通过archetype-metada.xml文件指定新的默认值,如

  • You can have the __packageInPathFormat__ folder under your src/main/java, for example
  • The __packageInPathFormat would then be replaced by the package option transforming dots into slashes
  • The package option has a default value to groupId, so if you don't specify it, for a groupId with value com.sample, the path would be com/sample
  • You can hence specify at invocation time the package desired via -Dpackage=your.package repeating the values for -DgroupId and -DartifactId (a bit verbose and error prone though), the final result will actually be what you expected (transformed to correct path).
  • You can specify new default values via a archetype-metada.xml file, as specified in the official documentation, via the requiredProperties section, you could have something like:

<requiredProperties>
    <requiredProperty key="package">
        <defaultValue>__groupId__.__artifactId__</defaultValue>
    </requiredProperty>
</requiredProperties>

但是,生成的路径将是com.sample/artifactid而不是com/sample/artifactid.因此,由于处理工作流程会在将占位符转换为路径(可惜!)之后替换占位符,因此无法正常工作.
(注意:它将转换我们提供的点作为配置值,但不会将点转换为替换的占位符.)

However, the generated path would then be com.sample/artifactid rather than com/sample/artifactid. Hence it would not work as expected due to the processing workflow which would replace the placeholders after transforming it to a path (pity!).
(Note: it would transform the dot we provided as configured value, but would then not transform dots into the replaced placeholders).

通过快速代码分析,似乎generateArchetype方法中的DefaultFilesetArchetypeGenerator类准备上下文的时间过早(在其prepareVelocityContext方法中,将packageInPathFormat转换并添加到上下文中) ,然后将上下文传递给processArchetypeTemplate*方法,这些方法最终将调用Velocity引擎(然后将替换占位符).我不是Velocity专家,因此我可能会错过一些胶水,但是观察到的行为和代码工作流程似乎得出了这个结论.

As of a quick code analysis, seems like the DefaultFilesetArchetypeGenerator class in its generateArchetype method is preparing the context too early (in its prepareVelocityContext method, where the packageInPathFormat is transformed and added to the context), then the context is passed to processArchetypeTemplate* methods which would eventually invoke the Velocity engine (which is going to replace placeholders then). I am not a Velocity expert though, hence I may miss some glue, but the observed behavior and the code workflow seem to lead to this conclusion.

这篇关于如何在maven-archetype中将变量扩展到嵌套目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆