dokaspar
dokaspar

Reputation: 8624

How to set the Maven artifactId at runtime?

I would like to define the artifactId in a POM at runtime. I am aware from answers to this question that this is bad practise and Maven archetypes should be used instead, but I would really like to know if it is possible at all.

Currently I have a POM with an artifactId like this:

<artifactId>myproject${var}</artifactId>

and I can successfully build the project by setting the variable on the command line:

mvn install -Dvar=justatest

Now is it possible to change this input variable at runtime? For example convert it to uppercase (e.g. with the gmaven-plugin) or similar?

Upvotes: 1

Views: 3756

Answers (1)

Tunaki
Tunaki

Reputation: 137064

You cannot change the artifactId at build-time. It is part of the Maven coordinates (groupId:artifactId:version) so it must be fixed.

All other parameters you could change during the build with the maven-antrun-plugin.

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <id>touppercase</id>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <target>
                    <property name="varUpper" value="${var}"/>
                    <script language="javascript"> <![CDATA[
                        property = project.getProperty("varUpper");
                        project.setProperty("varUpper", property.toUpperCase());
                    ]]> </script>
                </target>
                <exportAntProperties>true</exportAntProperties>
            </configuration>
        </execution>
    </executions>
</plugin>

After this execution, Maven will have a new property called ${varUpper} that is uppercased ${var}. You need to set the correct phase to the snippet above to match your build process.

Upvotes: 2

Related Questions