diff --git a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java index dc66ee0e4..ee1990c71 100644 --- a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java +++ b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginMojo.java @@ -28,6 +28,7 @@ import org.apache.maven.artifact.DependencyResolutionRequiredException; import org.apache.maven.execution.MavenSession; import org.apache.maven.model.ConfigurationContainer; +import org.apache.maven.model.Plugin; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.Component; @@ -218,7 +219,7 @@ private List getClasspathElements() throws MojoExecutionException { } /** - * Scans the module structure of the current Maven installation to retrieve the ToolKit's plugin configurations and + * Scans the module structure of the current Maven setup to retrieve the ToolKit's plugin configurations and * stores the matches between AEM component Java packages and repository paths. The references are passed to the * {@link PluginSettings} builder * @param builder {@link PluginSettings.Builder} instance @@ -232,28 +233,46 @@ private void populateReferenceEntries(PluginSettings.Builder builder) { .collect(Collectors.toList()); for (MavenProject contentPackage : contentPackages) { - Xpp3Dom pluginConfig = contentPackage + Plugin pluginDefinition = contentPackage .getBuildPlugins() .stream() .filter(plugin -> PLUGIN_ARTIFACT_ID.equals(plugin.getArtifactId())) - .map(ConfigurationContainer::getConfiguration) - .filter(Xpp3Dom.class::isInstance) - .map(Xpp3Dom.class::cast) .findFirst() .orElse(null); - if (pluginConfig == null) { + if (pluginDefinition == null) { continue; } - String pathBase = Optional.ofNullable(pluginConfig.getChild(CONFIG_KEY_PATH_BASE)) - .map(Xpp3Dom::getValue) - .orElse(null); - String referenceBase = Optional.ofNullable(pluginConfig.getChild(CONFIG_KEY_REFERENCE_BASE)) - .map(Xpp3Dom::getValue) - .orElse(null); - builder.referenceEntry(pathBase, referenceBase); + Optional.ofNullable(pluginDefinition.getConfiguration()) + .filter(Xpp3Dom.class::isInstance) + .map(Xpp3Dom.class::cast) + .ifPresent(config -> populateReferenceEntry(config, builder)); + pluginDefinition + .getExecutions() + .stream() + .map(ConfigurationContainer::getConfiguration) + .filter(Xpp3Dom.class::isInstance) + .map(Xpp3Dom.class::cast) + .forEach(config -> populateReferenceEntry(config, builder)); } } + /** + * Extracts the path and reference base from the provided configuration and passes them to the + * {@link PluginSettings} builder + * @param config A {@link Xpp3Dom} object representing a configuration of the ToolKit's plugin or one of its + * executions + * @param builder {@link PluginSettings.Builder} instance + */ + private void populateReferenceEntry(Xpp3Dom config, PluginSettings.Builder builder) { + String pathBase = Optional.ofNullable(config.getChild(CONFIG_KEY_PATH_BASE)) + .map(Xpp3Dom::getValue) + .orElse(null); + String referenceBase = Optional.ofNullable(config.getChild(CONFIG_KEY_REFERENCE_BASE)) + .map(Xpp3Dom::getValue) + .orElse(null); + builder.referenceEntry(pathBase, referenceBase); + } + /** * Transfers to the main logger a line retrieved from the secondary Maven process * @param logger A logging method such as {@code .info()} or {@code .error()} diff --git a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginSettings.java b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginSettings.java index 896c84fc7..b79947a90 100644 --- a/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginSettings.java +++ b/plugin/src/main/java/com/exadel/aem/toolkit/plugin/maven/PluginSettings.java @@ -13,10 +13,12 @@ */ package com.exadel.aem.toolkit.plugin.maven; -import java.util.ArrayList; -import java.util.List; +import java.util.LinkedHashSet; +import java.util.Set; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; import com.exadel.aem.toolkit.plugin.exceptions.handlers.ExceptionHandlers; import com.exadel.aem.toolkit.plugin.utils.ClassUtil; @@ -29,9 +31,12 @@ public class PluginSettings { public static final PluginSettings EMPTY = new PluginSettings(); + private static final int HASH_SEED = 7; + private static final int HASH_FACTOR = 31; + private String defaultPathBase; - private List referenceEntries; + private Set referenceEntries; private String terminateOn; @@ -78,7 +83,7 @@ public String getTerminateOnRule() { * Initializes a {@code Builder} instance used to populate a {@link PluginSettings} object with values * @return {@code Builder} object */ - public static Builder builder() { + static Builder builder() { return new Builder(); } @@ -87,7 +92,7 @@ public static Builder builder() { */ static class Builder { private String pathBase; - private List referenceEntries; + private Set referenceEntries; private String terminateOn; /** @@ -110,12 +115,13 @@ public Builder defaultPathBase(String value) { * @return This instance * @see PluginSettings#getPathBase(Class) */ + @SuppressWarnings("UnusedReturnValue") public Builder referenceEntry(String pathValue, String referenceValue) { if (StringUtils.isAnyBlank(pathValue, referenceValue)) { return this; } if (referenceEntries == null) { - referenceEntries = new ArrayList<>(); + referenceEntries = new LinkedHashSet<>(); } referenceEntries.add(new ReferenceEntry(pathValue, referenceValue)); return this; @@ -179,5 +185,31 @@ public String getPathBase() { public boolean matches(Class component) { return ClassUtil.matchesReference(component, referenceBase); } + + /** + * Compares the current instance with another object + * @param other Another object + * @return True if the objects are equal, false otherwise + */ + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } + if (other == null || getClass() != other.getClass()) { + return false; + } + ReferenceEntry that = (ReferenceEntry) other; + return new EqualsBuilder().append(pathBase, that.pathBase).append(referenceBase, that.referenceBase).isEquals(); + } + + /** + * Calculates the hash code of the current instance + * @return Integer value + */ + @Override + public int hashCode() { + return new HashCodeBuilder(HASH_SEED, HASH_FACTOR).append(pathBase).append(referenceBase).toHashCode(); + } } }