[MNG-7617] Small optimisations and cleanup in the project/model building (#816)

* Clean a bit DefaultProfileActivationContext
* Cleanup DefaultProjectBuildingResult
* Cache the injected list to avoid repetitive lookups
* Lazily compute the MavenBuildTimestamp
* Use a single loop to select active profiles
This commit is contained in:
Guillaume Nodet 2022-12-02 14:58:56 +01:00 committed by GitHub
parent f0cc176b33
commit 249c0fe0f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 75 deletions

View File

@ -19,7 +19,7 @@
package org.apache.maven.project;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.maven.model.building.ModelProblem;
@ -30,15 +30,15 @@ import org.apache.maven.model.building.ModelProblem;
*/
class DefaultProjectBuildingResult implements ProjectBuildingResult {
private String projectId;
private final String projectId;
private File pomFile;
private final File pomFile;
private MavenProject project;
private final MavenProject project;
private List<ModelProblem> problems;
private final List<ModelProblem> problems;
private DependencyResolutionResult dependencyResolutionResult;
private final DependencyResolutionResult dependencyResolutionResult;
/**
* Creates a new result with the specified contents.
@ -54,7 +54,7 @@ class DefaultProjectBuildingResult implements ProjectBuildingResult {
: "";
this.pomFile = (project != null) ? project.getFile() : null;
this.project = project;
this.problems = problems;
this.problems = problems != null ? problems : Collections.emptyList();
this.dependencyResolutionResult = dependencyResolutionResult;
}
@ -68,7 +68,9 @@ class DefaultProjectBuildingResult implements ProjectBuildingResult {
DefaultProjectBuildingResult(String projectId, File pomFile, List<ModelProblem> problems) {
this.projectId = (projectId != null) ? projectId : "";
this.pomFile = pomFile;
this.problems = problems;
this.project = null;
this.problems = problems != null ? problems : Collections.emptyList();
this.dependencyResolutionResult = null;
}
public String getProjectId() {
@ -84,10 +86,6 @@ class DefaultProjectBuildingResult implements ProjectBuildingResult {
}
public List<ModelProblem> getProblems() {
if (problems == null) {
problems = new ArrayList<>();
}
return problems;
}

View File

@ -23,17 +23,19 @@ import java.util.Map;
import org.codehaus.plexus.interpolation.AbstractValueSource;
class BuildTimestampValueSource extends AbstractValueSource {
private final MavenBuildTimestamp mavenBuildTimestamp;
private final Date startTime;
private final Map<String, String> properties;
BuildTimestampValueSource(Date startTime, Map<String, String> properties) {
super(false);
this.mavenBuildTimestamp = new MavenBuildTimestamp(startTime, properties);
this.startTime = startTime;
this.properties = properties;
}
@Override
public Object getValue(String expression) {
if ("build.timestamp".equals(expression) || "maven.build.timestamp".equals(expression)) {
return mavenBuildTimestamp.formattedTimestamp();
return new MavenBuildTimestamp(startTime, properties).formattedTimestamp();
}
return null;
}

View File

@ -18,7 +18,6 @@
*/
package org.apache.maven.model.profile;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toMap;
import java.io.File;
@ -26,6 +25,7 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;
/**
* Describes the environmental context used to determine the activation status of profiles.
@ -58,12 +58,7 @@ public class DefaultProfileActivationContext implements ProfileActivationContext
* @return This context, never {@code null}.
*/
public DefaultProfileActivationContext setActiveProfileIds(List<String> activeProfileIds) {
if (activeProfileIds != null) {
this.activeProfileIds = Collections.unmodifiableList(activeProfileIds);
} else {
this.activeProfileIds = Collections.emptyList();
}
this.activeProfileIds = unmodifiable(activeProfileIds);
return this;
}
@ -79,12 +74,7 @@ public class DefaultProfileActivationContext implements ProfileActivationContext
* @return This context, never {@code null}.
*/
public DefaultProfileActivationContext setInactiveProfileIds(List<String> inactiveProfileIds) {
if (inactiveProfileIds != null) {
this.inactiveProfileIds = Collections.unmodifiableList(inactiveProfileIds);
} else {
this.inactiveProfileIds = Collections.emptyList();
}
this.inactiveProfileIds = unmodifiable(inactiveProfileIds);
return this;
}
@ -102,13 +92,7 @@ public class DefaultProfileActivationContext implements ProfileActivationContext
*/
@SuppressWarnings("unchecked")
public DefaultProfileActivationContext setSystemProperties(Properties systemProperties) {
if (systemProperties != null) {
this.systemProperties = Collections.unmodifiableMap((Map) systemProperties);
} else {
this.systemProperties = Collections.emptyMap();
}
return this;
return setSystemProperties(toMap(systemProperties));
}
/**
@ -119,12 +103,7 @@ public class DefaultProfileActivationContext implements ProfileActivationContext
* @return This context, never {@code null}.
*/
public DefaultProfileActivationContext setSystemProperties(Map<String, String> systemProperties) {
if (systemProperties != null) {
this.systemProperties = Collections.unmodifiableMap(systemProperties);
} else {
this.systemProperties = Collections.emptyMap();
}
this.systemProperties = unmodifiable(systemProperties);
return this;
}
@ -143,13 +122,7 @@ public class DefaultProfileActivationContext implements ProfileActivationContext
*/
@SuppressWarnings("unchecked")
public DefaultProfileActivationContext setUserProperties(Properties userProperties) {
if (userProperties != null) {
this.userProperties = Collections.unmodifiableMap((Map) userProperties);
} else {
this.userProperties = Collections.emptyMap();
}
return this;
return setUserProperties(toMap(userProperties));
}
/**
@ -161,12 +134,7 @@ public class DefaultProfileActivationContext implements ProfileActivationContext
* @return This context, never {@code null}.
*/
public DefaultProfileActivationContext setUserProperties(Map<String, String> userProperties) {
if (userProperties != null) {
this.userProperties = Collections.unmodifiableMap(userProperties);
} else {
this.userProperties = Collections.emptyMap();
}
this.userProperties = unmodifiable(userProperties);
return this;
}
@ -194,15 +162,30 @@ public class DefaultProfileActivationContext implements ProfileActivationContext
}
public DefaultProfileActivationContext setProjectProperties(Properties projectProperties) {
if (projectProperties != null) {
this.projectProperties = projectProperties.entrySet().stream()
.collect(collectingAndThen(
toMap(k -> String.valueOf(k.getKey()), v -> String.valueOf(v)),
Collections::unmodifiableMap));
} else {
this.projectProperties = Collections.emptyMap();
}
return setProjectProperties(toMap(projectProperties));
}
public DefaultProfileActivationContext setProjectProperties(Map<String, String> projectProperties) {
this.projectProperties = unmodifiable(projectProperties);
return this;
}
private static List<String> unmodifiable(List<String> list) {
return list != null ? Collections.unmodifiableList(list) : Collections.emptyList();
}
private static Map<String, String> unmodifiable(Map<String, String> map) {
return map != null ? Collections.unmodifiableMap(map) : Collections.emptyMap();
}
private static Map<String, String> toMap(Properties properties) {
if (properties != null && !properties.isEmpty()) {
return properties.entrySet().stream()
.collect(Collectors.toMap(e -> String.valueOf(e.getKey()), e -> String.valueOf(e.getValue())));
} else {
return null;
}
}
}

View File

@ -46,7 +46,7 @@ public class DefaultProfileSelector implements ProfileSelector {
@Inject
public DefaultProfileSelector(List<ProfileActivator> activators) {
this.activators = activators;
this.activators = new ArrayList<>(activators);
}
public DefaultProfileSelector addProfileActivator(ProfileActivator profileActivator) {
@ -96,19 +96,17 @@ public class DefaultProfileSelector implements ProfileSelector {
for (ProfileActivator activator : activators) {
if (activator.presentInConfig(profile, context, problems)) {
isActive = true;
}
}
for (ProfileActivator activator : activators) {
try {
if (activator.presentInConfig(profile, context, problems)) {
isActive &= activator.isActive(profile, context, problems);
try {
if (!activator.isActive(profile, context, problems)) {
return false;
}
} catch (RuntimeException e) {
problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE)
.setMessage("Failed to determine activation for profile " + profile.getId())
.setLocation(profile.getLocation(""))
.setException(e));
return false;
}
} catch (RuntimeException e) {
problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE)
.setMessage("Failed to determine activation for profile " + profile.getId())
.setLocation(profile.getLocation(""))
.setException(e));
return false;
}
}
return isActive;