Skip to content

Commit ff979c4

Browse files
committed
[MBUILDCACHE-73] Allow arbitrary expressions as additional reconcilation properties
1 parent 1eb321d commit ff979c4

File tree

6 files changed

+60
-8
lines changed

6 files changed

+60
-8
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ under the License.
345345
<model>src/main/mdo/build-cache-diff.mdo</model>
346346
<model>src/main/mdo/build-cache-report.mdo</model>
347347
</models>
348-
<version>1.2.0</version>
348+
<version>1.3.0</version>
349349
</configuration>
350350
<executions>
351351
<execution>

src/main/java/org/apache/maven/buildcache/BuildCacheMojosExecutionStrategy.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,8 @@ private boolean verifyCacheConsistency(
331331
final CompletedExecution completedExecution = cachedBuild.findMojoExecutionInfo(cacheCandidate);
332332
final String fullGoalName = cacheCandidate.getMojoDescriptor().getFullGoalName();
333333

334-
if (completedExecution != null && !isParamsMatched(project, cacheCandidate, mojo, completedExecution)) {
334+
if (completedExecution != null
335+
&& !isParamsMatched(project, session, cacheCandidate, mojo, completedExecution)) {
335336
LOGGER.info(
336337
"Mojo cached parameters mismatch with actual, forcing full project build. Mojo: {}",
337338
fullGoalName);
@@ -365,7 +366,11 @@ private boolean verifyCacheConsistency(
365366
}
366367

367368
boolean isParamsMatched(
368-
MavenProject project, MojoExecution mojoExecution, Mojo mojo, CompletedExecution completedExecution) {
369+
MavenProject project,
370+
MavenSession session,
371+
MojoExecution mojoExecution,
372+
Mojo mojo,
373+
CompletedExecution completedExecution) {
369374
List<TrackedProperty> tracked = cacheConfig.getTrackedProperties(mojoExecution);
370375

371376
for (TrackedProperty trackedProperty : tracked) {
@@ -378,7 +383,12 @@ boolean isParamsMatched(
378383

379384
final String currentValue;
380385
try {
381-
Object value = ReflectionUtils.getValueIncludingSuperclasses(propertyName, mojo);
386+
Object value;
387+
if (trackedProperty.getExpression() != null) {
388+
value = DtoUtils.interpolateExpression(trackedProperty.getExpression(), session, mojoExecution);
389+
} else {
390+
value = ReflectionUtils.getValueIncludingSuperclasses(propertyName, mojo);
391+
}
382392
Path baseDirPath = project.getBasedir().toPath();
383393
currentValue = DtoUtils.normalizeValue(value, baseDirPath);
384394
} catch (IllegalAccessException e) {

src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,19 @@ private void recordMojoProperties(CompletedExecution execution, MojoExecutionEve
730730
}
731731
}
732732
}
733+
// add properties with expressions
734+
for (TrackedProperty trackedProperty : trackedProperties) {
735+
if (trackedProperty.getExpression() != null) {
736+
String propertyName = trackedProperty.getPropertyName();
737+
if (!isExcluded(propertyName, logAll, noLogProperties, forceLogProperties)) {
738+
Object value = DtoUtils.interpolateExpression(
739+
trackedProperty.getExpression(),
740+
executionEvent.getSession(),
741+
executionEvent.getExecution());
742+
DtoUtils.addProperty(execution, propertyName, value, baseDirPath, true);
743+
}
744+
}
745+
}
733746
}
734747

735748
private static Method getGetter(String fieldName, Class<?> clazz) {

src/main/java/org/apache/maven/buildcache/xml/DtoUtils.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232
import org.apache.maven.buildcache.xml.build.DigestItem;
3333
import org.apache.maven.buildcache.xml.build.PropertyValue;
3434
import org.apache.maven.buildcache.xml.config.TrackedProperty;
35+
import org.apache.maven.execution.MavenSession;
3536
import org.apache.maven.model.Dependency;
37+
import org.apache.maven.plugin.MojoExecution;
38+
import org.apache.maven.plugin.PluginParameterExpressionEvaluator;
3639
import org.slf4j.Logger;
3740
import org.slf4j.LoggerFactory;
3841

@@ -214,4 +217,14 @@ private static String normalizedPath(Path path, Path baseDirPath) {
214217
}
215218
return normalizedPath;
216219
}
220+
221+
public static Object interpolateExpression(String expression, MavenSession session, MojoExecution execution) {
222+
try {
223+
PluginParameterExpressionEvaluator evaluator = new PluginParameterExpressionEvaluator(session, execution);
224+
return evaluator.evaluate(expression);
225+
} catch (Exception e) {
226+
LOGGER.warn("Cannot interpolate expression '{}': {}", expression, e.getMessage(), e);
227+
return expression; // return the expression as is if interpolation fails
228+
}
229+
}
217230
}

src/main/mdo/build-cache-config.mdo

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,6 +1441,13 @@ under the License.
14411441
</xs:documentation>
14421442
</xs:annotation>
14431443
</xs:attribute>
1444+
<xs:attribute name="expression" type="xs:string">
1445+
<xs:annotation>
1446+
<xs:documentation>
1447+
Interpolated expression to use as cached value.
1448+
</xs:documentation>
1449+
</xs:annotation>
1450+
</xs:attribute>
14441451
</xs:extension>
14451452
</xs:simpleContent>
14461453
</xs:complexType>
@@ -1465,6 +1472,11 @@ under the License.
14651472
<name>defaultValue</name>
14661473
<type>String</type>
14671474
</field>
1475+
<field xml.attribute="true">
1476+
<name>expression</name>
1477+
<type>String</type>
1478+
<description>Interpolated expression to use as cached value.</description>
1479+
</field>
14681480
</fields>
14691481
</class>
14701482
</classes>
@@ -1497,3 +1509,4 @@ under the License.
14971509
-->
14981510

14991511
</model>
1512+

src/test/java/org/apache/maven/buildcache/BuildCacheMojosExecutionStrategyTest.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.maven.buildcache.xml.build.CompletedExecution;
3131
import org.apache.maven.buildcache.xml.build.PropertyValue;
3232
import org.apache.maven.buildcache.xml.config.TrackedProperty;
33+
import org.apache.maven.execution.MavenSession;
3334
import org.apache.maven.execution.scope.internal.MojoExecutionScope;
3435
import org.apache.maven.plugin.MavenPluginManager;
3536
import org.apache.maven.plugin.MojoExecution;
@@ -54,6 +55,7 @@ class ParametersMatchingTest {
5455
private MojoExecution executionMock;
5556
private CompletedExecution cacheRecordMock;
5657
private CacheConfig cacheConfigMock;
58+
private MavenSession sessionMock;
5759

5860
@BeforeEach
5961
void setUp() {
@@ -69,6 +71,7 @@ void setUp() {
6971
projectMock = mock(MavenProject.class);
7072
executionMock = mock(MojoExecution.class);
7173
cacheRecordMock = mock(CompletedExecution.class);
74+
sessionMock = mock(MavenSession.class);
7275
}
7376

7477
@Test
@@ -106,7 +109,7 @@ void testBasicParamsMatching() {
106109
Arrays.asList("a", "b", "c"),
107110
new String[] {"c", "d", "e"});
108111

109-
assertTrue(strategy.isParamsMatched(projectMock, executionMock, testMojo, cacheRecordMock));
112+
assertTrue(strategy.isParamsMatched(projectMock, sessionMock, executionMock, testMojo, cacheRecordMock));
110113
}
111114

112115
@Test
@@ -133,7 +136,7 @@ void testSkipValue() {
133136
testMojo.setAnyObject("true");
134137

135138
assertTrue(
136-
strategy.isParamsMatched(projectMock, executionMock, testMojo, cacheRecordMock),
139+
strategy.isParamsMatched(projectMock, sessionMock, executionMock, testMojo, cacheRecordMock),
137140
"If property set to 'skipValue' mismatch could be ignored because cached build"
138141
+ " is more complete than requested build");
139142
}
@@ -162,7 +165,7 @@ void testDefaultValue() {
162165
testMojo.setAnyObject("defaultValue");
163166

164167
assertTrue(
165-
strategy.isParamsMatched(projectMock, executionMock, testMojo, cacheRecordMock),
168+
strategy.isParamsMatched(projectMock, sessionMock, executionMock, testMojo, cacheRecordMock),
166169
"If property has defaultValue it must be matched even if cache record has no this field");
167170
}
168171

@@ -188,7 +191,7 @@ void testMismatch() {
188191
TestMojo testMojo = new TestMojo();
189192
testMojo.setAnyObject("2");
190193

191-
assertFalse(strategy.isParamsMatched(projectMock, executionMock, testMojo, cacheRecordMock));
194+
assertFalse(strategy.isParamsMatched(projectMock, sessionMock, executionMock, testMojo, cacheRecordMock));
192195
}
193196

194197
@NotNull

0 commit comments

Comments
 (0)