压倒一切的“干净” Maven 中的生命周期

发布于 2024-10-20 00:11:23 字数 1182 浏览 7 评论 0原文

我正在阅读一本解释如何覆盖 Maven 的“默认”生命周期的书。

它说:要为打包类型定义新的生命周期,您需要在 Plexus 中配置 LifecycleMapping 组件。在您的插件项目中,在 src/main/resources 下创建 META-INF/plexus/components.xml。在 Components.xml 中添加如下所示的内容,就完成了。通过以下配置,我可以自定义“jar”打包类型的默认生命周期。现在如果我执行
$ mvn 包
它直接执行“package”阶段,跳过默认生命周期的所有其他阶段,并执行“maven-zip-plugin”的“echo”目标。

<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>
                    <package>org.sonatype.mavenbook.plugins:maven-zip-plugin:echo
                    </package>
                </phases>
            </configuration>
        </component>
    </components>
</component-set>

我的问题是:如何定制“干净”的生命周期。例如,假设有人输入
$ mvn 干净
我不想运行 clean:clean 来执行“maven-clean-plugin”插件的“clean”目标,而是想执行“customPlugin”的“customClean”目标。

I was going through a book that explain how to override 'default' lifecycle of Maven.

It says: To define a new lifecycle for a packaging type, you'll need to configure a LifecycleMapping component in Plexus. In your plugin project, create a META-INF/plexus/components.xml under src/main/resources. In components.xml add the content as shown below, and you're done. With below configuration, I'm able to customize the default lifecycle for 'jar' packaging type. Now If I exeute
$ mvn package

It straigh away executes 'package' phase skipping all other phases of default lifecycle and executes 'echo' goal of 'maven-zip-plugin'.

<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>
                    <package>org.sonatype.mavenbook.plugins:maven-zip-plugin:echo
                    </package>
                </phases>
            </configuration>
        </component>
    </components>
</component-set>

My question is: How can I customize 'clean' lifecycle. For example, assume when some one types
$ mvn clean

Instead of running clean:clean that will execute 'clean' goal of 'maven-clean-plugin' plugin, I wanted to execute 'customClean' goal of 'customPlugin'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

面如桃花 2024-10-27 00:11:23

对于您所描述的情况,更简单的方法是阻止 maven-clean-pluginclean 阶段运行,并将 customPlugin 附加到 clean > 相代替。这比短路整个生命周期更简单,并将所有 Maven 配置保留在 pom.xml 中。

1 阻止 maven-clean-plugin

<plugin>
    <artifactId>maven-clean-plugin</artifactId>
    <version>2.4.1</version>
    <configuration>
        <skip>true</skip>
    </configuration>
</plugin>

2 将您自己的插件附加到 clean 阶段

<plugin>
    <artifactId>maven-customPlugin-plugin</artifactId>
    <version>customPlugin-version</version>
    <executions>
        <execution>
            <id>customised-clean</id>
            <goals>
                <goal>customClean</goal>
            </goals>
            <phase>clean</phase>
        </execution>
    </executions>
</plugin>

For what you describe, it is simpler to just prevent the maven-clean-plugin from running during the clean phase, and attach customPlugin to the clean phase instead. This is simpler than short-circuiting the whole lifecycle, and keeps all your maven config in your pom.

1 prevent the maven-clean-plugin

<plugin>
    <artifactId>maven-clean-plugin</artifactId>
    <version>2.4.1</version>
    <configuration>
        <skip>true</skip>
    </configuration>
</plugin>

2 attach your own plugin to the clean phase

<plugin>
    <artifactId>maven-customPlugin-plugin</artifactId>
    <version>customPlugin-version</version>
    <executions>
        <execution>
            <id>customised-clean</id>
            <goals>
                <goal>customClean</goal>
            </goals>
            <phase>clean</phase>
        </execution>
    </executions>
</plugin>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文