Templating a Maven Archetype

依然范特西╮ 提交于 2019-12-02 08:12:34

问题


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

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.

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.

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>

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?

Thank you.


回答1:


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.

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


来源:https://stackoverflow.com/questions/23933256/templating-a-maven-archetype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!