You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by gn...@apache.org on 2022/12/02 13:59:02 UTC
[maven] branch master updated: [MNG-7617] Small optimisations and cleanup in the project/model building (#816)
This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git
The following commit(s) were added to refs/heads/master by this push:
new 249c0fe0f [MNG-7617] Small optimisations and cleanup in the project/model building (#816)
249c0fe0f is described below
commit 249c0fe0f1273576531d65cb8f9239b87cab2196
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Fri Dec 2 14:58:56 2022 +0100
[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
---
.../project/DefaultProjectBuildingResult.java | 22 +++----
.../interpolation/BuildTimestampValueSource.java | 8 ++-
.../profile/DefaultProfileActivationContext.java | 77 +++++++++-------------
.../model/profile/DefaultProfileSelector.java | 24 ++++---
4 files changed, 56 insertions(+), 75 deletions(-)
diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingResult.java b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingResult.java
index 2fcaf451d..199237bf9 100644
--- a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingResult.java
+++ b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingResult.java
@@ -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;
}
diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/BuildTimestampValueSource.java b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/BuildTimestampValueSource.java
index 08014f725..77f3b832b 100644
--- a/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/BuildTimestampValueSource.java
+++ b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/BuildTimestampValueSource.java
@@ -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;
}
diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/profile/DefaultProfileActivationContext.java b/maven-model-builder/src/main/java/org/apache/maven/model/profile/DefaultProfileActivationContext.java
index bc62f4806..a27e7c71f 100644
--- a/maven-model-builder/src/main/java/org/apache/maven/model/profile/DefaultProfileActivationContext.java
+++ b/maven-model-builder/src/main/java/org/apache/maven/model/profile/DefaultProfileActivationContext.java
@@ -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;
+ }
+ }
}
diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/profile/DefaultProfileSelector.java b/maven-model-builder/src/main/java/org/apache/maven/model/profile/DefaultProfileSelector.java
index 980ccf2eb..47bbf0373 100644
--- a/maven-model-builder/src/main/java/org/apache/maven/model/profile/DefaultProfileSelector.java
+++ b/maven-model-builder/src/main/java/org/apache/maven/model/profile/DefaultProfileSelector.java
@@ -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;