Skip to content

Commit

Permalink
read-project-properties optimization - set all properties in one call
Browse files Browse the repository at this point in the history
In Maven 4 project model will be immutable
each change in project will cause rebuild it again
so minimize call counts for changing properties
  • Loading branch information
slawekjaranowski committed Oct 21, 2023
1 parent c816bf2 commit 8f4e6be
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/main/java/org/codehaus/mojo/properties/ReadPropertiesMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;

import org.apache.maven.plugin.AbstractMojo;
Expand Down Expand Up @@ -188,13 +191,17 @@ private void loadProperties(Resource resource) throws MojoExecutionException {
Properties properties = new Properties();
properties.load(stream);
Properties projectProperties = project.getProperties();
Map<String, String> newProperties = new HashMap<>();

for (String key : properties.stringPropertyNames()) {
String propertyName = effectivePrefix + key;
if (override || !projectProperties.containsKey(propertyName)) {
projectProperties.put(propertyName, properties.get(key));
newProperties.put(propertyName, properties.getProperty(key));
}
}
// change project properties at one call
projectProperties.putAll(newProperties);
getLog().info("Loading " + newProperties.size() + " properties from " + resource);
}
} catch (IOException e) {
throw new MojoExecutionException("Error reading properties from " + resource, e);
Expand All @@ -210,13 +217,28 @@ private void missing(Resource resource) throws MojoExecutionException {
}

private void resolveProperties() throws MojoExecutionException, MojoFailureException {
getLog().debug("resolve properties");
Properties environment = loadSystemEnvironmentPropertiesWhenDefined();
Properties projectProperties = project.getProperties();

for (Enumeration<?> n = projectProperties.propertyNames(); n.hasMoreElements(); ) {
String k = (String) n.nextElement();
projectProperties.setProperty(k, getPropertyValue(k, projectProperties, environment));
Map<String, String> newProperties = new HashMap<>();

for (String key : projectProperties.stringPropertyNames()) {
String newValue = getPropertyValue(key, projectProperties, environment);
String oldValue = projectProperties.getProperty(key);
if (!Objects.equals(newValue, oldValue)) {
newProperties.put(key, newValue);
}
}

if (!newProperties.isEmpty()) {
getLog().debug("resolve " + newProperties.size() + " properties");
// change project properties at one call
projectProperties.putAll(newProperties);
} else {
getLog().debug("all properties was resolved");
}
getLog().debug("resolve properties - done");
}

private Properties loadSystemEnvironmentPropertiesWhenDefined() throws MojoExecutionException {
Expand Down

0 comments on commit 8f4e6be

Please sign in to comment.