如何在Maven原型中提供自定义逻辑?

jit*_*h74 12 velocity archetypes maven

我有兴趣创建一个Maven原型,我认为我已经掌握了大部分基础知识.但是,我坚持的一件事是,有时我想使用自定义逻辑来填充模板.例如,如果有人生成我的原型并将artifactId指定为hello-world,我想生成一个名为HelloWorld的类,它只打印出"Hello World!".到控制台.如果另一个人使用artifactId = howdy-there生成它,那么genned类将是HowdyThere,它将打印出"Howdy There!".

我知道,Maven的原型机制在幕后使用Velocity模板引擎,所以我读了这篇关于创建自定义指令的文章.这似乎是我想要的,所以我创建了一个名为HyphenatedToCamelCaseDirective的类,它扩展了org.apache.velocity.runtime.directive.Directive.在该类中,我的getName()实现返回"hyphenatedCamelCase".在我的archetype-metadata.xml文件中,我有以下内容......

<requiredProperties>
    <requiredProperty key="userdirective">
        <defaultValue>com.jlarge.HyphenatedToCamelCaseDirective</defaultValue>
    </requiredProperty>
</requiredProperties>
Run Code Online (Sandbox Code Playgroud)

我的模板类看起来像这样......

package ${package};

public class #hyphenatedToCamelCase('$artifactId') {

    // userdirective = $userdirective
    public static void main(String[] args) {
        System.out.println("#hyphenatedToCamelCase('$artifactId')"));
    }
} 
Run Code Online (Sandbox Code Playgroud)

在我安装我的原型后然后做一个原型:通过指定artifactId = howdy-there和groupId = f1.f2生成,结果类看起来像这样......

package f1.f2;

public class #hyphenatedToCamelCase('howdy-there') {

    // userdirective = com.jlarge.HyphenatedToCamelCaseDirective    
    public static void main(String[] args) {
        System.out.println("#hyphenatedToCamelCase('howdy-there')"));
    }
}
Run Code Online (Sandbox Code Playgroud)

结果表明,尽管userdirective按照我的预期方式设置,但它并没有像我希望的那样评估#hyphenatedToCamelCase指令.在指令类中,我有一个render方法将消息记录到System.out,但是该消息没有显示在控制台中,因此这让我相信该方法永远不会在archetype:generate期间执行.

我在这里错过了一些简单的东西,还是这种方法不是要走的路?

6to*_*ton 5

archetype-metatadata xml 的必需属性部分用于将附加属性传递给速度上下文,它不是传递速度引擎配置。因此,设置名为 userDirective 的属性只会使变量 $userDirective 可用,而不会向速度引擎添加自定义指令。

如果您查看源代码,maven-archetype 插件使用的速度引擎不依赖于任何外部属性源进行配置。生成项目的代码依赖于VelocityComponent的自动装配(由 plexus 容器)实现。

这是初始化速度引擎的代码:

public void initialize()
    throws InitializationException
{
    engine = new VelocityEngine();

    // avoid "unable to find resource 'VM_global_library.vm' in any resource loader."
    engine.setProperty( "velocimacro.library", "" );

    engine.setProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, this );

    if ( properties != null )
    {
        for ( Enumeration e = properties.propertyNames(); e.hasMoreElements(); )
        {
            String key = e.nextElement().toString();

            String value = properties.getProperty( key );

            engine.setProperty( key, value );

            getLogger().debug( "Setting property: " + key + " => '" + value + "'." );
        }
    }

    try
    {
        engine.init();
    }
    catch ( Exception e )
    {
        throw new InitializationException( "Cannot start the velocity engine: ", e );
    }
}
Run Code Online (Sandbox Code Playgroud)

有一种添加自定义指令的巧妙方法。您在上面看到的属性是从plexus-velocity-1.1.8.jar 中的Components.xml文件中读取的。因此,打开此文件并添加您的配置属性

<component-set>
  <components>
    <component>
      <role>org.codehaus.plexus.velocity.VelocityComponent</role>
      <role-hint>default</role-hint>
      <implementation>org.codehaus.plexus.velocity.DefaultVelocityComponent</implementation>
      <configuration>
        <properties>
          <property>
            <name>resource.loader</name>
            <value>classpath,site</value>
          </property>
          ...
          <property>
            <name>userdirective</name>
            <value>com.jlarge.HyphenatedToCamelCaseDirective</value>
          </property>
        </properties>
      </configuration>
    </component>
  </components>
</component-set>
Run Code Online (Sandbox Code Playgroud)

接下来将您的自定义指令类文件添加到此 jar 并运行 archetype:generate。

正如你所看到的,这是非常脆弱的,你需要找到一种方法来分发这个被黑的 plexus-velocity jar。根据您计划使用此原型的目的,可能值得付出努力。