[MNG-7407] Introduce a ModelVersionProcessor component to make CI Friendly Versions pluggable

This closes 
This commit is contained in:
Christoph Läubrich 2022-02-02 10:00:56 +01:00 committed by Michael Osipov
parent 6f14196846
commit 2bb1228de6
7 changed files with 154 additions and 41 deletions

View File

@ -24,7 +24,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;
@ -133,12 +135,18 @@ public class DefaultModelBuilderFactory
{
UrlNormalizer normalizer = newUrlNormalizer();
PathTranslator pathTranslator = newPathTranslator();
return new StringVisitorModelInterpolator().setPathTranslator( pathTranslator ).setUrlNormalizer( normalizer );
return new StringVisitorModelInterpolator().setPathTranslator( pathTranslator ).setUrlNormalizer( normalizer )
.setVersionPropertiesProcessor( newModelVersionPropertiesProcessor() );
}
protected ModelVersionProcessor newModelVersionPropertiesProcessor()
{
return new DefaultModelVersionProcessor();
}
protected ModelValidator newModelValidator()
{
return new DefaultModelValidator();
return new DefaultModelValidator( newModelVersionPropertiesProcessor() );
}
protected ModelNormalizer newModelNormalizer()

View File

@ -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<String> PROJECT_PREFIXES = Arrays.asList( "pom.", "project." );
private static final Collection<String> TRANSLATED_PATH_EXPRESSIONS;
@ -88,9 +82,8 @@ public abstract class AbstractStringBasedModelInterpolator
@Inject
private UrlNormalizer urlNormalizer;
public AbstractStringBasedModelInterpolator()
{
}
@Inject
private ModelVersionProcessor versionProcessor;
public AbstractStringBasedModelInterpolator setPathTranslator( PathTranslator pathTranslator )
{
@ -104,6 +97,12 @@ public abstract class AbstractStringBasedModelInterpolator
return this;
}
public AbstractStringBasedModelInterpolator setVersionPropertiesProcessor( ModelVersionProcessor processor )
{
this.versionProcessor = processor;
return this;
}
protected List<ValueSource> createValueSources( final Model model, final File projectDir,
final ModelBuildingRequest config,
final ModelProblemCollector problems )
@ -162,19 +161,8 @@ public abstract class AbstractStringBasedModelInterpolator
valueSources.add( new MapBasedValueSource( config.getUserProperties() ) );
// 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 ) );
}
// to define them via command line e.g.: mvn -Drevision=6.5.7 ...
versionProcessor.overwriteModelProperties( modelProperties, config );
valueSources.add( new MapBasedValueSource( modelProperties ) );
valueSources.add( new MapBasedValueSource( config.getSystemProperties() ) );

View File

@ -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
* <a href="https://maven.apache.org/maven-ci-friendly.html">CI Friendly Versions</a>
*/
@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 ) );
}
}
}

View File

@ -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 <code>true</code> 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 );
}

View File

@ -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 CI_FRIENDLY_EXPRESSION = Pattern.compile( "\\$\\{(.+?)\\}" );
private static final List<String> 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;
@ -87,6 +83,14 @@ public class DefaultModelValidator
private final Set<String> validIds = new HashSet<>();
private ModelVersionProcessor versionProcessor;
@Inject
public DefaultModelValidator( ModelVersionProcessor versionProcessor )
{
this.versionProcessor = versionProcessor;
}
@Override
public void validateRawModel( Model m, ModelBuildingRequest request, ModelProblemCollector problems )
{
@ -930,21 +934,14 @@ public class DefaultModelValidator
return true;
}
//
// Acceptable versions for continuous delivery
//
// changelist
// revision
// sha1
//
Matcher m = CI_FRIENDLY_EXPRESSION.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 );
return false;
}
}

View File

@ -54,7 +54,8 @@ public class StringSearchModelInterpolatorTest
throws Exception
{
super.setUp();
interpolator = new StringSearchModelInterpolator();
interpolator =
new StringSearchModelInterpolator().setVersionPropertiesProcessor( new DefaultModelVersionProcessor() );
}
@ -580,6 +581,7 @@ public class StringSearchModelInterpolatorTest
SimpleProblemCollector problems = new SimpleProblemCollector();
StringSearchModelInterpolator interpolator = new StringSearchModelInterpolator();
interpolator.setVersionPropertiesProcessor( new DefaultModelVersionProcessor() );
interpolator.interpolateObject( new ClassWithFinalField(), new Model(), null, request, problems );
assertProblemFree( problems );
@ -605,6 +607,7 @@ public class StringSearchModelInterpolatorTest
SimpleProblemCollector problems = new SimpleProblemCollector();
StringSearchModelInterpolator interpolator = new StringSearchModelInterpolator();
interpolator.setVersionPropertiesProcessor( new DefaultModelVersionProcessor() );
interpolator.interpolateObject( model, model, null, request, problems );
assertProblemFree( problems );

View File

@ -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 junit.framework.TestCase;
@ -95,7 +96,7 @@ public class DefaultModelValidatorTest
{
super.setUp();
validator = new DefaultModelValidator();
validator = new DefaultModelValidator(new DefaultModelVersionProcessor() );
}
@Override