模板化Maven原型 [英] Templating a Maven Archetype

查看:72
本文介绍了模板化Maven原型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建自己的Maven原型,这是我使用的项目的通用模板.

I am creating my own maven archetype, which is a common template for projects that i use.

在该模板中,我有许多"exec-maven-plugin"块,实际上每个项目都不同,这意味着在一个项目中,我可能有2个"exec-maven-plugin"块,而在另外一个中,我可能有3个或更多.

In that template i have a number of "exec-maven-plugin" blocks, which actually varies for each project, meaning that in a project i might have 2 "exec-maven-plugin" blocks and in another one i might have 3 or more.

在他使用我创建的原型创建项目时,我希望由用户来驱动.例如,将要求用户提供许多主要类别,并根据他选择输入的主要类别,应创建许多"exec-maven-plugin"块.

I would like that to be driver by the user, at the time he creates a project using the archetype i have created. For example the user will be asked for a number of main classes and according to how many he selects to enter, that many "exec-maven-plugin" blocks should be created.

例如,如果要求用户输入他将要拥有的主要班级,则可以输入: com.domain.MyFirstMain,com.domainMySecondMain 因此,maven pom.xml应该类似于以下内容:

For example if the user is asked for the main classes that he will have he might enter: com.domain.MyFirstMain, com.domainMySecondMain Thus the maven pom.xml should look similar to below:

<profiles>
    <profile>
        <id>Main1</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <configuration>
                        <executable>java</executable>
                        <arguments>
                            <argument>com.domain.MyFirstMain</argument>
                        </arguments>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>Main2</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <configuration>
                        <executable>java</executable>
                        <arguments>
                            <argument>com.domain.MySecondMain</argument>
                        </arguments>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

有人知道我创建maven原型时是否可以实现这一目标,或者唯一的方法就是让用户在pom.xml中添加所需的块?

Does anyone know if i can achieve that when i create a maven archetype or the only way to go is to let user add the required blocks in the pom.xml?

谢谢.

推荐答案

应该可以做您想做的事情. Maven在复制时使用 Apache Velocity 处理原型文件他们到新项目.我已经成功地完成了类似的工作,例如,提示原型用户输入参数"useSomeFeature",并在响应以"Y"或"y"开头的情况下添加插件执行.

It should be possible to do what you want. Maven uses Apache Velocity to process the archetype files when copying them to the new project. I have successfully done something similar by prompting the archetype user for the argument "useSomeFeature" and adding a plugin execution if the response begins with 'Y' or 'y', for example.

我的用例基于布尔回复添加了文本;您的用例需要一个for循环.它看起来像这样.注意,这是未经测试的代码,我将其交给您以使语法完全正确,添加任何所需的错误处理并使之正常工作. :)反正你有主意.

My use case added text based on a Boolean reply; your use case requires a for loop. It will look something like this. Note, this is untested code, I leave it to you to get the syntax exactly right, add any desired error handling, and make it work. :) You have the idea, anyway.

## archetype-resources/pom.xml
## assumes the template variable holding the main class list is mainClassAnswer
#set( $mainClasses = $mainClassAnswer.split(","))

.... basic POM elements here ....

<profiles>
#set ( $loopCount = 0 )
#foreach( $mainClass in $mainClasses )
  #set ( $trimmedMainClass = $mainClass.trim() )
  #set ( $loopCount = $loopCount + 1 )
  <profile>
      <id>Main${loopCount}</id>
      <build>
          <plugins>
              <plugin>
                  <groupId>org.codehaus.mojo</groupId>
                  <artifactId>exec-maven-plugin</artifactId>
                  <configuration>
                      <executable>java</executable>
                      <arguments>
                          <argument>${trimmedMainClass}</argument>
                      </arguments>
                  </configuration>
              </plugin>
          </plugins>
      </build>
  </profile>
#end
</profiles>
.... rest of POM here ....

这篇关于模板化Maven原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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