Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ public interface DependencyManagementInjector {
/**
* Merges default values from the dependency management section of the given model into itself.
*
* @param model The model into which to merge the values specified by its dependency management sections, must not
* be <code>null</code>.
* @param model The model to read from, must not be {@code null}.
* @param builder The model builder to write changes to, must not be {@code null}.
* @param request The model building request that holds further settings, must not be {@code null}.
* @param problems The container used to collect problems that were encountered, must not be {@code null}.
*/
Model injectManagement(Model model, ModelBuilderRequest request, ModelProblemCollector problems);
void injectManagement(
Model model, Model.Builder builder, ModelBuilderRequest request, ModelProblemCollector problems);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ public interface InheritanceAssembler {
*
* @param child The child model into which to merge the values inherited from the parent, must not be
* <code>null</code>.
* @param builder The builder to write inherited values into, must not be {@code null}.
* @param parent The (read-only) parent model from which to inherit the values, may be <code>null</code>.
* @param request The model building request that holds further settings, must not be {@code null}.
* @param problems The container used to collect problems that were encountered, must not be {@code null}.
*/
Model assembleModelInheritance(
Model child, Model parent, ModelBuilderRequest request, ModelProblemCollector problems);
void assembleModelInheritance(
Model child,
Model.Builder builder,
Model parent,
ModelBuilderRequest request,
ModelProblemCollector problems);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,23 @@ public interface ModelNormalizer {
/**
* Merges duplicate elements like multiple declarations of the same build plugin in the specified model.
*
* @param model The model whose duplicate elements should be merged, must not be {@code null}.
* @param model The model to read from, must not be {@code null}.
* @param builder The model builder to write changes to, must not be {@code null}.
* @param request The model building request that holds further settings, must not be {@code null}.
* @param problems The container used to collect problems that were encountered, must not be {@code null}.
*/
Model mergeDuplicates(Model model, ModelBuilderRequest request, ModelProblemCollector problems);
void mergeDuplicates(
Model model, Model.Builder builder, ModelBuilderRequest request, ModelProblemCollector problems);

/**
* Sets default values in the specified model that for technical reasons cannot be set directly in the Modello
* definition.
*
* @param model The model in which to set the default values, must not be {@code null}.
* @param model The model to read from, must not be {@code null}.
* @param builder The model builder to write changes to, must not be {@code null}.
* @param request The model building request that holds further settings, must not be {@code null}.
* @param problems The container used to collect problems that were encountered, must not be {@code null}.
*/
Model injectDefaultValues(Model model, ModelBuilderRequest request, ModelProblemCollector problems);
void injectDefaultValues(
Model model, Model.Builder builder, ModelBuilderRequest request, ModelProblemCollector problems);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ public interface ModelPathTranslator {
* Resolves the well-known paths of the specified model against the given base directory. Paths within plugin
* configuration are not processed.
*
* @param model The model whose paths should be resolved, may be {@code null}.
* @param model The model to read from, may be {@code null}.
* @param builder The model builder to write changes to, must not be {@code null}.
* @param basedir The base directory to resolve relative paths against, may be {@code null}.
* @param request The model building request that holds further settings, must not be {@code null}.
* @since 4.0.0
*/
Model alignToBaseDirectory(Model model, Path basedir, ModelBuilderRequest request);
void alignToBaseDirectory(Model model, Model.Builder builder, Path basedir, ModelBuilderRequest request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ public interface ModelUrlNormalizer {
/**
* Normalizes the well-known URLs of the specified model.
*
* @param model The model whose URLs should be normalized, may be {@code null}.
* @param model The model to read from, may be {@code null}.
* @param builder The model builder to write changes to, must not be {@code null}.
* @param request The model building request that holds further settings, must not be {@code null}.
*/
Model normalize(Model model, ModelBuilderRequest request);
void normalize(Model model, Model.Builder builder, ModelBuilderRequest request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ public interface PluginConfigurationExpander {
/**
* Merges values from general build plugin configuration into the individual plugin executions of the given model.
*
* @param model The model whose build plugin configuration should be expanded, must not be <code>null</code>.
* @param model The model to read from, must not be {@code null}.
* @param builder The model builder to write changes to, must not be {@code null}.
* @param request The model building request that holds further settings, must not be {@code null}.
* @param problems The container used to collect problems that were encountered, must not be {@code null}.
*/
Model expandPluginConfiguration(Model model, ModelBuilderRequest request, ModelProblemCollector problems);
void expandPluginConfiguration(
Model model, Model.Builder builder, ModelBuilderRequest request, ModelProblemCollector problems);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ public interface PluginManagementInjector {
/**
* Merges default values from the plugin management section of the given model into itself.
*
* @param model The model into which to merge the values specified by its plugin management section, must not be
* <code>null</code>.
* @param model The model to read from, must not be {@code null}.
* @param builder The model builder to write changes to, must not be {@code null}.
* @param request The model building request that holds further settings, must not be {@code null}.
* @param problems The container used to collect problems that were encountered, must not be {@code null}.
*/
Model injectManagement(Model model, ModelBuilderRequest request, ModelProblemCollector problems);
void injectManagement(
Model model, Model.Builder builder, ModelBuilderRequest request, ModelProblemCollector problems);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,34 @@ public interface ProfileInjector {
* and model completely decoupled by injecting deep copies rather than the original objects from the profile.
*
* @param model The model into which to merge the values defined by the profile, must not be <code>null</code>.
* @param builder The builder to write profile-injected values into, must not be {@code null}.
* @param profile The (read-only) profile whose values should be injected, may be <code>null</code>.
* @param request The model building request that holds further settings, must not be {@code null}.
* @param problems The container used to collect problems that were encountered, must not be {@code null}.
*/
default Model injectProfile(
Model model, Profile profile, ModelBuilderRequest request, ModelProblemCollector problems) {
return injectProfiles(model, List.of(profile), request, problems);
default void injectProfile(
Model model,
Model.Builder builder,
Profile profile,
ModelBuilderRequest request,
ModelProblemCollector problems) {
injectProfiles(model, builder, List.of(profile), request, problems);
}

/**
* Merges values from the specified profile into the given model. Implementations are expected to keep the profile
* Merges values from the specified profiles into the given model. Implementations are expected to keep the profile
* and model completely decoupled by injecting deep copies rather than the original objects from the profile.
*
* @param model The model into which to merge the values defined by the profile, must not be <code>null</code>.
* @param model The model into which to merge the values defined by the profiles, must not be <code>null</code>.
* @param builder The builder to write profile-injected values into, must not be {@code null}.
* @param profiles The (read-only) list of profiles whose values should be injected, must not be <code>null</code>.
* @param request The model building request that holds further settings, must not be {@code null}.
* @param problems The container used to collect problems that were encountered, must not be {@code null}.
*/
Model injectProfiles(
Model model, List<Profile> profiles, ModelBuilderRequest request, ModelProblemCollector problems);
void injectProfiles(
Model model,
Model.Builder builder,
List<Profile> profiles,
ModelBuilderRequest request,
ModelProblemCollector problems);
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,11 @@ public DefaultModelUrlNormalizer(UrlNormalizer urlNormalizer) {
}

@Override
public Model normalize(Model model, ModelBuilderRequest request) {
public void normalize(Model model, Model.Builder builder, ModelBuilderRequest request) {
if (model == null) {
return null;
return;
}

Model.Builder builder = Model.newBuilder(model);
builder.url(normalize(model.getUrl()));

Scm scm = model.getScm();
Expand All @@ -70,8 +69,6 @@ public Model normalize(Model model, ModelBuilderRequest request) {
builder.distributionManagement(dist.withSite(site.withUrl(normalize(site.getUrl()))));
}
}

return builder.build();
}

private String normalize(String url) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,48 @@
public class DefaultPluginConfigurationExpander implements PluginConfigurationExpander {

@Override
public Model expandPluginConfiguration(Model model, ModelBuilderRequest request, ModelProblemCollector problems) {
public void expandPluginConfiguration(
Model model, Model.Builder builder, ModelBuilderRequest request, ModelProblemCollector problems) {
Build build = model.getBuild();
if (build != null) {
build = build.withPlugins(expandPlugin(build.getPlugins()));
List<Plugin> expandedPlugins = expandPlugin(build.getPlugins());
PluginManagement pluginManagement = build.getPluginManagement();
if (pluginManagement != null) {
build = build.withPluginManagement(
pluginManagement.withPlugins(expandPlugin(pluginManagement.getPlugins())));
List<Plugin> expandedMgmtPlugins =
pluginManagement != null ? expandPlugin(pluginManagement.getPlugins()) : null;

boolean buildModified = expandedPlugins != build.getPlugins()
|| (expandedMgmtPlugins != null && expandedMgmtPlugins != pluginManagement.getPlugins());

if (buildModified) {
Build.Builder bb = Build.newBuilder(build);
if (expandedPlugins != build.getPlugins()) {
bb.plugins(expandedPlugins);
}
if (expandedMgmtPlugins != null && expandedMgmtPlugins != pluginManagement.getPlugins()) {
bb.pluginManagement(PluginManagement.newBuilder(pluginManagement)
.plugins(expandedMgmtPlugins)
.build());
}
build = bb.build();
}
model = model.withBuild(build);
}
Reporting reporting = model.getReporting();
List<ReportPlugin> expandedReportPlugins = null;
if (reporting != null) {
expandReport(reporting.getPlugins());
expandedReportPlugins = expandReport(reporting.getPlugins());
}
boolean modelModified = build != model.getBuild()
|| (expandedReportPlugins != null && expandedReportPlugins != reporting.getPlugins());
if (modelModified) {
if (build != model.getBuild()) {
builder.build(build);
}
if (expandedReportPlugins != null && expandedReportPlugins != reporting.getPlugins()) {
builder.reporting(Reporting.newBuilder(reporting)
.plugins(expandedReportPlugins)
.build());
}
}
return model.withBuild(build);
}

private List<Plugin> expandPlugin(List<Plugin> oldPlugins) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,17 @@ public class DefaultDependencyManagementInjector implements DependencyManagement
private ManagementModelMerger merger = new ManagementModelMerger();

@Override
public Model injectManagement(Model model, ModelBuilderRequest request, ModelProblemCollector problems) {
return merger.mergeManagedDependencies(model);
public void injectManagement(
Model model, Model.Builder builder, ModelBuilderRequest request, ModelProblemCollector problems) {
merger.mergeManagedDependencies(model, builder);
}

/**
* ManagementModelMerger
*/
protected static class ManagementModelMerger extends MavenModelMerger {

public Model mergeManagedDependencies(Model model) {
public void mergeManagedDependencies(Model model, Model.Builder builder) {
DependencyManagement dependencyManagement = model.getDependencyManagement();
if (dependencyManagement != null) {
Map<Object, Dependency> dependencies = new HashMap<>();
Expand Down Expand Up @@ -86,10 +87,9 @@ public Model mergeManagedDependencies(Model model) {
Dependency dependency = dependencies.get(key);
newDeps.add(dependency);
}
return Model.newBuilder(model).dependencies(newDeps).build();
builder.dependencies(newDeps);
}
}
return model;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.apache.maven.api.services.ModelBuilderRequest;
import org.apache.maven.api.services.ModelProblemCollector;
import org.apache.maven.api.services.model.InheritanceAssembler;
import org.apache.maven.model.v4.MavenMerger;

/**
* Handles inheritance of model values.
Expand All @@ -52,25 +51,32 @@ public class DefaultInheritanceAssembler implements InheritanceAssembler {

private static final String CHILD_DIRECTORY_PROPERTY = "project.directory";

private final MavenMerger merger;
private final InheritanceModelMerger merger;

@Inject
public DefaultInheritanceAssembler() {
this(new InheritanceModelMerger());
}

public DefaultInheritanceAssembler(MavenMerger merger) {
public DefaultInheritanceAssembler(InheritanceModelMerger merger) {
this.merger = merger;
}

@Override
public Model assembleModelInheritance(
Model child, Model parent, ModelBuilderRequest request, ModelProblemCollector problems) {
public void assembleModelInheritance(
Model child,
Model.Builder builder,
Model parent,
ModelBuilderRequest request,
ModelProblemCollector problems) {
if (parent == null) {
return;
}
Map<Object, Object> hints = new HashMap<>();
String childPath = child.getProperties().getOrDefault(CHILD_DIRECTORY_PROPERTY, child.getArtifactId());
hints.put(CHILD_DIRECTORY, childPath);
hints.put(MavenModelMerger.CHILD_PATH_ADJUSTMENT, getChildPathAdjustment(child, parent, childPath));
return merger.merge(child, parent, false, hints);
merger.mergeIntoBuilder(builder, child, parent, false, hints);
}

/**
Expand Down Expand Up @@ -140,6 +146,15 @@ private String getChildPathAdjustment(Model child, Model parent, String childDir
*/
protected static class InheritanceModelMerger extends MavenModelMerger {

public void mergeIntoBuilder(
Model.Builder builder, Model target, Model source, boolean sourceDominant, Map<?, ?> hints) {
Map<Object, Object> context = new HashMap<>();
if (hints != null) {
context.putAll(hints);
}
mergeModel(builder, target, source, sourceDominant, context);
}

@Override
protected String extrapolateChildUrl(String parentUrl, boolean appendPath, Map<Object, Object> context) {
Object childDirectory = context.get(CHILD_DIRECTORY);
Expand Down
Loading
Loading