Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can I specify a maven project.artifactId at runtime?

Tags:

maven

maven-3

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...]

like image 916
Andy Dennie Avatar asked Jun 08 '12 19:06

Andy Dennie


People also ask

What is the difference between groupId and an artifactId in Maven?

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.

How do you get POM properties at runtime?

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.

What is artifactId in Maven project?

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.


2 Answers

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.

like image 122
GallifreyanCode Avatar answered Oct 17 '22 20:10

GallifreyanCode


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

like image 42
Nicolas Forney Avatar answered Oct 17 '22 20:10

Nicolas Forney