4

am trying to pass Map kind of parameters to my maven plugin through command line. Here is how i tried,

$mvn -U -X sample.plugin:hello-maven-plugin:1.0-SNAPSHOT:sayhi -Dsayhi.myMap=key1=value1

$mvn -U -X sample.plugin:hello-maven-plugin:1.0-SNAPSHOT:sayhi -Dsayhi.myMap={key1=value1}

None of these are working and getting following error:

Caused by:
org.codehaus.plexus.component.configurator.ComponentConfigurationException:
Cannot assign configuration entry 'myMap' with value '${sayhi.myMap}' of type
java.lang.String to property of type java.util.Map**

Here is my parameter in Mojo:

/**
 * My Map.
 */
@Parameter(property = "sayhi.myMap", required = false)
private Map<String,String> myMap = new HashMap<String, String>();

followed instructions at ==> https://maven.apache.org/guides/mini/guide-configuring-plugins.html#Mapping_Collections, but no luck., i think am missing something very small. am working on maven v3.2.1

thanks

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
  • I have tried all other like Array, List and Set parameters. all are working through command line. struck only with Map. – gangadhar mamillapalli Apr 17 '16 at 15:56
  • Why would you like to give such information via command line? What is the purpose of the plugin? – khmarbaise Apr 19 '16 at 08:31
  • we do like to run maven plugin through command line( through Java code), So for that we need to pass configurations parameters. this is our in house plugin. – gangadhar mamillapalli Apr 20 '16 at 06:45
  • What is the purpose of not using Maven itself and using the life cycle etc. ? What kind of problem would you like to solve? – khmarbaise Apr 20 '16 at 18:34
  • HI @khmarbaise, we have one in house maven plugin which does some scanning of the project code in which it is placed. now we wanted to use it through bamboo. so for this instead of enforcing the users to place this in their project pom, we are running the plugin through command line by passing the project information. hope i have explained you good. – gangadhar mamillapalli Apr 21 '16 at 00:15
  • 1
    To be honest it sounds wrong, cause everything you seemed to implement is already in Maven itself ...Furthermore you can use an appropriate corporate pom which solves such things... – khmarbaise Apr 21 '16 at 08:41

2 Answers2

2

We don't have the option to pass the map variables in the command line as per my knowledge, but you can pass map variable in the following way.

Create xml file and create one plugin (, for XML please refer maven doc)

<myMap>
      <key1>value1</key1>
      <key2>value2</key2>
</myMap>

Your mojo will be:

@Parameter(property = "myMap", required = false)
private Map<String,String> myMap;

Yor maven command will be:

$mvn -s <path_to_xml_file> -U -X sample.plugin:hello-maven-plugin:1.0-SNAPSHOT:sayhi
Sireesh Vattikuti
  • 1,180
  • 1
  • 9
  • 21
  • working with XML file wont suits my requirement, As i mentioned in my initial post, all other collections Arrays,Lists,Sets are working and with the page ==> https://maven.apache.org/guides/mini/guide-configuring-plugins.html#Mapping_Collections , they no where mentioned that MAP wont work – gangadhar mamillapalli Apr 20 '16 at 12:22
  • surprised to see no one faced this situation. raised an issue with maven aswell – gangadhar mamillapalli Apr 24 '16 at 15:43
  • @gangadharmamillapalli _"they no where mentioned that MAP wont work_" is not true. [Guide to Developing Java Plugins](https://maven.apache.org/guides/plugin/guide-java-plugin-development.html#Parameter_Types_With_Multiple_Values) mentions it: "_Maps – This category covers any class which implements `java.util.Map` such as `HashMap` but does not implement `java.util.Properties`._" – Gerold Broser Apr 25 '16 at 12:02
0

I experienced the same issue and Guide to Developing Java Plugins, Introduction, Parameter Types With Multiple Values, Maps explains it:

Maps

This category covers any class which implements java.util.Map such as HashMap but does not implement java.util.Properties.

See this answer how I solved it.

The workaround includes the following:

    ...

    @Parameter( property = "map", required = true )
    private String[] mapEntries;
    private Map<String, String> map;

    ...

        map = Arrays.stream( mapEntries ).collect( Collectors.toMap( s -> s, s -> s ) ); // with Java >=8
        putMapEntriesToMap(); // with Java <8

    ...

    private void putMapEntriesToMap()
        {
        map = new HashMap<String, String>( mapEntries.length );
        for ( String entry : mapEntries )
            {
            int equalsPosition = entry.indexOf( "=" );
            map.put(
                entry.substring( 0, equalsPosition ),
                entry.substring( equalsPosition + 1 ) );
            }
        } // putMapEntriesToMap()

    ...        
Community
  • 1
  • 1
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107