Skip to content

Commit

Permalink
Merge pull request #544 from exadel-inc/bugfix/EAK-540
Browse files Browse the repository at this point in the history
[EAK-542] Added support for the config properties specified in <execution> sections
  • Loading branch information
smiakchilo authored Aug 21, 2024
2 parents 0fcceee + 3d7f6ac commit ca16285
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -218,7 +219,7 @@ private List<String> 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
Expand All @@ -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()}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<ReferenceEntry> referenceEntries;
private Set<ReferenceEntry> referenceEntries;

private String terminateOn;

Expand Down Expand Up @@ -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();
}

Expand All @@ -87,7 +92,7 @@ public static Builder builder() {
*/
static class Builder {
private String pathBase;
private List<ReferenceEntry> referenceEntries;
private Set<ReferenceEntry> referenceEntries;
private String terminateOn;

/**
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
}
}

0 comments on commit ca16285

Please sign in to comment.