Maven插件 - 将参数传递到生命周期阶段

时间:2015-10-22 14:39:29

标签: maven maven-3 maven-plugin

我们有一个自定义maven插件,可以为我们的项目生成一些代码工件。该插件已经配置了这样的生命周期,“generate-resources”阶段调用了我们的自定义类。

<?xml version="1.0" encoding="UTF-8"?>
<component-set>
    <components>
        <component>
            <role>org.apache.maven.lifecycle.mapping.LifecycleMapping</role>
            <role-hint>zip</role-hint>
            <implementation>org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping</implementation>
            <configuration>
                <phases>
                    <generate-resources>com.b.e:productconfig-maven-plugin:generate</generate-resources>
                    <package>com.b.e:productconfig-maven-plugin:zip</package>
                    <install>org.apache.maven.plugins:maven-install-plugin:install</install>
                    <deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy>
                </phases>
            </configuration>
        </component>
    </components>
</component-set>

我们有一个新的要求来运行这个'generate-resources'阶段,参数略有不同。我曾希望我们可以定义第二阶段并传入像这样的mojo自定义属性

<?xml version="1.0" encoding="UTF-8"?>
<component-set>
    <components>
        <component>
            <role>org.apache.maven.lifecycle.mapping.LifecycleMapping</role>
            <role-hint>zip</role-hint>
            <implementation>org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping</implementation>
            <configuration>
                <phases>
                    <generate-resources>com.b.e:productconfig-maven-plugin:generate -Dmojo.param=A</generate-resources>
                    <generate-resources>com.b.e:productconfig-maven-plugin:generate -Dmojo.param=B</generate-resources>
                    <package>com.b.e:productconfig-maven-plugin:zip</package>
                    <install>org.apache.maven.plugins:maven-install-plugin:install</install>
                    <deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy>
                </phases>
            </configuration>
        </component>
    </components>
</component-set>

但这会引发此错误。

[ERROR] Could not find goal 'generate -Dmojo.param=A' in plugin com.b.e:productconfig-maven-plugin:1.0.6-SNAPSHOT among available goals generate, zip -> [Help 1]
org.apache.maven.plugin.MojoNotFoundException: Could not find goal 'generate -DoutputFileBaseDir=/home/poc/b/product_configuration/productconfig-maven-plugin/target/generated/delta' in plugin com.b.e:productconfig-maven-plugin:1.0.6-SNAPSHOT among available goals export, generate
    at org.apache.maven.plugin.internal.DefaultMavenPluginManager.getMojoDescriptor(DefaultMavenPluginManager.java:267)
    at org.apache.maven.plugin.DefaultBuildPluginManager.getMojoDescriptor(DefaultBuildPluginManager.java:185)
    at org.apache.maven.lifecycle.internal.DefaultLifecycleExecutionPlanCalculator.setupMojoExecution(DefaultLifecycleExecutionPlanCalculator.java:152)
    at org.apache.maven.lifecycle.internal.DefaultLifecycleExecutionPlanCalculator.setupMojoExecutions(DefaultLifecycleExecutionPlanCalculator.java:139)
    at org.apache.maven.lifecycle.internal.DefaultLifecycleExecutionPlanCalculator.calculateExecutionPlan(DefaultLifecycleExecutionPlanCalculator.java:116)
    at org.apache.maven.lifecycle.internal.DefaultLifecycleExecutionPlanCalculator.calculateExecutionPlan(DefaultLifecycleExecutionPlanCalculator.java:129)
    at org.apache.maven.lifecycle.internal.BuilderCommon.resolveBuildPlan(BuilderCommon.java:92)

有人知道将参数正确传递到生命周期阶段的方法吗?

2 个答案:

答案 0 :(得分:2)

有两个基本相同要求的问题,没有一个问题有答案:

我很确定这个“带有属性/参数定义的生命周期映射”不起作用。

component.xml中的声明指定哪个目标绑定到生命周期的某个阶段,即a.k.a。生命周期映射。换句话说,在我的理解中:

  • 如果在构建运行期间传递了一个阶段,那么执行什么
  • 关于如何这样做。

没有参考文献:

提到:

  1. 将多个目标绑定到阶段。

    由于DefaultLifecycleMapping.getPhases(...)返回Map,其中键表示相位,这根本不可能。

  2. 与属性/参数定义的目标 - 阶段绑定。

    错误消息的Could not find goal 'generate -Dmojo.param=A'部分告诉我目标部分的完整字符串被视为目标名称,不再应用任何参数提取。

答案 1 :(得分:1)

  1. 对于Binding more than one goal to a phase

    您可以在目标之间放置一个逗号,这肯定适用于Maven 3.6.1,但是我不能说这是否是一项新功能。即:

    <package>org.apache.maven.plugins:maven-jar-plugin:jar,org.expath.pkg:expath-pkg-maven-plugin:pkg</package>
    
  2. 对于A goal-to-phase binding with a property/parameter definition

    我能够以编程方式完成您的要求。

    对于我的插件,我希望能够选择性地首先编译Java类并将其放在Jar中,但是最终的包格式是Xar,将其视为包装器,它将包含Jar和其他内容。< / p>

    我没有使用DefaultLifecycleMapping中的components.xml,而是创建了自己的LifecycleMapping组件,该组件指定了默认生命周期,并从那里可以设置(供我使用-case)Jar插件。

    我的自定义生命周期组件:

    @Component(role = LifecycleMapping.class, hint = "xar", description = "EXPath Pkg Lifecycle Mapping")
    public class PkgLifecycleMapping implements LifecycleMapping {
    
        private final Map<String, Lifecycle> lifecycleMap = new HashMap<>();
    
        public PkgLifecycleMapping() {
            final Map<String, LifecyclePhase> phases = new HashMap<>();
            phases.put("process-resources", new LifecyclePhase("org.apache.maven.plugins:maven-resources-plugin:resources"));
            phases.put("compile", new LifecyclePhase("org.apache.maven.plugins:maven-compiler-plugin:compile"));
    
            final LifecyclePhase packageLifecyclePhase = new LifecyclePhase("org.apache.maven.plugins:maven-jar-plugin:jar,org.expath.pkg:expath-pkg-maven-plugin:pkg");
    
            // configure the maven-jar-plugin mojo
            final LifecycleMojo jarLifecyclePhase = packageLifecyclePhase.getMojos().get(0);
            Xpp3Dom config = jarLifecyclePhase.getConfiguration();
            if (config == null) {
                config =  new Xpp3Dom("configuration");
            }
            final Xpp3Dom skipIfEmpty = new Xpp3Dom("skipIfEmpty");
            skipIfEmpty.setValue("true");
            config.addChild(skipIfEmpty);
            jarLifecyclePhase.setConfiguration(config);
    
            phases.put("package", packageLifecyclePhase);
    
            phases.put("install", new LifecyclePhase("org.apache.maven.plugins:maven-install-plugin:install"));
            phases.put("deploy", new LifecyclePhase("org.apache.maven.plugins:maven-deploy-plugin:deploy"));
    
            final Lifecycle lifecycle = new Lifecycle();
            lifecycle.setId("default");
            lifecycle.setLifecyclePhases( phases );
    
            lifecycleMap.put("default", lifecycle );
        }
    
        @Override
        public Map<String, Lifecycle> getLifecycles() {
            return lifecycleMap;
        }
    
        @Override
        @Deprecated
        public List<String> getOptionalMojos(final String lifecycle) {
            return null;
        }
    
        @Override
        @Deprecated
        public Map<String, String> getPhases(final String lifecycle) {
            final Lifecycle lifecycleMapping = lifecycleMap.get(lifecycle);
            if (lifecycleMapping != null) {
                return LifecyclePhase.toLegacyMap(lifecycleMapping.getLifecyclePhases());
            } else {
                return null;
            }
        }
    }
    

    您还需要将plexus元数据插件添加到pom.xml中的构建插件中:

        <plugin>
            <groupId>org.codehaus.plexus</groupId>
            <artifactId>plexus-component-metadata</artifactId>
            <version>2.0.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate-metadata</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    

    我的src/main/resources/META-INF/plexus/components.xml如下:

    <component-set>
        <components>
            <component>
                <role>org.apache.maven.artifact.handler.ArtifactHandler</role>
                <role-hint>xar</role-hint>
    <implementation>org.apache.maven.artifact.handler.DefaultArtifactHandler</implementation>
                <configuration>
                    <type>xar</type>
                    <extension>xar</extension>
                    <packaging>xar</packaging>
                </configuration>
            </component>
        </components>
    </component-set>
    

    构建后,我生成的target/classes/META-INF/plexus/components.xml如下:

    <component-set>
        <components>
            <component>
                <role>org.apache.maven.artifact.handler.ArtifactHandler</role>
                <role-hint>xar</role-hint>
                <implementation>org.apache.maven.artifact.handler.DefaultArtifactHandler</implementation>
                <configuration>
                    <type>xar</type>
                    <extension>xar</extension>
                    <packaging>xar</packaging>
                </configuration>
            </component>
            <component>
              <role>org.apache.maven.lifecycle.mapping.LifecycleMapping</role>
              <role-hint>xar</role-hint>
              <implementation>org.expath.pkg.maven.PkgLifecycleMapping</implementation>
              <description>EXPath Pkg Lifecycle Mapping</description>
              <isolated-realm>false</isolated-realm>
            </component>
        </components>
    </component-set>