【问题标题】:Templating a Maven Archetype模板化 Maven 原型
【发布时间】:2014-05-29 12:23:40
【问题描述】:

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

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

当他使用我创建的原型创建项目时,我希望它成为用户的驱动程序。例如,用户将被要求输入多个主类,并根据他选择输入的数量,创建许多“exec-maven-plugin”块。

例如,如果用户被要求提供他将拥有的主要课程,他可能会输入: com.domain.MyFirstMain, com.domainMySecondMain 因此 maven pom.xml 应该如下所示:

<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 中添加所需的块?

谢谢。

【问题讨论】:

    标签: java maven maven-plugin maven-archetype


    【解决方案1】:

    应该可以做你想做的事。 Maven 在将原型文件复制到新项目时使用Apache Velocity 处理它们。例如,我通过提示原型用户输入参数“useSomeFeature”并在响应以“Y”或“y”开头时添加插件执行来成功地完成了类似的操作。

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

    ## 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 ....
    

    【讨论】:

    • 感谢您的回答。我还没有尝试过,但听起来很有希望。我马上试试。
    • 谢谢,它按照您的描述工作。它完全符合我的需要。
    猜你喜欢
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    相关资源
    最近更新 更多