4

我正在创建一个 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 中显示与上面发布的完全一样的依赖项片段。它不会取代它。

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

4

2 回答 2

2

我认为简单的方法是使用 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 ...'。

于 2013-02-06T15:46:30.500 回答
1

我发现的最简单的方法是将其添加为默认变量,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>

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

于 2021-06-22T16:45:21.650 回答