Using custom properties in your own maven archetype

时间:2016-04-07 10:29:34

标签: java maven

UPDATED (after reading Passing extra properties to maven archetype:generate):

I created my own archetype. The src/main/resources/archetype-resources/pom.xml:

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>$groupId</groupId>
    <artifactId>$artifactId</artifactId>
    <version>$version</version>
    <packaging>jar</packaging>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>$groupId</groupId>
                <artifactId>${3rdPartyDepsArtifactId}</artifactId>
                <version>${3rdPartyDepsVersion}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

</project>

Want to use my own 3rd-party-deps-artifact and 3rd-party-deps-version parameters. So I added the src/main/resources/META-INF/maven/archetype-metadata.xml file:

<archetype-descriptor name="basic">
    <requiredProperties>
        <requiredProperty key="3rdPartyDepsArtifactId"/>
        <requiredProperty key="3rdPartyDepsVersion"/>
    </requiredProperties>
</archetype-descriptor>

Install archetype into my local repository. And then create artifact based on my custom archetype:

mvn archetype:generate -DarchetypeGroupId=test -DarchetypeArtifactId=test2 -DarchetypeVersion=1.0.0-SNAPSHOT -D3rdPartyDepsArtifactId=3rd-party-deps -D3rdPartyDepsVersion=1.0.0-SNAPSHOT -DgroupId=test3 -DartifactId=my_test -Dversion=1.0.0-SNAPSHOT

In the output I can see that both 3rdPartyDepsVersion and 3rdPartyDepsArtifactId are applied now: Confirm properties configuration:

groupId: ...
artifactId: ...
version: 1.0.0-SNAPSHOT
package: ...
3rdPartyDepsArtifactId: 3rd-party-deps
3rdPartyDepsVersion: 1.0.0-SNAPSHOT

But in the generated project, inside of dependencyManagement these properties are still ignored:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>test3</groupId>
            <artifactId>${3rdPartyDepsArtifactId}</artifactId>
            <version>${3rdPartyDepsVersion}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

How can I force maven to use 3rdPartyDepsArtifactId and 3rdPartyDepsVersion properties?

1 个答案:

答案 0 :(得分:0)

Not sure whether this is possible as you want it but an alternative approach could be to include the property in your settings.xml (within a profile) or pass them within the maven command -D.