Skip to content

Instantly share code, notes, and snippets.

@sureshnath
Created March 17, 2019 15:35
Show Gist options
  • Save sureshnath/361725cc9bacfb5bcfb1a27c57956d87 to your computer and use it in GitHub Desktop.
Save sureshnath/361725cc9bacfb5bcfb1a27c57956d87 to your computer and use it in GitHub Desktop.
MoJo command line
https://stackoverflow.com/questions/31620967/any-way-to-specify-a-fileset-as-command-line-parameter
@sureshnath
Copy link
Author

mvn so:multiple-value-maven-plugin:values
-Darray=VALUE_1,VALUE_2,VALUE_3
-Dlist=VALUE_1,VALUE_2,VALUE_3
-Dset=VALUE_1,VALUE_2,VALUE_3
-Dmap=KEY_1=VALUE_1,KEY_2=VALUE_2,KEY_3=VALUE_3
-Dincludes=/,/usr/*
-Dexcludes=/root,/tmp

package so;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.maven.model.FileSet;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

@mojo( name = "values", requiresProject = false )
public class MultipleValuesMojo extends AbstractMojo
{
@parameter( property = "array", required = true )
private String[] array;

@Parameter( property = "list", required = true )
private List<String> list;

@Parameter( property = "set", required = true )
private String[] setElements;
private Set<String> set;

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

@Parameter( property = "includes", required = true )
private List<String> includes;

@Parameter( property = "excludes", required = true )
private List<String> excludes;

@Override
public void execute() throws MojoExecutionException
    {
    getLog().info( "Array: " + Arrays.toString( array ) );
    getLog().info( " List: " + list.toString() );
    set = Arrays.stream( setElements ).collect( Collectors.toSet() ); // with Java >=8
    addSetElementsToSet(); // with Java <8
    getLog().info( "  Set: " + set.toString() );
    map = Arrays.stream( mapEntries ).collect( Collectors.toMap( s -> s, s -> s ) ); // with Java >=8
    putMapEntriesToMap(); // with Java <8
    getLog().info( "  Map: " + map.toString() );

    getLog().info( "Includes: " + includes.toString() );
    getLog().info( "Excludes: " + excludes.toString() );

    FileSet fileSet = new FileSet();
    fileSet.setIncludes( includes );
    fileSet.setExcludes( excludes );
    getLog().info( " FileSet: " + fileSet.toString() );
    } // execute()

private void addSetElementsToSet()
    {
    set = new HashSet<String>( setElements.length );
    for ( String entry : setElements )
        {
        set.add( entry );
        }
    } // addSetElementsToSet()

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()

} // MultipleValuesMojo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment