diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilderFactory.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilderFactory.java index 6334f1b84b..8c7b603ab4 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilderFactory.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilderFactory.java @@ -26,7 +26,9 @@ import org.apache.maven.model.composition.DefaultDependencyManagementImporter; import org.apache.maven.model.composition.DependencyManagementImporter; import org.apache.maven.model.inheritance.DefaultInheritanceAssembler; import org.apache.maven.model.inheritance.InheritanceAssembler; +import org.apache.maven.model.interpolation.DefaultModelVersionProcessor; import org.apache.maven.model.interpolation.ModelInterpolator; +import org.apache.maven.model.interpolation.ModelVersionProcessor; import org.apache.maven.model.interpolation.StringVisitorModelInterpolator; import org.apache.maven.model.io.DefaultModelReader; import org.apache.maven.model.io.ModelReader; @@ -259,12 +261,19 @@ public class DefaultModelBuilderFactory { UrlNormalizer normalizer = newUrlNormalizer(); PathTranslator pathTranslator = newPathTranslator(); - return new StringVisitorModelInterpolator( pathTranslator, normalizer ); + ModelVersionProcessor versionProcessor = newModelVersionPropertiesProcessor(); + return new StringVisitorModelInterpolator( pathTranslator, normalizer, versionProcessor ); + } + + protected ModelVersionProcessor newModelVersionPropertiesProcessor() + { + return new DefaultModelVersionProcessor(); } protected ModelValidator newModelValidator() { - return new DefaultModelValidator(); + ModelVersionProcessor processor = newModelVersionPropertiesProcessor(); + return new DefaultModelValidator( processor ); } protected ModelNormalizer newModelNormalizer() diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/AbstractStringBasedModelInterpolator.java b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/AbstractStringBasedModelInterpolator.java index 588b91340b..9fb48a28c9 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/AbstractStringBasedModelInterpolator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/AbstractStringBasedModelInterpolator.java @@ -52,12 +52,6 @@ import org.codehaus.plexus.interpolation.ValueSource; public abstract class AbstractStringBasedModelInterpolator implements ModelInterpolator { - public static final String SHA1_PROPERTY = "sha1"; - - public static final String CHANGELIST_PROPERTY = "changelist"; - - public static final String REVISION_PROPERTY = "revision"; - private static final List PROJECT_PREFIXES = Arrays.asList( "pom.", "project." ); private static final Collection TRANSLATED_PATH_EXPRESSIONS; @@ -84,12 +78,15 @@ public abstract class AbstractStringBasedModelInterpolator private final PathTranslator pathTranslator; private final UrlNormalizer urlNormalizer; + private final ModelVersionProcessor versionProcessor; @Inject - public AbstractStringBasedModelInterpolator( PathTranslator pathTranslator, UrlNormalizer urlNormalizer ) + public AbstractStringBasedModelInterpolator( PathTranslator pathTranslator, UrlNormalizer urlNormalizer, + ModelVersionProcessor processor ) { this.pathTranslator = pathTranslator; this.urlNormalizer = urlNormalizer; + this.versionProcessor = processor; } protected List createValueSources( final Model model, final File projectDir, @@ -151,18 +148,8 @@ public abstract class AbstractStringBasedModelInterpolator // Overwrite existing values in model properties. Otherwise it's not possible // to define the version via command line: mvn -Drevision=6.5.7 ... - if ( config.getSystemProperties().containsKey( REVISION_PROPERTY ) ) - { - modelProperties.put( REVISION_PROPERTY, config.getSystemProperties().get( REVISION_PROPERTY ) ); - } - if ( config.getSystemProperties().containsKey( CHANGELIST_PROPERTY ) ) - { - modelProperties.put( CHANGELIST_PROPERTY, config.getSystemProperties().get( CHANGELIST_PROPERTY ) ); - } - if ( config.getSystemProperties().containsKey( SHA1_PROPERTY ) ) - { - modelProperties.put( SHA1_PROPERTY, config.getSystemProperties().get( SHA1_PROPERTY ) ); - } + versionProcessor.overwriteModelProperties( modelProperties, config ); + valueSources.add( new MapBasedValueSource( modelProperties ) ); valueSources.add( new MapBasedValueSource( config.getSystemProperties() ) ); diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/DefaultModelVersionProcessor.java b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/DefaultModelVersionProcessor.java new file mode 100644 index 0000000000..27e3469a3b --- /dev/null +++ b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/DefaultModelVersionProcessor.java @@ -0,0 +1,69 @@ +package org.apache.maven.model.interpolation; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.util.Properties; + +import javax.inject.Named; +import javax.inject.Singleton; + +import org.apache.maven.model.building.ModelBuildingRequest; + +/** + * Maven default implementation of the {@link ModelVersionProcessor} to support + * CI Friendly Versions + */ +@Named +@Singleton +public class DefaultModelVersionProcessor + implements ModelVersionProcessor +{ + + private static final String SHA1_PROPERTY = "sha1"; + + private static final String CHANGELIST_PROPERTY = "changelist"; + + private static final String REVISION_PROPERTY = "revision"; + + @Override + public boolean isValidProperty( String property ) + { + return REVISION_PROPERTY.equals( property ) || CHANGELIST_PROPERTY.equals( property ) + || SHA1_PROPERTY.equals( property ); + } + + @Override + public void overwriteModelProperties( Properties modelProperties, ModelBuildingRequest request ) + { + if ( request.getSystemProperties().containsKey( REVISION_PROPERTY ) ) + { + modelProperties.put( REVISION_PROPERTY, request.getSystemProperties().get( REVISION_PROPERTY ) ); + } + if ( request.getSystemProperties().containsKey( CHANGELIST_PROPERTY ) ) + { + modelProperties.put( CHANGELIST_PROPERTY, request.getSystemProperties().get( CHANGELIST_PROPERTY ) ); + } + if ( request.getSystemProperties().containsKey( SHA1_PROPERTY ) ) + { + modelProperties.put( SHA1_PROPERTY, request.getSystemProperties().get( SHA1_PROPERTY ) ); + } + + } +} diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/ModelVersionProcessor.java b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/ModelVersionProcessor.java new file mode 100644 index 0000000000..06b17f390e --- /dev/null +++ b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/ModelVersionProcessor.java @@ -0,0 +1,47 @@ +package org.apache.maven.model.interpolation; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.util.Properties; + +import org.apache.maven.model.building.ModelBuildingRequest; + +/** + * Allows a fixed set of properties that are valid inside a version and that could be overwritten for example on the + * commandline + */ +public interface ModelVersionProcessor +{ + + /** + * @param property the property to check + * @return true if this is a valid property for this processor + */ + boolean isValidProperty( String property ); + + /** + * This method is responsible for examining the request and possibly overwrite of the valid properties in the model + * + * @param modelProperties + * @param request + */ + void overwriteModelProperties( Properties modelProperties, ModelBuildingRequest request ); + +} diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/StringSearchModelInterpolator.java b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/StringSearchModelInterpolator.java index d775fe3290..99f55bd70c 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/StringSearchModelInterpolator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/StringSearchModelInterpolator.java @@ -68,14 +68,10 @@ public class StringSearchModelInterpolator } @Inject - public StringSearchModelInterpolator( PathTranslator pathTranslator, UrlNormalizer urlNormalizer ) + public StringSearchModelInterpolator( PathTranslator pathTranslator, UrlNormalizer urlNormalizer, + ModelVersionProcessor processor ) { - super( pathTranslator, urlNormalizer ); - } - - StringSearchModelInterpolator() - { - super( null, null ); + super( pathTranslator, urlNormalizer, processor ); } @Override diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/StringVisitorModelInterpolator.java b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/StringVisitorModelInterpolator.java index e639a50e34..e2438d540a 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/StringVisitorModelInterpolator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/StringVisitorModelInterpolator.java @@ -92,9 +92,10 @@ public class StringVisitorModelInterpolator extends AbstractStringBasedModelInterpolator { @Inject - public StringVisitorModelInterpolator( PathTranslator pathTranslator, UrlNormalizer urlNormalizer ) + public StringVisitorModelInterpolator( PathTranslator pathTranslator, UrlNormalizer urlNormalizer, + ModelVersionProcessor processor ) { - super( pathTranslator, urlNormalizer ); + super( pathTranslator, urlNormalizer, processor ); } interface InnerInterpolator diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java b/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java index 7ac53a4d23..93a99ccf3b 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java @@ -44,7 +44,7 @@ import org.apache.maven.model.building.ModelProblem.Severity; import org.apache.maven.model.building.ModelProblem.Version; import org.apache.maven.model.building.ModelProblemCollector; import org.apache.maven.model.building.ModelProblemCollectorRequest; -import org.apache.maven.model.interpolation.AbstractStringBasedModelInterpolator; +import org.apache.maven.model.interpolation.ModelVersionProcessor; import org.codehaus.plexus.util.StringUtils; import java.io.File; @@ -58,6 +58,7 @@ import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; +import javax.inject.Inject; import javax.inject.Named; import javax.inject.Singleton; @@ -72,11 +73,6 @@ public class DefaultModelValidator private static final Pattern EXPRESSION_NAME_PATTERN = Pattern.compile( "\\$\\{(.+?)\\}" ); - private static final List CI_FRIENDLY_POSSIBLE_PROPERTY_NAMES = - Arrays.asList( AbstractStringBasedModelInterpolator.REVISION_PROPERTY, - AbstractStringBasedModelInterpolator.CHANGELIST_PROPERTY, - AbstractStringBasedModelInterpolator.SHA1_PROPERTY ); - private static final String ILLEGAL_FS_CHARS = "\\/:\"<>|?*"; private static final String ILLEGAL_VERSION_CHARS = ILLEGAL_FS_CHARS; @@ -89,6 +85,14 @@ public class DefaultModelValidator private final Set validProfileIds = new HashSet<>(); + private final ModelVersionProcessor versionProcessor; + + @Inject + public DefaultModelValidator( ModelVersionProcessor versionProcessor ) + { + this.versionProcessor = versionProcessor; + } + @Override public void validateFileModel( Model m, ModelBuildingRequest request, ModelProblemCollector problems ) { @@ -762,7 +766,7 @@ public class DefaultModelValidator String prefix2, ModelBuildingRequest request ) { Map index = new HashMap<>(); - + for ( Repository repository : repositories ) { validateStringNotEmpty( prefix, prefix2, "id", problems, Severity.ERROR, Version.V20, repository.getId(), @@ -783,7 +787,7 @@ public class DefaultModelValidator } } } - + String key = repository.getId(); Repository existing = index.get( key ); @@ -998,17 +1002,11 @@ public class DefaultModelValidator return true; } - // - // Acceptable versions for continuous delivery - // - // changelist - // revision - // sha1 - // Matcher m = EXPRESSION_NAME_PATTERN.matcher( string.trim() ); while ( m.find() ) { - if ( !CI_FRIENDLY_POSSIBLE_PROPERTY_NAMES.contains( m.group( 1 ) ) ) + String property = m.group( 1 ); + if ( !versionProcessor.isValidProperty( property ) ) { addViolation( problems, severity, version, fieldName, null, "contains an expression but should be a constant.", tracker ); diff --git a/maven-model-builder/src/test/java/org/apache/maven/model/interpolation/StringSearchModelInterpolatorTest.java b/maven-model-builder/src/test/java/org/apache/maven/model/interpolation/StringSearchModelInterpolatorTest.java index 50f4df0fbd..cc2f2970c8 100644 --- a/maven-model-builder/src/test/java/org/apache/maven/model/interpolation/StringSearchModelInterpolatorTest.java +++ b/maven-model-builder/src/test/java/org/apache/maven/model/interpolation/StringSearchModelInterpolatorTest.java @@ -58,7 +58,7 @@ public class StringSearchModelInterpolatorTest { protected ModelInterpolator createInterpolator() { - return new StringSearchModelInterpolator(); + return new StringSearchModelInterpolator( null, null, new DefaultModelVersionProcessor() ); } @Test @@ -579,7 +579,8 @@ public class StringSearchModelInterpolatorTest request.setUserProperties( props ); SimpleProblemCollector problems = new SimpleProblemCollector(); - StringSearchModelInterpolator interpolator = new StringSearchModelInterpolator(); + StringSearchModelInterpolator interpolator = + new StringSearchModelInterpolator( null, null, new DefaultModelVersionProcessor() ); interpolator.interpolateObject( new ClassWithFinalField(), new Model(), null, request, problems ); assertProblemFree( problems ); @@ -605,7 +606,8 @@ public class StringSearchModelInterpolatorTest model.setLocation( "", new InputLocation( 1, 1, source ) ); SimpleProblemCollector problems = new SimpleProblemCollector(); - StringSearchModelInterpolator interpolator = new StringSearchModelInterpolator(); + StringSearchModelInterpolator interpolator = + new StringSearchModelInterpolator( null, null, new DefaultModelVersionProcessor() ); interpolator.interpolateObject( model, model, null, request, problems ); assertProblemFree( problems ); diff --git a/maven-model-builder/src/test/java/org/apache/maven/model/interpolation/StringVisitorModelInterpolatorTest.java b/maven-model-builder/src/test/java/org/apache/maven/model/interpolation/StringVisitorModelInterpolatorTest.java index e4b11cca8c..8fa92cecf9 100644 --- a/maven-model-builder/src/test/java/org/apache/maven/model/interpolation/StringVisitorModelInterpolatorTest.java +++ b/maven-model-builder/src/test/java/org/apache/maven/model/interpolation/StringVisitorModelInterpolatorTest.java @@ -23,6 +23,6 @@ public class StringVisitorModelInterpolatorTest extends AbstractModelInterpolato { protected ModelInterpolator createInterpolator() { - return new StringVisitorModelInterpolator( null, null ); + return new StringVisitorModelInterpolator( null, null, new DefaultModelVersionProcessor() ); } } \ No newline at end of file diff --git a/maven-model-builder/src/test/java/org/apache/maven/model/validation/DefaultModelValidatorTest.java b/maven-model-builder/src/test/java/org/apache/maven/model/validation/DefaultModelValidatorTest.java index 4517eb75f1..57dae7ccbe 100644 --- a/maven-model-builder/src/test/java/org/apache/maven/model/validation/DefaultModelValidatorTest.java +++ b/maven-model-builder/src/test/java/org/apache/maven/model/validation/DefaultModelValidatorTest.java @@ -26,6 +26,7 @@ import org.apache.maven.model.Model; import org.apache.maven.model.building.DefaultModelBuildingRequest; import org.apache.maven.model.building.ModelBuildingRequest; import org.apache.maven.model.building.SimpleProblemCollector; +import org.apache.maven.model.interpolation.DefaultModelVersionProcessor; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -103,7 +104,7 @@ public class DefaultModelValidatorTest public void setUp() throws Exception { - validator = new DefaultModelValidator(); + validator = new DefaultModelValidator( new DefaultModelVersionProcessor() ); } @AfterEach @@ -876,7 +877,7 @@ public class DefaultModelValidatorTest assertViolations( result, 0, 0, 1 ); assertEquals( "'parent.version' is either LATEST or RELEASE (both of them are being deprecated)", result.getWarnings().get( 0 ) ); } - + @Test public void repositoryWithExpression() throws Exception { @@ -884,7 +885,7 @@ public class DefaultModelValidatorTest assertViolations( result, 0, 1, 0 ); assertEquals( "'repositories.repository.[repo].url' contains an expression but should be a constant.", result.getErrors().get( 0 ) ); } - + @Test public void repositoryWithBasedirExpression() throws Exception {