Skip to content
Draft
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 @@ -503,6 +503,14 @@ boolean isParamsMatched(
CompletedExecution completedExecution) {
List<TrackedProperty> tracked = cacheConfig.getTrackedProperties(mojoExecution);

if (mojoExecution.getPlugin() != null) {
LOGGER.debug(
"Checking parameter match for {}:{} - tracking {} properties",
mojoExecution.getPlugin().getArtifactId(),
mojoExecution.getGoal(),
tracked.size());
}

for (TrackedProperty trackedProperty : tracked) {
final String propertyName = trackedProperty.getPropertyName();

Expand All @@ -511,7 +519,7 @@ boolean isParamsMatched(
expectedValue = trackedProperty.getDefaultValue() != null ? trackedProperty.getDefaultValue() : "null";
}

final String currentValue;
String currentValue;
try {
Object value;
if (trackedProperty.getExpression() != null) {
Expand All @@ -524,8 +532,14 @@ boolean isParamsMatched(
} catch (IllegalAccessException e) {
LOGGER.error("Cannot extract plugin property {} from mojo {}", propertyName, mojo, e);
return false;
} catch (Exception e) {
LOGGER.warn("Cannot extract plugin property {} from mojo {}", propertyName, mojo, e);
return false;
}

LOGGER.debug(
"Checking property '{}': expected='{}', actual='{}'", propertyName, expectedValue, currentValue);

if (!Strings.CS.equals(currentValue, expectedValue)) {
if (!Strings.CS.equals(currentValue, trackedProperty.getSkipValue())) {
LOGGER.info(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@
project.addLifecyclePhase("package");
}
}
restoredAttachedArtifacts.forEach(project::addAttachedArtifact);

Check warning on line 489 in src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java

View workflow job for this annotation

GitHub Actions / Verify / ubuntu-latest jdk-17-zulu 3.10.0-rc-1

addAttachedArtifact(org.apache.maven.artifact.Artifact) in org.apache.maven.project.MavenProject has been deprecated

Check warning on line 489 in src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java

View workflow job for this annotation

GitHub Actions / Verify / ubuntu-latest jdk-17-zulu 3.10.0-rc-1

addAttachedArtifact(org.apache.maven.artifact.Artifact) in org.apache.maven.project.MavenProject has been deprecated
restorationReport.setSuccess(true);
} catch (Exception e) {
LOGGER.debug("Cannot restore cache, continuing with normal build.", e);
Expand Down Expand Up @@ -943,7 +943,11 @@
try {
Field field = ReflectionUtils.getFieldByNameIncludingSuperclasses(propertyName, mojo.getClass());
if (field != null) {
final Object value = ReflectionUtils.getValueIncludingSuperclasses(propertyName, mojo);
final Object value = normalizeMojoProperty(
propertyName,
ReflectionUtils.getValueIncludingSuperclasses(propertyName, mojo),
mojoExecution,
executionEvent.getProject());
CacheUtils.addProperty(execution, propertyName, value, baseDirPath, tracked);
continue;
}
Expand Down Expand Up @@ -985,6 +989,30 @@
}
}

private Object normalizeMojoProperty(
String propertyName, Object value, MojoExecution mojoExecution, MavenProject project) {
if (!(value instanceof List)
|| !"compilerArgs".equals(propertyName)
|| mojoExecution.getPlugin() == null
|| !"maven-compiler-plugin".equals(mojoExecution.getPlugin().getArtifactId())) {
return value;
}

List<?> args = (List<?>) value;
List<Object> normalized = new ArrayList<>(args.size());
for (int i = 0; i < args.size(); i++) {
Object arg = args.get(i);
if ("--module-version".equals(arg)
&& i + 1 < args.size()
&& Objects.equals(project.getVersion(), args.get(i + 1))) {
i++;
} else {
normalized.add(arg);
}
}
return normalized.isEmpty() ? null : normalized;
}

private static Method getGetter(String fieldName, Class<?> clazz) {
String getterMethodName = "get" + org.codehaus.plexus.util.StringUtils.capitalizeFirstLetter(fieldName);
Method[] methods = clazz.getMethods();
Expand Down
179 changes: 167 additions & 12 deletions src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import org.apache.maven.buildcache.xml.config.Exclude;
import org.apache.maven.buildcache.xml.config.Executables;
import org.apache.maven.buildcache.xml.config.ExecutionConfigurationScan;
import org.apache.maven.buildcache.xml.config.ExecutionControl;
import org.apache.maven.buildcache.xml.config.ExecutionIdsList;
import org.apache.maven.buildcache.xml.config.GoalReconciliation;
import org.apache.maven.buildcache.xml.config.GoalsList;
Expand Down Expand Up @@ -122,6 +121,7 @@ public class CacheConfigImpl implements org.apache.maven.buildcache.xml.CacheCon
private final XmlService xmlService;
private final Provider<MavenSession> providerSession;
private final RuntimeInformation rtInfo;
private final PluginParameterLoader parameterLoader;

private volatile CacheState state;
private CacheConfig cacheConfig;
Expand All @@ -133,6 +133,7 @@ public CacheConfigImpl(XmlService xmlService, Provider<MavenSession> providerSes
this.xmlService = xmlService;
this.providerSession = providerSession;
this.rtInfo = rtInfo;
this.parameterLoader = new PluginParameterLoader();
}

@Nonnull
Expand Down Expand Up @@ -252,27 +253,181 @@ public boolean isLogAllProperties(MojoExecution mojoExecution) {
}

private GoalReconciliation findReconciliationConfig(MojoExecution mojoExecution) {
if (cacheConfig.getExecutionControl() == null) {
if (mojoExecution == null) {
return null;
}

final ExecutionControl executionControl = cacheConfig.getExecutionControl();
if (executionControl.getReconcile() == null) {
final String goal = mojoExecution.getGoal();
final Plugin plugin = mojoExecution.getPlugin();

if (plugin == null) {
return null;
}

final List<GoalReconciliation> reconciliation =
executionControl.getReconcile().getPlugins();
// First check explicit configuration
if (cacheConfig.getExecutionControl() != null
&& cacheConfig.getExecutionControl().getReconcile() != null) {
List<GoalReconciliation> explicitConfigs =
cacheConfig.getExecutionControl().getReconcile().getPlugins();
for (GoalReconciliation config : explicitConfigs) {
if (isPluginMatch(plugin, config) && Strings.CS.equals(goal, config.getGoal())) {
// Validate explicit config against parameter definitions (with version)
validateReconciliationConfig(config, plugin);
return config;
}
}
}

for (GoalReconciliation goalReconciliationConfig : reconciliation) {
final String goal = mojoExecution.getGoal();
// Auto-generate from parameter definitions (track all cache-key parameters)
GoalReconciliation autoGenerated = generateReconciliationFromParameters(plugin, goal);
if (autoGenerated != null) {
LOGGER.debug(
"Auto-generated reconciliation config for {}:{} with {} cache-key parameters",
plugin.getArtifactId(),
goal,
autoGenerated.getReconciles() != null
? autoGenerated.getReconciles().size()
: 0);
}
return autoGenerated;
}

if (isPluginMatch(mojoExecution.getPlugin(), goalReconciliationConfig)
&& Strings.CS.equals(goal, goalReconciliationConfig.getGoal())) {
return goalReconciliationConfig;
/**
* Validates a single reconciliation config against plugin parameter definitions.
* Uses plugin version to load the appropriate parameter definition.
*/
private void validateReconciliationConfig(GoalReconciliation config, Plugin plugin) {
String artifactId = config.getArtifactId();
String goal = config.getGoal();
String pluginVersion = plugin.getVersion();

// Load parameter definition for this plugin with version
PluginParameterDefinition pluginDef = parameterLoader.load(artifactId, pluginVersion);

if (pluginDef == null) {
LOGGER.warn(
"No parameter definition found for plugin {}:{} version {}. "
+ "Cannot validate reconciliation configuration. "
+ "Consider adding a parameter definition file to plugin-parameters/{}.xml",
artifactId,
goal,
pluginVersion != null ? pluginVersion : "unknown",
artifactId);
return;
}

// Get goal definition
PluginParameterDefinition.GoalParameterDefinition goalDef = pluginDef.getGoal(goal);
if (goalDef == null) {
LOGGER.warn(
"Goal '{}' not found in parameter definition for plugin {} version {}. "
+ "Cannot validate reconciliation configuration.",
goal,
artifactId,
pluginVersion != null ? pluginVersion : "unknown");
return;
}

// Validate each tracked property
List<TrackedProperty> properties = config.getReconciles();

for (TrackedProperty property : properties) {
String propertyName = property.getPropertyName();

if (!goalDef.hasParameter(propertyName)) {
LOGGER.warn(
"Unknown parameter '{}' in reconciliation config for {}:{} version {}. "
+ "This may indicate a plugin version mismatch or renamed parameter. "
+ "Consider updating parameter definition or removing from reconciliation.",
propertyName,
artifactId,
goal,
pluginVersion != null ? pluginVersion : "unknown");
} else {
PluginParameterDefinition.ParameterDefinition paramDef = goalDef.getParameter(propertyName);
if (!paramDef.isCacheKey()) {
LOGGER.warn(
"Parameter '{}' in reconciliation config for {}:{} is not marked as a cache key. "
+ "Changing it will not invalidate the cache according to the plugin definition.",
propertyName,
artifactId,
goal);
}
}
}
return null;
}

/**
* Auto-generates a reconciliation config by tracking all cache-key parameters
* from the plugin parameter definition.
* This provides automatic default tracking for any plugin with parameter definitions.
*
* @param plugin The plugin to generate config for
* @param goal The goal name
* @return Auto-generated config tracking all cache-key parameters, or null if no parameter definition exists
*/
private GoalReconciliation generateReconciliationFromParameters(Plugin plugin, String goal) {
String artifactId = plugin.getArtifactId();
String pluginVersion = plugin.getVersion();

LOGGER.debug(
"Attempting to auto-generate reconciliation config for {}:{} version {}",
artifactId,
goal,
pluginVersion);

// Load parameter definition for this plugin
PluginParameterDefinition pluginDef = parameterLoader.load(artifactId, pluginVersion);
if (pluginDef == null) {
LOGGER.debug("No parameter definition found for {}:{}", artifactId, pluginVersion);
return null;
}

// Get goal definition
PluginParameterDefinition.GoalParameterDefinition goalDef = pluginDef.getGoal(goal);
if (goalDef == null) {
LOGGER.debug("No goal definition found for goal '{}' in plugin {}", goal, artifactId);
return null;
}

LOGGER.debug(
"Found goal definition for {}:{} with {} total parameters",
artifactId,
goal,
goalDef.getParameters().size());

// Collect all cache-key parameters
List<TrackedProperty> cacheKeyProperties = new ArrayList<>();
for (PluginParameterDefinition.ParameterDefinition param :
goalDef.getParameters().values()) {
if (param.isCacheKey()) {
LOGGER.debug("Adding cache-key parameter '{}' to auto-generated config", param.getName());
TrackedProperty property = new TrackedProperty();
property.setPropertyName(param.getName());
cacheKeyProperties.add(property);
}
}

Comment thread
cowwoc marked this conversation as resolved.
// Only create config if there are cache-key parameters to track
if (cacheKeyProperties.isEmpty()) {
LOGGER.debug("No cache-key parameters found for {}:{}", artifactId, goal);
return null;
}

LOGGER.debug("Created auto-generated config with {} cache-key parameters", cacheKeyProperties.size());

// Create auto-generated reconciliation config
GoalReconciliation config = new GoalReconciliation();
config.setArtifactId(artifactId);
if (plugin.getGroupId() != null) {
config.setGroupId(plugin.getGroupId());
}
config.setGoal(goal);
for (TrackedProperty property : cacheKeyProperties) {
config.addReconcile(property);
}

return config;
}

@Nonnull
Expand Down
Loading
Loading