简体   繁体   中英

Having a map as a Maven plugin parameter

I am using maven 3 and I want to pass in a type Map as a parameter.

I have this in my mojo at the moment:

/**
 * @parameter expression="${rep.env}" alias="environments"
 * @required
 */
private Map<String,String[]> environments = null;

I am passing in this during the configuration:

                <environments>
                    <Testing>
                        <param>
                            unit
                        </param>
                    </Testing>
                </environments>

It is complaining that it is missing the parameter environments, are you allowed to do this in maven?

Did you try to just remove the alias="environments" attribute?

Another point is that I am not sure that Maven will allow you to set a Map of String[] as key. I think it will only deal with Map<String, String> ( the page here only shows a basic Map example).

Eventually, what you can do is to allow comma-separated value instead of a String[] :

<configuration>
    <environments>
        <one>a,b,c</one>
        <two>d</two>
    </environments>
</configuration>

and then, when you have to deal with your values, you simply split your String to get a String array (you can use Apache Commons-lang StringUtils to do that easily):

/**
 * @parameter expression="${rep.env}"
 * @required
 */
private Map<String, String> environments = null;

public void foo() {
    String[] values = StringUtils.split(environments.get("one"), ',');
    // values == {"a", "b", "c"};
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM