Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -30,15 +30,15 @@
*/
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.
Expand All @@ -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;
}

Expand All @@ -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() {
Expand All @@ -84,10 +86,6 @@ public MavenProject getProject() {
}

public List<ModelProblem> getProblems() {
if (problems == null) {
problems = new ArrayList<>();
}

return problems;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@
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) {
gnodet marked this conversation as resolved.
Show resolved Hide resolved
super(false);
this.mavenBuildTimestamp = new MavenBuildTimestamp(startTime, properties);
this.startTime = startTime;
this.properties = properties;
mthmulders marked this conversation as resolved.
Show resolved Hide resolved
}

@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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
*/
package org.apache.maven.model.profile;

import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toMap;

import java.io.File;
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.
Expand Down Expand Up @@ -58,12 +58,7 @@ public List<String> getActiveProfileIds() {
* @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;
}

Expand All @@ -79,12 +74,7 @@ public List<String> getInactiveProfileIds() {
* @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;
}

Expand All @@ -102,13 +92,7 @@ public Map<String, String> getSystemProperties() {
*/
@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));
}

/**
Expand All @@ -119,12 +103,7 @@ public DefaultProfileActivationContext setSystemProperties(Properties systemProp
* @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;
}

Expand All @@ -143,13 +122,7 @@ public Map<String, String> getUserProperties() {
*/
@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));
}

/**
Expand All @@ -161,12 +134,7 @@ public DefaultProfileActivationContext setUserProperties(Properties userProperti
* @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;
}

Expand Down Expand Up @@ -194,15 +162,30 @@ public Map<String, String> getProjectProperties() {
}

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);
mthmulders marked this conversation as resolved.
Show resolved Hide resolved

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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -96,19 +96,17 @@ private boolean isActive(Profile profile, ProfileActivationContext context, Mode
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;
Expand Down