gpt4 book ai didi

maven - 有什么方法可以将 FileSet 指定为命令行参数?

转载 作者:行者123 更新时间:2023-12-04 15:20:56 30 4
gpt4 key购买 nike

我正在创建一个不需要运行项目的 Mojo。

我想使用类似于 org.apache.maven.model.FileSet 的东西(提供包含和排除的多个目录)作为 @parameter但我的问题是我需要能够使用命令行设置这些值。

知道如何实现这一目标吗?

最佳答案

看:

  • Guide to Developing Java Plugins, Parameter Types With Multiple Values
  • Using Plugin Tools Java5 Annotations
  • Maven Plugin Tool for Annotations, Supported Annotations
  • org.apache.maven.model.FileSet

  • 聚甲醛

      <groupId>so</groupId>
    <artifactId>multiple-values-maven-plugin</artifactId>
    <version>1.0</version>
    <packaging>maven-plugin</packaging>

    <dependencies>
    <dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-plugin-api</artifactId>
    <version>3.3.3</version>
    </dependency>

    <dependency>
    <groupId>org.apache.maven.plugin-tools</groupId>
    <artifactId>maven-plugin-annotations</artifactId>
    <version>3.4</version>
    <scope>provided</scope><!-- annotations are needed only to build the plugin -->
    </dependency>
    </dependencies>

    <!-- This latest plugin has to be used if using Java 8 classes. -->
    <build>
    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-plugin-plugin</artifactId>
    <version>3.4</version>
    </plugin>
    </plugins>
    </build>

    魔力

    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



    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

    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building multiple-values-maven-plugin 1.0
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- multiple-values-maven-plugin:1.0:values (default-cli) @ multiple-values-maven-plugin ---
    [INFO] Array: [VALUE_1, VALUE_2, VALUE_3]
    [INFO] List: [VALUE_1, VALUE_2, VALUE_3]
    [INFO] Set: [VALUE_3, VALUE_2, VALUE_1]
    [INFO] Map: {KEY_1=VALUE_1, KEY_3=VALUE_3, KEY_2=VALUE_2}
    [INFO] Includes: [/, /usr/*]
    [INFO] Excludes: [/root, /tmp]
    [INFO] FileSet: FileSet {directory: null, PatternSet [includes: {/, /usr/*}, excludes: {/root, /tmp}]}
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 1.671 s
    [INFO] Finished at: 2015-07-25T21:44:09+02:00
    [INFO] Final Memory: 11M/115M
    [INFO] ------------------------------------------------------------------------

    关于maven - 有什么方法可以将 FileSet 指定为命令行参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31620967/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com