I have a maven POM that I'd like to use as a template, producing artifacts with different names depending on what arguments I pass to it. But I can't figure out how to specify the artifactId at runtime.
If I parameterize the <artifactId>
element like this:
<artifact>foo-${bar}</artifact>
maven complains:
[WARNING] 'artifactId' contains an expression but should be a constant.
If I leave out <artifactId>
in the POM and try to specify it on the command line with
mvn -Dproject.artifactId=foo ...
maven complains:
[ERROR] 'artifactId' is missing.
Is there another trick I could use to accomplish this, other than resorting to generating the POM file on-the-fly from a template? [Hmm, maybe I could do that using maven resource filtering...]
The groupId is a parameter indicating the group or individual that created a project, which is often a reversed company domain name. The artifactId is the base package name used in the project, and we use the standard archetype.
Get the pom properties using the properties plugin This will write all properties defined in the properties section of the pom into the version. properties file. But this has its downside. If you want a property to appear in the file, you will have to define it in the properties section.
artifactId is the name of the jar without version. If you created it, then you can choose whatever name you want with lowercase letters and no strange symbols. If it's a third party jar, you have to take the name of the jar as it's distributed.
If I get it right you want to reuse a blueprint maven application and be able to change the artifactId.
This use case can be best accomplished with Maven archetypes. See this to get you started. It's fairly straight forward and its worth learning. You have your normal Maven project and you add variables like ${groupId}
in your blueprint pom. They then get replaced by parameters given by you at the archetype generation:
mvn archetype:generate \
-DarchetypeGroupId=<archetype-groupId> \
-DarchetypeArtifactId=<archetype-artifactId> \
-DarchetypeVersion=<archetype-version> \
-DgroupId=<my.groupid> \
-DartifactId=<my-artifactId>
There are also a lot of archetypes created by people on GitHub where you can learn more about structuring and filtering in Maven archetypes For example.
Alternatively you can try to set up the Maven filtering without using the archetype system but I have no experience with that. I don't think you can run a project without it having a valid artifactId, some generation must happen before that (like in generating from an archetype) but I'm not sure.
It's possible to set the artifactId at runtime (I use the following solution for a monorepository containing multiple applications). Your project pom.xml should include:
<project>
<artifactId>${artifactId}</artifactId>
...
<properties>
<artifactId>defaultArtifactId</artifactId>
</properties>
...
To set the artifactId at runtime use the mvn
command with the -DartifactId=foo
option.
Example : mvn verify -DartifactId=foo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With