Templating a Maven Archetype

后端 未结 1 1289
無奈伤痛
無奈伤痛 2021-01-23 09:21

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 actuall

相关标签:
1条回答
  • 2021-01-23 09:55

    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 ....
    
    0 讨论(0)
提交回复
热议问题