mirror of https://github.com/apache/maven.git
[MNG-7407] Introduce a ModelVersionProcessor component to make CI Friendly Versions pluggable
This closes #675
This commit is contained in:
parent
0be5e406d7
commit
62cf29bab3
maven-model-builder/src
main/java/org/apache/maven/model
building
interpolation
AbstractStringBasedModelInterpolator.javaDefaultModelVersionProcessor.javaModelVersionProcessor.javaStringSearchModelInterpolator.javaStringVisitorModelInterpolator.java
validation
test/java/org/apache/maven/model
|
@ -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()
|
||||
|
|
|
@ -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;
|
||||
|
@ -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<ValueSource> 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() ) );
|
||||
|
|
|
@ -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 ) );
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -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 );
|
||||
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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<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;
|
||||
|
@ -89,6 +85,14 @@ public class DefaultModelValidator
|
|||
|
||||
private final Set<String> 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<String, Repository> 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 );
|
||||
|
|
|
@ -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 );
|
||||
|
|
|
@ -23,6 +23,6 @@ public class StringVisitorModelInterpolatorTest extends AbstractModelInterpolato
|
|||
{
|
||||
protected ModelInterpolator createInterpolator()
|
||||
{
|
||||
return new StringVisitorModelInterpolator( null, null );
|
||||
return new StringVisitorModelInterpolator( null, null, new DefaultModelVersionProcessor() );
|
||||
}
|
||||
}
|
|
@ -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
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue