|
| 1 | +package org.jenkinsci.plugins.pipeline.maven.model; |
| 2 | + |
| 3 | +import java.io.Serializable; |
| 4 | +import java.time.ZonedDateTime; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.List; |
| 8 | +import java.util.SortedSet; |
| 9 | +import java.util.TreeSet; |
| 10 | +import java.util.stream.Collectors; |
| 11 | + |
| 12 | +import javax.annotation.Nonnull; |
| 13 | + |
| 14 | +/** |
| 15 | + * @author <a href="mailto:[email protected]">Cyrille Le Clerc</a> |
| 16 | + */ |
| 17 | +public class MavenExecutionDetails implements Comparable<MavenExecutionDetails>, Serializable { |
| 18 | + |
| 19 | + private static final long serialVersionUID = 1L; |
| 20 | + |
| 21 | + private SortedSet<MavenProjectExecutionDetails> mavenProjectExecutionDetails = new TreeSet<>(); |
| 22 | + |
| 23 | + @Nonnull |
| 24 | + private final ZonedDateTime start; |
| 25 | + @Nonnull |
| 26 | + private ZonedDateTime stop; |
| 27 | + |
| 28 | + public MavenExecutionDetails(@Nonnull ZonedDateTime start) { |
| 29 | + this.start = start; |
| 30 | + this.stop = stop; |
| 31 | + } |
| 32 | + |
| 33 | + public SortedSet<MavenProjectExecutionDetails> getMavenProjectExecutionDetails() { |
| 34 | + return mavenProjectExecutionDetails; |
| 35 | + } |
| 36 | + |
| 37 | + @Nonnull |
| 38 | + public ZonedDateTime getStart() { |
| 39 | + return start; |
| 40 | + } |
| 41 | + |
| 42 | + @Nonnull |
| 43 | + public ZonedDateTime getStop() { |
| 44 | + return stop; |
| 45 | + } |
| 46 | + |
| 47 | + public void setStop(@Nonnull ZonedDateTime stop) { |
| 48 | + this.stop = stop; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public int compareTo(MavenExecutionDetails o) { |
| 53 | + return this.start.compareTo(o.start); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * it's a poor comparison but there is no risk of having 2 builds starting at the same time |
| 58 | + * |
| 59 | + * @param o |
| 60 | + * @return |
| 61 | + */ |
| 62 | + @Override |
| 63 | + public boolean equals(Object o) { |
| 64 | + if (this == o) return true; |
| 65 | + if (o == null || getClass() != o.getClass()) return false; |
| 66 | + |
| 67 | + MavenExecutionDetails that = (MavenExecutionDetails) o; |
| 68 | + |
| 69 | + return start.equals(that.start); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public int hashCode() { |
| 74 | + return start.hashCode(); |
| 75 | + } |
| 76 | + |
| 77 | + public String getExecutionDurationDetails() { |
| 78 | + List<MavenExecutionAggregatedPhasesDetails> aggregates = new ArrayList(Arrays.asList( |
| 79 | + new MavenExecutionAggregatedPhasesDetails("validate", "initialize"), |
| 80 | + new MavenExecutionAggregatedPhasesDetails("generate-sources", "process-sources", "generate-resources", "process-resources", "compile", "process-classes"), |
| 81 | + new MavenExecutionAggregatedPhasesDetails("generate-test-sources", "process-test-sources", "generate-test-resources", "process-test-resources", "test-compile", "process-test-classes", "test"), |
| 82 | + new MavenExecutionAggregatedPhasesDetails("prepare-package", "package"), |
| 83 | + new MavenExecutionAggregatedPhasesDetails("pre-integration-test", "integration-test", "post-integration-test"), |
| 84 | + new MavenExecutionAggregatedPhasesDetails("install"), |
| 85 | + new MavenExecutionAggregatedPhasesDetails("deploy"), |
| 86 | + new MavenExecutionAggregatedPhasesDetails("pre-clean", "clean", "post-clean"), |
| 87 | + new MavenExecutionAggregatedPhasesDetails("pre-site", "site", "post-site", "site-deploy") |
| 88 | + |
| 89 | + )); |
| 90 | + |
| 91 | + for (MavenProjectExecutionDetails projectExecutionDetails : |
| 92 | + getMavenProjectExecutionDetails()) { |
| 93 | + for (MavenMojoExecutionDetails mojoExecutionDetails : projectExecutionDetails.getMojoExecutionDetails()) { |
| 94 | + boolean added = false; |
| 95 | + for (MavenExecutionAggregatedPhasesDetails aggregate : aggregates) { |
| 96 | + if (aggregate.offer(mojoExecutionDetails)) { |
| 97 | + added = true; |
| 98 | + break; |
| 99 | + } |
| 100 | + } |
| 101 | + if (!added) { |
| 102 | + MavenExecutionAggregatedPhasesDetails aggregate = new MavenExecutionAggregatedPhasesDetails(mojoExecutionDetails.getLifecyclePhase()); |
| 103 | + aggregates.add(aggregate); |
| 104 | + boolean offer = aggregate.offer(mojoExecutionDetails); |
| 105 | + if (offer == false) { |
| 106 | + throw new IllegalStateException("Failure to add " + mojoExecutionDetails + " to " + aggregate); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + StringBuilder sb = new StringBuilder(); |
| 112 | + |
| 113 | + for (MavenExecutionAggregatedPhasesDetails aggregate : aggregates) { |
| 114 | + if (aggregate.getMojoExecutionDetails().isEmpty()) { |
| 115 | + |
| 116 | + } else { |
| 117 | + sb.append(" * " + aggregate.getPhases().stream().collect(Collectors.joining(", ")) + " --- " + aggregate.getDuration() + "\n"); |
| 118 | + for (MavenMojoExecutionDetails mojoExecutionDetails : aggregate.getMojoExecutionDetails().stream().filter(m -> m.getDurationMillis() > 1000).collect(Collectors.toList())) { |
| 119 | + sb.append(" * " + mojoExecutionDetails.getPlugin().getArtifactId() + ":" + mojoExecutionDetails.getGoal() + " (" + mojoExecutionDetails.getExecutionId() + ")" + " @ " + mojoExecutionDetails.getProject().getArtifactId() + " --- " + mojoExecutionDetails.getDuration() + "\n"); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return sb.toString(); |
| 125 | + } |
| 126 | +} |
0 commit comments