如何对maven执行的不同阶段进行计时

发布于 2024-10-19 09:25:00 字数 135 浏览 1 评论 0原文

我有一个 Maven 构建,速度非常慢。我想知道是否有一种方法可以分析 Maven 的执行情况,以便找出哪些是最耗时的步骤。

稍后我将想要比较旧版本(速度更快)的构建之间的这些时间,因此理想情况下它们应该采用可以比较/比较/绘制图表的格式。

I have a maven build that is extremely slow. I would like to know whether there is a way to profile maven execution in order to find out which are the most time-consuming steps.

Later I will want to compare these times between builds for older versions (which were faster), so they should be ideally in a format that can be compared/diffed/graphed.

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

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

发布评论

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

评论(5

梦晓ヶ微光ヅ倾城 2024-10-26 09:25:00

这是最快的方法:

export MAVEN_OPTS="-Dorg.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss,SSS \
                   -Dorg.slf4j.simpleLogger.showDateTime=true" 
mvn test

结果

MAVEN_OPTS="-Dorg.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss,SSS -Dorg.slf4j.simpleLogger.showDateTime=true" mvn test
17:06:07,330 [INFO] Scanning for projects...
17:06:07,447 [INFO] 
17:06:07,447 [INFO] ------------------------------------------------------------------------
17:06:07,448 [INFO] Building bimble-server 0.0.1-SNAPSHOT
17:06:07,448 [INFO] ------------------------------------------------------------------------
17:06:07,747 [INFO] 
17:06:07,748 [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ bimble-server ---

如果您随后将该环境变量添加到 shell 的配置文件(如 ~/.bashrc~/.profile)中,您将获得每次使用 Maven 时的这些时间。

基于 Stanley Hillner 博客的信息:< /a>

This is the quickest possible way:

export MAVEN_OPTS="-Dorg.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss,SSS \
                   -Dorg.slf4j.simpleLogger.showDateTime=true" 
mvn test

Results in

MAVEN_OPTS="-Dorg.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss,SSS -Dorg.slf4j.simpleLogger.showDateTime=true" mvn test
17:06:07,330 [INFO] Scanning for projects...
17:06:07,447 [INFO] 
17:06:07,447 [INFO] ------------------------------------------------------------------------
17:06:07,448 [INFO] Building bimble-server 0.0.1-SNAPSHOT
17:06:07,448 [INFO] ------------------------------------------------------------------------
17:06:07,747 [INFO] 
17:06:07,748 [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ bimble-server ---

If you then add that environment variable to your shell's config file (like ~/.bashrc or ~/.profile) you will have those timings every time you use Maven.

Based on info from Stanley Hillner's blog:

爱,才寂寞 2024-10-26 09:25:00

开箱即用的解决方案是 takari maven profiler:
https://github.com/takari/maven-profiler

其页面的示例输出:

org.apache.maven:maven-core:3.1.2-SNAPSHOT

    clean 176ms
        org.apache.maven.plugins:maven-clean-plugin:2.5 (default-clean) 176ms

    initialize 408ms
        org.codehaus.mojo:buildnumber-maven-plugin:1.2 (create-noncanonicalrev) 349ms
        org.codehaus.mojo:buildnumber-maven-plugin:1.2 (create-buildnumber) 59ms

    generate-sources 408ms
        org.codehaus.modello:modello-maven-plugin:1.8.1 (standard) 369ms
        org.codehaus.modello:modello-maven-plugin:1.8.1 (standard) 28ms
        org.codehaus.modello:modello-maven-plugin:1.8.1 (standard) 11ms

    generate-resources 933ms
        org.apache.maven.plugins:maven-remote-resources-plugin:1.4 (default) 932ms

    process-resources 225ms
        org.apache.maven.plugins:maven-resources-plugin:2.6 (default-resources) 224ms

    compile 4s 522ms
        org.apache.maven.plugins:maven-compiler-plugin:2.5.1 (default-compile) 4s 522ms

    process-classes 6s 880ms
        org.codehaus.mojo:animal-sniffer-maven-plugin:1.6 (check-java-1.5-compat) 5s 814ms
        org.codehaus.plexus:plexus-component-metadata:1.5.5 (default) 946ms
        org.sonatype.plugins:sisu-maven-plugin:1.1 (default) 120ms

    process-test-resources 173ms
        org.apache.maven.plugins:maven-resources-plugin:2.6 (default-testResources) 173ms

    test-compile 818ms
        org.apache.maven.plugins:maven-compiler-plugin:2.5.1 (default-testCompile) 818ms

    process-test-classes 134ms
        org.codehaus.plexus:plexus-component-metadata:1.5.5 (default) 110ms
        org.sonatype.plugins:sisu-maven-plugin:1.1 (default) 23ms

    test 11s 306ms
        org.apache.maven.plugins:maven-surefire-plugin:2.12 (default-test) 11s 306ms

    package 1s 371ms
        org.apache.maven.plugins:maven-jar-plugin:2.4 (default-jar) 502ms
        org.apache.maven.plugins:maven-site-plugin:3.3 (attach-descriptor) 869ms

Out of the box solution is the takari maven profiler:
https://github.com/takari/maven-profiler

Sample output from its page:

org.apache.maven:maven-core:3.1.2-SNAPSHOT

    clean 176ms
        org.apache.maven.plugins:maven-clean-plugin:2.5 (default-clean) 176ms

    initialize 408ms
        org.codehaus.mojo:buildnumber-maven-plugin:1.2 (create-noncanonicalrev) 349ms
        org.codehaus.mojo:buildnumber-maven-plugin:1.2 (create-buildnumber) 59ms

    generate-sources 408ms
        org.codehaus.modello:modello-maven-plugin:1.8.1 (standard) 369ms
        org.codehaus.modello:modello-maven-plugin:1.8.1 (standard) 28ms
        org.codehaus.modello:modello-maven-plugin:1.8.1 (standard) 11ms

    generate-resources 933ms
        org.apache.maven.plugins:maven-remote-resources-plugin:1.4 (default) 932ms

    process-resources 225ms
        org.apache.maven.plugins:maven-resources-plugin:2.6 (default-resources) 224ms

    compile 4s 522ms
        org.apache.maven.plugins:maven-compiler-plugin:2.5.1 (default-compile) 4s 522ms

    process-classes 6s 880ms
        org.codehaus.mojo:animal-sniffer-maven-plugin:1.6 (check-java-1.5-compat) 5s 814ms
        org.codehaus.plexus:plexus-component-metadata:1.5.5 (default) 946ms
        org.sonatype.plugins:sisu-maven-plugin:1.1 (default) 120ms

    process-test-resources 173ms
        org.apache.maven.plugins:maven-resources-plugin:2.6 (default-testResources) 173ms

    test-compile 818ms
        org.apache.maven.plugins:maven-compiler-plugin:2.5.1 (default-testCompile) 818ms

    process-test-classes 134ms
        org.codehaus.plexus:plexus-component-metadata:1.5.5 (default) 110ms
        org.sonatype.plugins:sisu-maven-plugin:1.1 (default) 23ms

    test 11s 306ms
        org.apache.maven.plugins:maven-surefire-plugin:2.12 (default-test) 11s 306ms

    package 1s 371ms
        org.apache.maven.plugins:maven-jar-plugin:2.4 (default-jar) 502ms
        org.apache.maven.plugins:maven-site-plugin:3.3 (attach-descriptor) 869ms
山色无中 2024-10-26 09:25:00

https://github.com/jcgay/maven-profiler 是一个类似的方便工具。它易于设置和使用。 (在核心 Maven 中拥有类似的东西或 EventSpy takari/maven-profiler 作为一个选项肯定会很简洁;评论 https://issues.apache.org/jira/browse/MNG-4639 ..)

https://github.com/jcgay/maven-profiler is a similar handy tool. It's easy to setup and use. (Having something like it or EventSpy takari/maven-profiler in core Maven as an option would certainly be neat; comment in https://issues.apache.org/jira/browse/MNG-4639 ..)

本王不退位尔等都是臣 2024-10-26 09:25:00

该功能已包含在 Maven3 中。这是相关的票证: https://issues.apache.org/jira/browse/MNG -4639

如果您需要对 Maven2 执行相同的操作,我建议您构建自己的插件,该插件可以挂钩到执行的所有阶段(或者只是您需要跟踪的阶段)。

This functionality has been included in Maven3. Here's the associated ticket: https://issues.apache.org/jira/browse/MNG-4639

If you need to do the same with Maven2 I'd recommend building yor own plugin that is hooks into all phases of execution ( or just the ones you need to track).

残月升风 2024-10-26 09:25:00

我只是在这里创建一个要点: https://gist.github.com/boly38/7316378

这是一个关于如何记录某些 maven 生命周期的日期时间的示例步骤

当然,您可以调整此示例来设置您自己的输出格式(并绘制图表......)。

希望这有助于


提取:

        <profile>
            <id>stats</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>log_validate</id>
                                <phase>validate</phase>
                                <goals><goal>run</goal></goals>
                                <configuration>
                                    <tasks>
                                        <tstamp><format property="stepTstamp" pattern="dd-HH:mm:ss" locale="en,US" /></tstamp>
                                        <echo file="stats.log" append="true"
                                              message="${line.separator}${line.separator}${stepTstamp} validate${line.separator}"/>
                                    </tasks>
                                </configuration>
                            </execution>
    (...)
                            <execution>
                                <id>log_process_sources</id>
                                <phase>process-sources</phase>
                                <goals><goal>run</goal></goals>
                                <configuration>
                                    <tasks>
                                        <tstamp><format property="stepTstamp" pattern="dd-HH:mm:ss" locale="en,US" /></tstamp>
                                        <echo file="stats.log" append="true"
                                              message="${stepTstamp} process-sources${line.separator}"/>
                                    </tasks>
                                </configuration>
                            </execution>
(...)

i just create a gist here : https://gist.github.com/boly38/7316378

This is a sample example on how to log datetime of some maven lifecycle steps.

Of course you could adapt this sample to set your own output format (and graph it ...).

Hope this help


Extract :

        <profile>
            <id>stats</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>log_validate</id>
                                <phase>validate</phase>
                                <goals><goal>run</goal></goals>
                                <configuration>
                                    <tasks>
                                        <tstamp><format property="stepTstamp" pattern="dd-HH:mm:ss" locale="en,US" /></tstamp>
                                        <echo file="stats.log" append="true"
                                              message="${line.separator}${line.separator}${stepTstamp} validate${line.separator}"/>
                                    </tasks>
                                </configuration>
                            </execution>
    (...)
                            <execution>
                                <id>log_process_sources</id>
                                <phase>process-sources</phase>
                                <goals><goal>run</goal></goals>
                                <configuration>
                                    <tasks>
                                        <tstamp><format property="stepTstamp" pattern="dd-HH:mm:ss" locale="en,US" /></tstamp>
                                        <echo file="stats.log" append="true"
                                              message="${stepTstamp} process-sources${line.separator}"/>
                                    </tasks>
                                </configuration>
                            </execution>
(...)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文