按组合而不是继承导入maven插件配置。可以使用构建扩展来完成吗?

时间:2012-07-31 21:36:37

标签: java maven maven-plugin maven-extension

按合成而不是继承导入maven插件配置。是否可以使用构建扩展来完成?

我已经使用maven超过3年了,并且有一个总是让我烦恼的缺点。 是时候找到解决方案了。

问题:

我有一个带有3个孩子的“爸爸”maven模块:“男孩”,“女孩”和“孩子”。 这些孩子中的每一个都必须拥有自己独特的一组插件配置,用于默认的“干净安装”构建。 我不想把这个配置放在孩子们的poms上。我宁愿把它放在我以后可以重复使用的地方。

我已尝试使用配置文件,但它不起作用 - 请在MNG-5127上查看我的评论和附加项目

我通过对daddy.zip项目进行以下更改找到了更好的解决方案:

1)在爸爸的pom上,用[phase] none [/ phase]

替换插件执行的[profiles]
<build>
    ...
    <plugins>
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <id>printboy</id>
                    <phase>none</phase>
                    <configuration>
                        <target>
                            <echo message="@@@@@@@@@@@@ HELLO! Iam a BOY project"/>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
                <execution>
                    <id>printkid</id>
                    <phase>none</phase>
                    <configuration>
                        <target>
                            <echo message="@@@@@@@@@@@@ HELLO! Iam a KID project"/>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
                <execution>
                    <id>printgirl</id>
                    <phase>none</phase>
                    <configuration>
                        <target>
                            <echo message="@@@@@@@@@@@@ HELLO! Iam a GIRL project"/>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    ...
</build>

2)在同一个pom上,添加了自定义构建扩展

<build>
    <extensions>
        <extension>
            <groupId>br.com.touchtec.maven.plugins</groupId>
            <artifactId>execution-enabler-extension</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </extension>
    </extensions>
    ...
</build>

3)扩展将根据项目的属性值更改插件执行阶段。下面的Java代码。

@Component(role = AbstractMavenLifecycleParticipant.class, hint = "enableif")
public class EnableIfExtension extends AbstractMavenLifecycleParticipant {

    @Override
    public void afterProjectsRead(MavenSession session)
            throws MavenExecutionException {
        String phase = "validate";
        for(MavenProject project : session.getProjects()){
            Properties properties = project.getProperties();
            if(properties != null){
                if("boy".equals(properties.getProperty("project_type"))){
                    setExecutionPhase(project, "printboy", phase);
                    setExecutionPhase(project, "printkid", phase);
                }
                if("girl".equals(properties.getProperty("project_type"))){
                    setExecutionPhase(project, "printgirl", phase);
                    setExecutionPhase(project, "printkid", phase);
                }
                if("kid".equals(properties.getProperty("project_type"))){
                    setExecutionPhase(project, "printkid", phase);
                }
            }
        }
    }

    private void setExecutionPhase(MavenProject project, String executionId, String phase) {
        for(Plugin plugin : project.getBuild().getPlugins()){
            if(plugin.getExecutions() != null){
                for(PluginExecution execution : plugin.getExecutions()){
                    if(executionId.equals(execution.getId())){
                        execution.setPhase(phase);
                    }
                }
            }
        }
    }
}

4)子模块poms只需要定义一个属性来告诉构建扩展器要做什么,例如,boy项目:

<project>
    <properties>
        <project_type>boy</project_type>
    </properties>

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.family</groupId>
    <artifactId>boy</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <parent>
        <groupId>com.family</groupId>
        <artifactId>daddy</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <!-- No "build" section. Beautifull. -->
</project>

在像这样的爸爸项目上调用mvn clean install将输出所需的结果:男孩将是男孩,女孩将是女孩,两个都将是孩子。

问题是:我们能做得更好吗?

理论上,构建扩展可以导入来自不同pom的男孩,女孩和孩子的构建配置定义......对吗? 这将是将[build]配置导入pom的一种聪明而优雅的方式。

有很多可用的maven插件,但我没有看到很多(实际上是任何)可用的构建扩展,可以插入到构建中。 有没有人知道有助于此的构建扩展?


更新

这不是一个真正的答案,但它是我问题的实际解决方案,所以这里就是。

  

按合成而不是继承导入maven插件配置。是否可以使用构建扩展来完成?

可能

  

有没有人知道有助于此的构建扩展?

我很确定没有一个

但是,有一种使用配置文件的解决方案。

诀窍是基于文件的配置文件激活实际上是继承的(它只适用于maven3)

请参阅daddy2.zip

附带的MNG-5127

这比使用构建扩展策略更好,因为使用配置文件比仅更改一些插件执行阶段提供了更多的灵活性。


更新2

  
    

有没有人知道有助于此的构建扩展?

  
     

我很确定没有一个

实际上有一些东西!

它作为项目附在MNG-5102(作者Maurizio Pillitu) 我还没有对它进行测试,但似乎它与提议的构建扩展非常接近。

1 个答案:

答案 0 :(得分:6)

我认为Maven Tiles可以帮助您了解具体的用例。 它仍在进行中,但它目前正在完成您所描述的内容,我设法“分解”wicket-quickstart并成功运行Jetty,甚至添加了多模块结构。

任何评论或提示都非常受欢迎。 谢谢,   莫里吉奥