Skip to content

Commit 5fc82b1

Browse files
author
Christoph Läubrich
committed
[MNG-7407] Introduce a ModelVersionProcessor component to make CI
Friends Versions pluggable
1 parent a472975 commit 5fc82b1

File tree

10 files changed

+161
-39
lines changed

10 files changed

+161
-39
lines changed

maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilderFactory.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
import org.apache.maven.model.composition.DependencyManagementImporter;
2727
import org.apache.maven.model.inheritance.DefaultInheritanceAssembler;
2828
import org.apache.maven.model.inheritance.InheritanceAssembler;
29+
import org.apache.maven.model.interpolation.DefaultModelVersionProcessor;
2930
import org.apache.maven.model.interpolation.ModelInterpolator;
31+
import org.apache.maven.model.interpolation.ModelVersionProcessor;
3032
import org.apache.maven.model.interpolation.StringVisitorModelInterpolator;
3133
import org.apache.maven.model.io.DefaultModelReader;
3234
import org.apache.maven.model.io.ModelReader;
@@ -259,12 +261,19 @@ protected ModelInterpolator newModelInterpolator()
259261
{
260262
UrlNormalizer normalizer = newUrlNormalizer();
261263
PathTranslator pathTranslator = newPathTranslator();
262-
return new StringVisitorModelInterpolator( pathTranslator, normalizer );
264+
ModelVersionProcessor versionProcessor = newModelVersionPropertiesProcessor();
265+
return new StringVisitorModelInterpolator( pathTranslator, normalizer, versionProcessor );
266+
}
267+
268+
protected ModelVersionProcessor newModelVersionPropertiesProcessor()
269+
{
270+
return new DefaultModelVersionProcessor();
263271
}
264272

265273
protected ModelValidator newModelValidator()
266274
{
267-
return new DefaultModelValidator();
275+
ModelVersionProcessor processor = newModelVersionPropertiesProcessor();
276+
return new DefaultModelValidator( processor );
268277
}
269278

270279
protected ModelNormalizer newModelNormalizer()

maven-model-builder/src/main/java/org/apache/maven/model/interpolation/AbstractStringBasedModelInterpolator.java

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,6 @@
5252
public abstract class AbstractStringBasedModelInterpolator
5353
implements ModelInterpolator
5454
{
55-
public static final String SHA1_PROPERTY = "sha1";
56-
57-
public static final String CHANGELIST_PROPERTY = "changelist";
58-
59-
public static final String REVISION_PROPERTY = "revision";
60-
6155
private static final List<String> PROJECT_PREFIXES = Arrays.asList( "pom.", "project." );
6256

6357
private static final Collection<String> TRANSLATED_PATH_EXPRESSIONS;
@@ -82,14 +76,17 @@ public abstract class AbstractStringBasedModelInterpolator
8276
TRANSLATED_PATH_EXPRESSIONS = translatedPrefixes;
8377
}
8478

79+
private final ModelVersionProcessor versionProcessor;
8580
private final PathTranslator pathTranslator;
8681
private final UrlNormalizer urlNormalizer;
8782

8883
@Inject
89-
public AbstractStringBasedModelInterpolator( PathTranslator pathTranslator, UrlNormalizer urlNormalizer )
84+
public AbstractStringBasedModelInterpolator( PathTranslator pathTranslator, UrlNormalizer urlNormalizer,
85+
ModelVersionProcessor processor )
9086
{
9187
this.pathTranslator = pathTranslator;
9288
this.urlNormalizer = urlNormalizer;
89+
this.versionProcessor = processor;
9390
}
9491

9592
protected List<ValueSource> createValueSources( final Model model, final File projectDir,
@@ -151,18 +148,8 @@ public Object getValue( String expression )
151148

152149
// Overwrite existing values in model properties. Otherwise it's not possible
153150
// to define the version via command line: mvn -Drevision=6.5.7 ...
154-
if ( config.getSystemProperties().containsKey( REVISION_PROPERTY ) )
155-
{
156-
modelProperties.put( REVISION_PROPERTY, config.getSystemProperties().get( REVISION_PROPERTY ) );
157-
}
158-
if ( config.getSystemProperties().containsKey( CHANGELIST_PROPERTY ) )
159-
{
160-
modelProperties.put( CHANGELIST_PROPERTY, config.getSystemProperties().get( CHANGELIST_PROPERTY ) );
161-
}
162-
if ( config.getSystemProperties().containsKey( SHA1_PROPERTY ) )
163-
{
164-
modelProperties.put( SHA1_PROPERTY, config.getSystemProperties().get( SHA1_PROPERTY ) );
165-
}
151+
versionProcessor.overwriteModelProperties( modelProperties, config );
152+
166153
valueSources.add( new MapBasedValueSource( modelProperties ) );
167154

168155
valueSources.add( new MapBasedValueSource( config.getSystemProperties() ) );
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package org.apache.maven.model.interpolation;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
import java.util.Properties;
23+
24+
import javax.inject.Named;
25+
import javax.inject.Singleton;
26+
27+
import org.apache.maven.model.building.ModelBuildingRequest;
28+
29+
/**
30+
* Maven default implementation of the {@link ModelVersionProcessor} to support
31+
* <a href="https://maven.apache.org/maven-ci-friendly.html">CI Friendly Versions</a>
32+
*/
33+
@Named
34+
@Singleton
35+
public class DefaultModelVersionProcessor
36+
implements ModelVersionProcessor
37+
{
38+
39+
private static final String SHA1_PROPERTY = "sha1";
40+
41+
private static final String CHANGELIST_PROPERTY = "changelist";
42+
43+
private static final String REVISION_PROPERTY = "revision";
44+
45+
@Override
46+
public boolean isValidProperty( String property )
47+
{
48+
return REVISION_PROPERTY.equals( property ) || CHANGELIST_PROPERTY.equals( property )
49+
|| SHA1_PROPERTY.equals( property );
50+
}
51+
52+
@Override
53+
public void overwriteModelProperties( Properties modelProperties, ModelBuildingRequest request )
54+
{
55+
if ( request.getUserProperties().containsKey( REVISION_PROPERTY ) )
56+
{
57+
modelProperties.put( REVISION_PROPERTY, request.getUserProperties().get( REVISION_PROPERTY ) );
58+
}
59+
if ( request.getUserProperties().containsKey( CHANGELIST_PROPERTY ) )
60+
{
61+
modelProperties.put( CHANGELIST_PROPERTY, request.getUserProperties().get( CHANGELIST_PROPERTY ) );
62+
}
63+
if ( request.getUserProperties().containsKey( SHA1_PROPERTY ) )
64+
{
65+
modelProperties.put( SHA1_PROPERTY, request.getUserProperties().get( SHA1_PROPERTY ) );
66+
}
67+
68+
}
69+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.apache.maven.model.interpolation;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
import java.util.Properties;
23+
24+
import org.apache.maven.model.building.ModelBuildingRequest;
25+
26+
/**
27+
* Allows a fixed set of properties that are valid inside a version and that could be overwritten for example on the
28+
* commandline
29+
*/
30+
public interface ModelVersionProcessor
31+
{
32+
33+
/**
34+
* @param property the property to check
35+
* @return <code>true</code> if this is a valid property for this processor
36+
*/
37+
boolean isValidProperty( String property );
38+
39+
/**
40+
* This method is responsible for examining the request and possibly overwrite of the valid properties in the model
41+
*
42+
* @param modelProperties
43+
* @param request
44+
*/
45+
void overwriteModelProperties( Properties modelProperties, ModelBuildingRequest request );
46+
47+
}

maven-model-builder/src/main/java/org/apache/maven/model/interpolation/StringSearchModelInterpolator.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,15 @@ private interface InnerInterpolator
6868
}
6969

7070
@Inject
71-
public StringSearchModelInterpolator( PathTranslator pathTranslator, UrlNormalizer urlNormalizer )
71+
public StringSearchModelInterpolator( PathTranslator pathTranslator, UrlNormalizer urlNormalizer,
72+
ModelVersionProcessor processor )
7273
{
73-
super( pathTranslator, urlNormalizer );
74+
super( pathTranslator, urlNormalizer, processor );
7475
}
7576

7677
StringSearchModelInterpolator()
7778
{
78-
super( null, null );
79+
super( null, null, null );
7980
}
8081

8182
@Override

maven-model-builder/src/main/java/org/apache/maven/model/interpolation/StringVisitorModelInterpolator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,10 @@ public class StringVisitorModelInterpolator
9292
extends AbstractStringBasedModelInterpolator
9393
{
9494
@Inject
95-
public StringVisitorModelInterpolator( PathTranslator pathTranslator, UrlNormalizer urlNormalizer )
95+
public StringVisitorModelInterpolator( PathTranslator pathTranslator, UrlNormalizer urlNormalizer,
96+
ModelVersionProcessor processor )
9697
{
97-
super( pathTranslator, urlNormalizer );
98+
super( pathTranslator, urlNormalizer, processor );
9899
}
99100

100101
interface InnerInterpolator

maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
import org.apache.maven.model.building.ModelProblem.Version;
4545
import org.apache.maven.model.building.ModelProblemCollector;
4646
import org.apache.maven.model.building.ModelProblemCollectorRequest;
47-
import org.apache.maven.model.interpolation.AbstractStringBasedModelInterpolator;
47+
import org.apache.maven.model.interpolation.ModelVersionProcessor;
4848
import org.codehaus.plexus.util.StringUtils;
4949

5050
import java.io.File;
@@ -58,6 +58,7 @@
5858
import java.util.regex.Matcher;
5959
import java.util.regex.Pattern;
6060

61+
import javax.inject.Inject;
6162
import javax.inject.Named;
6263
import javax.inject.Singleton;
6364

@@ -72,11 +73,6 @@ public class DefaultModelValidator
7273

7374
private static final Pattern EXPRESSION_NAME_PATTERN = Pattern.compile( "\\$\\{(.+?)\\}" );
7475

75-
private static final List<String> CI_FRIENDLY_POSSIBLE_PROPERTY_NAMES =
76-
Arrays.asList( AbstractStringBasedModelInterpolator.REVISION_PROPERTY,
77-
AbstractStringBasedModelInterpolator.CHANGELIST_PROPERTY,
78-
AbstractStringBasedModelInterpolator.SHA1_PROPERTY );
79-
8076
private static final String ILLEGAL_FS_CHARS = "\\/:\"<>|?*";
8177

8278
private static final String ILLEGAL_VERSION_CHARS = ILLEGAL_FS_CHARS;
@@ -89,6 +85,14 @@ public class DefaultModelValidator
8985

9086
private final Set<String> validProfileIds = new HashSet<>();
9187

88+
private final ModelVersionProcessor versionProcessor;
89+
90+
@Inject
91+
public DefaultModelValidator( ModelVersionProcessor versionProcessor )
92+
{
93+
this.versionProcessor = versionProcessor;
94+
}
95+
9296
@Override
9397
public void validateFileModel( Model m, ModelBuildingRequest request, ModelProblemCollector problems )
9498
{
@@ -1008,11 +1012,11 @@ private boolean validateVersionNoExpression( String fieldName, ModelProblemColle
10081012
Matcher m = EXPRESSION_NAME_PATTERN.matcher( string.trim() );
10091013
while ( m.find() )
10101014
{
1011-
if ( !CI_FRIENDLY_POSSIBLE_PROPERTY_NAMES.contains( m.group( 1 ) ) )
1015+
String property = m.group( 1 );
1016+
if ( !versionProcessor.isValidProperty( property ) )
10121017
{
10131018
addViolation( problems, severity, version, fieldName, null,
10141019
"contains an expression but should be a constant.", tracker );
1015-
10161020
return false;
10171021
}
10181022
}

maven-model-builder/src/test/java/org/apache/maven/model/interpolation/StringSearchModelInterpolatorTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class StringSearchModelInterpolatorTest
5858
{
5959
protected ModelInterpolator createInterpolator()
6060
{
61-
return new StringSearchModelInterpolator();
61+
return new StringSearchModelInterpolator( null, null, new DefaultModelVersionProcessor() );
6262
}
6363

6464
@Test
@@ -579,7 +579,8 @@ public void testFinalFieldsExcludedFromInterpolation()
579579
request.setUserProperties( props );
580580

581581
SimpleProblemCollector problems = new SimpleProblemCollector();
582-
StringSearchModelInterpolator interpolator = new StringSearchModelInterpolator();
582+
StringSearchModelInterpolator interpolator =
583+
new StringSearchModelInterpolator( null, null, new DefaultModelVersionProcessor() );
583584
interpolator.interpolateObject( new ClassWithFinalField(), new Model(), null, request, problems );
584585

585586
assertProblemFree( problems );
@@ -605,7 +606,8 @@ public void locationTrackerShouldBeExcludedFromInterpolation()
605606
model.setLocation( "", new InputLocation( 1, 1, source ) );
606607

607608
SimpleProblemCollector problems = new SimpleProblemCollector();
608-
StringSearchModelInterpolator interpolator = new StringSearchModelInterpolator();
609+
StringSearchModelInterpolator interpolator =
610+
new StringSearchModelInterpolator( null, null, new DefaultModelVersionProcessor() );
609611
interpolator.interpolateObject( model, model, null, request, problems );
610612

611613
assertProblemFree( problems );

maven-model-builder/src/test/java/org/apache/maven/model/interpolation/StringVisitorModelInterpolatorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ public class StringVisitorModelInterpolatorTest extends AbstractModelInterpolato
2323
{
2424
protected ModelInterpolator createInterpolator()
2525
{
26-
return new StringVisitorModelInterpolator( null, null );
26+
return new StringVisitorModelInterpolator( null, null, new DefaultModelVersionProcessor() );
2727
}
2828
}

maven-model-builder/src/test/java/org/apache/maven/model/validation/DefaultModelValidatorTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.maven.model.building.DefaultModelBuildingRequest;
2727
import org.apache.maven.model.building.ModelBuildingRequest;
2828
import org.apache.maven.model.building.SimpleProblemCollector;
29+
import org.apache.maven.model.interpolation.DefaultModelVersionProcessor;
2930
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
3031
import org.junit.jupiter.api.AfterEach;
3132
import org.junit.jupiter.api.BeforeEach;
@@ -103,7 +104,8 @@ private void assertContains( String msg, String substring )
103104
public void setUp()
104105
throws Exception
105106
{
106-
validator = new DefaultModelValidator();
107+
validator =
108+
new DefaultModelValidator( new DefaultModelVersionProcessor() );
107109
}
108110

109111
@AfterEach

0 commit comments

Comments
 (0)