【问题标题】:Maven archetype creation : prototype pomMaven原型创建:原型pom
【发布时间】:2012-04-19 00:50:36
【问题描述】:

我正在创建一个 Maven 原型。在这个我有一个原型项目,当用户调用以下命令时,它会为用户创建:

mvn archetype:generate -DarchetypeGroupId=xxx -DarchetypeArtifactId=archtype-yyyy -DarchetypeVersion=1.1.0-S5-SNAPSHOT -DgroupId=zzz -DartifactId=proj11

在原型 pom 中,我想使用我在上述命令中指定的“archetypeVersion”属性。像这样:

<dependencies>
    <dependency>
        <groupId>mmmm</groupId>
        <artifactId>nte</artifactId>
        <version>${archetypeVersion}</version>
    </dependency>

这对我不起作用。创建项目时,它仍然在生成的 pom 中显示依赖项 sn-p,与上面发布的完全一样。它不会取代它。

这可能吗? maven允许这样做吗? 如果是,我该怎么做?

【问题讨论】:

    标签: maven maven-archetype


    【解决方案1】:

    我认为简单的方法是使用 maven-replacer-plugin。您必须将下一部分添加到原型 /pom.xml:

    <build>
        ...
        <plugins>
            <plugin>
                <groupId>com.google.code.maven-replacer-plugin</groupId>
                <artifactId>replacer</artifactId>
                <version>1.5.2</version>
                <executions>
                    <execution>
                        <phase>prepare-package</phase>
                        <goals><goal>replace</goal></goals> 
                    </execution>
                </executions>
                <configuration>
                    <file>target/classes/archetype-resources/pom.xml</file>
                    <replacements>
                        <replacement>
                            <token>\$\{archetypeVersion\}</token>
                            <value>${version}</value>
                        </replacement> 
                    </replacements>
                </configuration>
            </plugin>
        </plugins>
        ...
    <build>
    

    即此代码将“${archetypeVersion}”子字符串替换为当前版本的原型。您的“/src/main/resources/archetype-resources/pom.xml”包含下一个依赖项:

    <dependency>
        <groupId>xxxx</groupId>
        <artifactId>yyyy</artifactId>
        <version>${archetypeVersion}</version>
    </dependency>
    

    执行“mvn install”命令后,生成的文件“/target/classes/archetype-resources/pom.xml”将包含原型版本号。现在你已经安装了原型并且可以使用它:'mvn archetype:generate ...'。

    【讨论】:

      【解决方案2】:

      我发现的最简单的方法是将其作为默认变量添加到您的 META-INF/maven/archetype-metadata.xml 中,如下所示:

      <archetype-descriptor
          xmlns="https://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.1.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="https://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.1.0 http://maven.apache.org/xsd/archetype-descriptor-1.1.0.xsd"
          name="archetypeVersionExample">
      
          <requiredProperties>
              ...
              <requiredProperty key="archetypeVersion">
                  <defaultValue>${version}</defaultValue>
              </requiredProperty>
          </requiredProperties>
          ...
          
      </archetype-descriptor>
      

      不需要额外的插件或用户输入。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-09
        • 2015-07-14
        • 2011-06-28
        • 1970-01-01
        • 1970-01-01
        • 2011-05-09
        • 2017-04-18
        • 2023-03-17
        相关资源
        最近更新 更多