【问题标题】:Any way to specify a FileSet as command line parameter?有什么方法可以将 FileSet 指定为命令行参数?
【发布时间】:2015-10-15 17:49:00
【问题描述】:

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

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

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

【问题讨论】:

    标签: maven mojo


    【解决方案1】:

    见:

    POM

      <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] ------------------------------------------------------------------------
    

    【讨论】:

    • 关于如何在命令行上传递参数的令人印象深刻的例子,除了 Fileset 不是其中之一。
    • 这实际上并没有回答OP的问题,甚至一点点。投反对票。
    • @kyl191 现在好点了吗?
    • @MiguelFerreira [继续] 我很确定一个对象的(分层)成员变量结构不能由 一个 单个属性值表示,除非通过引入和评估相应的自己在这个属性值中构建结构——例如,通过使用 JSON 或 XML 来扩展我之前评论中提到的更简单的示例。所以,是的,你是对的,因为当需要通过命令行传递复杂对象的值时,它并不容易直接。
    • @MiguelFerreira jacoco.fileSets parameter 终于被删除了 due to "严重怀疑自从“合并”mojo 的初始实施以来,通过命令行对该属性的规范是否有效,因为支持结构是不是普通的字符串,而是复杂的对象".
    【解决方案2】:

    任何寻求解决方案的人:

    -D<some-property>.fileSet=['path-to-fileset']
    

    为我做了诀窍。它可能需要稍作修改,具体取决于您正在配置的插件,但您明白了。

    在 Maven 3.5.0 上测试

    【讨论】:

    • 如何在 Mojo 中声明 &lt;some-property&gt;?你从哪里得到这个&lt;some-property&gt;.fileSet=['path-to-fileset'] 语法?我无法完成这项工作,除非在 POM 中使用 &lt;build&gt;/&lt;plugins&gt;/&lt;plugin&gt;/&lt;configuration&gt;/&lt;fileset&gt;/&lt;directory&gt;${fileSet}&lt;properties&gt;/&lt;fileSet&gt;path-to-fileset 声明,可以在命令行上使用 -DfileSet=other-path-to-fileset 覆盖它们。但这不是 OP 想要的:“一个不需要项目来运行的 Mojo”。
    • 我做了一些研究来记住为什么我需要这个。这是我的答案的来源:github.com/jeremylong/DependencyCheck/issues/…
    • 感谢您从 2018 年开始挖掘这一点。但是,经过我自己的代码的大量尝试和错误,这种语法似乎只是假的。我用-DscanSet.fileSet=src/main 尝试了org.owasp:dependency-check-maven 插件,也得到了BUILD SUCCESS。然后我尝试了-DscanSet.fileSet=['src/main'] -X,在构建日志中发现:[DEBUG] (f) scanSet = []。插件的正常输出与任何一个参数都没有任何区别,因此如果没有-X 并搜索近一千行调试行,您将无法识别。
    猜你喜欢
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    相关资源
    最近更新 更多