Is it possible to make a parameter configurable ONLY via its alias?

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Is it possible to make a parameter configurable ONLY via its alias?

Tad Cordle
I'm working on a plugin
<https://github.com/GoogleContainerTools/jib/tree/master/jib-maven-plugin> that
has nested parameters, and I'd like to introduce aliases to give people the
option of more concise configuration. For example, instead of writing:

<configuration>
  <from>
    <image>baseImage</image>
    <auth>
      <username>user</username>
      <password>pass</password>
    </auth>
  </from>
</configuration>

they can instead write:

<configuration>
  <from.image>baseImage</from.image>
  <from.auth.username>user</from.auth.username>
  <from.auth.password>pass</from.auth.password>
</configuration>

However, it doesn't seem to be possible to declare aliases directly on
nested parameters. My workaround for this is to create a new top-level
field for each parameter. For example, the original configuration is
possible using this in the mojo:

public static class AuthConfiguration {
  @Parameter private String username;
  @Parameter private String password;
}
public static class FromConfiguration {
  @Parameter private String image;
  @Parameter private AuthConfiguration auth = new AuthConfiguration();
}
@Parameter private FromConfiguration from = new FromConfiguration();

And to create an alias, I add a new field, and choose one of the values in
the getter:

@Parameter(alias = "from.image")
private String fromImageAlias;

String getFromImage() {
  if (fromImageAlias != null) {
    return fromImageAlias;
  }
  return from.image;
}

This works, but since the alias is just an alias and not a replacement for
the field name, it's still possible to configure the parameter using the
name of the field. e.g.

<configuration>
  <fromImageAlias>baseImage</fromImageAlias>
</configuration>

Is it somehow possible to expose a parameter only by its alias, and
disallow configuration using the field name? Or even better, is there a way
of configuring aliases on nested parameters that I don't know about?

P.S. I notice the maven docs
<https://maven.apache.org/plugin-tools/apidocs/org/apache/maven/plugins/annotations/Parameter.html>
 say "nested bean injection requires Sisu or JSR330 annotations". Can this
be used to accomplish what I'm looking for, and if so how?