org.apache.maven.model.profile.DefaultProfileInjector Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of maven-model-builder Show documentation
Show all versions of maven-model-builder Show documentation
The effective model builder, with inheritance, profile activation, interpolation, ...
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.model.profile;
import javax.inject.Named;
import javax.inject.Singleton;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.maven.api.model.Build;
import org.apache.maven.api.model.BuildBase;
import org.apache.maven.api.model.Model;
import org.apache.maven.api.model.ModelBase;
import org.apache.maven.api.model.Plugin;
import org.apache.maven.api.model.PluginContainer;
import org.apache.maven.api.model.PluginExecution;
import org.apache.maven.api.model.Profile;
import org.apache.maven.api.model.ReportPlugin;
import org.apache.maven.api.model.ReportSet;
import org.apache.maven.api.model.Reporting;
import org.apache.maven.model.building.ModelBuildingRequest;
import org.apache.maven.model.building.ModelProblemCollector;
import org.apache.maven.model.merge.MavenModelMerger;
/**
* Handles profile injection into the model.
*
*/
@Named
@Singleton
@SuppressWarnings({"checkstyle:methodname"})
public class DefaultProfileInjector implements ProfileInjector {
private static final Map, Model>> CACHE = Collections.synchronizedMap(new WeakHashMap<>());
// In order for the weak hash map to work correctly, we must not hold any reference to
// the model used as the key. So we use a dummy model as a placeholder to indicate that
// we want to store the model used as they key.
private static final Model KEY = Model.newInstance();
private ProfileModelMerger merger = new ProfileModelMerger();
@Override
public void injectProfile(
org.apache.maven.model.Model model,
org.apache.maven.model.Profile profile,
ModelBuildingRequest request,
ModelProblemCollector problems) {
model.update(
injectProfile(model.getDelegate(), profile != null ? profile.getDelegate() : null, request, problems));
}
@Override
public Model injectProfile(
Model model, Profile profile, ModelBuildingRequest request, ModelProblemCollector problems) {
return injectProfiles(model, Collections.singletonList(profile), request, problems);
}
@Override
public Model injectProfiles(
Model model, List profiles, ModelBuildingRequest request, ModelProblemCollector problems) {
Model result = CACHE.computeIfAbsent(model, k -> new ConcurrentHashMap<>())
.computeIfAbsent(profiles, l -> doInjectProfiles(model, profiles));
return result == KEY ? model : result;
}
private Model doInjectProfiles(Model model, List profiles) {
Model orgModel = model;
for (Profile profile : profiles) {
if (profile != null) {
Model.Builder builder = Model.newBuilder(model);
merger.mergeModelBase(builder, model, profile);
if (profile.getBuild() != null) {
Build build = model.getBuild() != null ? model.getBuild() : Build.newInstance();
Build.Builder bbuilder = Build.newBuilder(build);
merger.mergeBuildBase(bbuilder, build, profile.getBuild());
builder.build(bbuilder.build());
}
model = builder.build();
}
}
return model == orgModel ? KEY : model;
}
/**
* ProfileModelMerger
*/
protected static class ProfileModelMerger extends MavenModelMerger {
public void mergeModelBase(ModelBase.Builder builder, ModelBase target, ModelBase source) {
mergeModelBase(builder, target, source, true, Collections.emptyMap());
}
public void mergeBuildBase(BuildBase.Builder builder, BuildBase target, BuildBase source) {
mergeBuildBase(builder, target, source, true, Collections.emptyMap());
}
@Override
protected void mergePluginContainer_Plugins(
PluginContainer.Builder builder,
PluginContainer target,
PluginContainer source,
boolean sourceDominant,
Map