Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven property referencing a list

Tags:

maven

I am working with a (custom, internally developed, details do not matter here) Maven plugin which takes a list of names, like this:

<configuration>
    <names>
        <name>Alice</name>
        <name>Bob</name>
        <name>Joe</name>
        <name>Sally</name>
    </names>
    ...

The list is hard-coded in the POM file.

I'd like to have something a little more dynamic, so that I can use a user-defined variable to specify a single name. I can do it this way:

mvn clean package -Dname=Bob

<configuration>
    <names>
        <name>${name}</name>
    </names>

The problem is that the only way I've found so far to do this is to have two profiles with duplicate plugin configurations: one with the pre-defined list, and one with the single user-defined value:

<profiles>
  <profile>
    <id>default</id>
        ...
        <configuration>
          <names>
              <name>Alice</name>
              <name>Bob</name>
              <name>Joe</name>
              <name>Sally</name>
          </names>
        ...
  </profile>

  <profile>
    <id>single-value</id>
    <activation>
      <property>
        <name>name</name>
      </property> 
    </activation>
    ...
      <configuration>
        <names>
          <name>${name}</name>
        </names>
    ...
  </profile>
</profiles>

This works, but my problem with it is that both profiles have almost completely identical configurations (and they are rather lengthy configurations as well) for this plugin, except for the list of names.

What I'd like to do is have a Maven property contain a list, rather than a single value. Something like this:

<project>
  <properties>
    <names>
      <name>bob</name>
    </names>
  </properties>
  ...

So that my single-value profile will simple redefine the names property. But of course, this doesn't work so easily: the code sample above produces a non-parseable POM error.

Is there any way to do what I want with some other Maven plugin, or trick?

like image 903
FrustratedWithFormsDesigner Avatar asked Jan 26 '26 09:01

FrustratedWithFormsDesigner


1 Answers

Use a parameter which takes a comma-separated value. In your code, translate this String to a List of Strings. You should really wonder if you want this to be set by commandline or if it is sufficient enough to do it only in the plugin configuration. The latter is preferred of course. Just as a small example: see http://maven.apache.org/plugins/maven-site-plugin/site-mojo.html#locales

like image 115
Robert Scholte Avatar answered Jan 28 '26 10:01

Robert Scholte



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!