mirror of https://github.com/apache/maven.git
[MNG-7407] Introduce a ModelVersionProcessor component to make CI Friendly Versions pluggable
This closes #674
This commit is contained in:
parent
6f14196846
commit
2bb1228de6
|
@ -24,7 +24,9 @@ import org.apache.maven.model.composition.DefaultDependencyManagementImporter;
|
||||||
import org.apache.maven.model.composition.DependencyManagementImporter;
|
import org.apache.maven.model.composition.DependencyManagementImporter;
|
||||||
import org.apache.maven.model.inheritance.DefaultInheritanceAssembler;
|
import org.apache.maven.model.inheritance.DefaultInheritanceAssembler;
|
||||||
import org.apache.maven.model.inheritance.InheritanceAssembler;
|
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.ModelInterpolator;
|
||||||
|
import org.apache.maven.model.interpolation.ModelVersionProcessor;
|
||||||
import org.apache.maven.model.interpolation.StringVisitorModelInterpolator;
|
import org.apache.maven.model.interpolation.StringVisitorModelInterpolator;
|
||||||
import org.apache.maven.model.io.DefaultModelReader;
|
import org.apache.maven.model.io.DefaultModelReader;
|
||||||
import org.apache.maven.model.io.ModelReader;
|
import org.apache.maven.model.io.ModelReader;
|
||||||
|
@ -133,12 +135,18 @@ public class DefaultModelBuilderFactory
|
||||||
{
|
{
|
||||||
UrlNormalizer normalizer = newUrlNormalizer();
|
UrlNormalizer normalizer = newUrlNormalizer();
|
||||||
PathTranslator pathTranslator = newPathTranslator();
|
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()
|
protected ModelValidator newModelValidator()
|
||||||
{
|
{
|
||||||
return new DefaultModelValidator();
|
return new DefaultModelValidator( newModelVersionPropertiesProcessor() );
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ModelNormalizer newModelNormalizer()
|
protected ModelNormalizer newModelNormalizer()
|
||||||
|
|
|
@ -52,12 +52,6 @@ import org.codehaus.plexus.interpolation.ValueSource;
|
||||||
public abstract class AbstractStringBasedModelInterpolator
|
public abstract class AbstractStringBasedModelInterpolator
|
||||||
implements ModelInterpolator
|
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 List<String> PROJECT_PREFIXES = Arrays.asList( "pom.", "project." );
|
||||||
|
|
||||||
private static final Collection<String> TRANSLATED_PATH_EXPRESSIONS;
|
private static final Collection<String> TRANSLATED_PATH_EXPRESSIONS;
|
||||||
|
@ -88,9 +82,8 @@ public abstract class AbstractStringBasedModelInterpolator
|
||||||
@Inject
|
@Inject
|
||||||
private UrlNormalizer urlNormalizer;
|
private UrlNormalizer urlNormalizer;
|
||||||
|
|
||||||
public AbstractStringBasedModelInterpolator()
|
@Inject
|
||||||
{
|
private ModelVersionProcessor versionProcessor;
|
||||||
}
|
|
||||||
|
|
||||||
public AbstractStringBasedModelInterpolator setPathTranslator( PathTranslator pathTranslator )
|
public AbstractStringBasedModelInterpolator setPathTranslator( PathTranslator pathTranslator )
|
||||||
{
|
{
|
||||||
|
@ -104,6 +97,12 @@ public abstract class AbstractStringBasedModelInterpolator
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public AbstractStringBasedModelInterpolator setVersionPropertiesProcessor( ModelVersionProcessor processor )
|
||||||
|
{
|
||||||
|
this.versionProcessor = processor;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
protected List<ValueSource> createValueSources( final Model model, final File projectDir,
|
protected List<ValueSource> createValueSources( final Model model, final File projectDir,
|
||||||
final ModelBuildingRequest config,
|
final ModelBuildingRequest config,
|
||||||
final ModelProblemCollector problems )
|
final ModelProblemCollector problems )
|
||||||
|
@ -162,19 +161,8 @@ public abstract class AbstractStringBasedModelInterpolator
|
||||||
valueSources.add( new MapBasedValueSource( config.getUserProperties() ) );
|
valueSources.add( new MapBasedValueSource( config.getUserProperties() ) );
|
||||||
|
|
||||||
// Overwrite existing values in model properties. Otherwise it's not possible
|
// Overwrite existing values in model properties. Otherwise it's not possible
|
||||||
// to define the version via command line: mvn -Drevision=6.5.7 ...
|
// to define them via command line e.g.: mvn -Drevision=6.5.7 ...
|
||||||
if ( config.getSystemProperties().containsKey( REVISION_PROPERTY ) )
|
versionProcessor.overwriteModelProperties( modelProperties, config );
|
||||||
{
|
|
||||||
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 ) );
|
|
||||||
}
|
|
||||||
valueSources.add( new MapBasedValueSource( modelProperties ) );
|
valueSources.add( new MapBasedValueSource( modelProperties ) );
|
||||||
|
|
||||||
valueSources.add( new MapBasedValueSource( config.getSystemProperties() ) );
|
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 );
|
||||||
|
|
||||||
|
}
|
|
@ -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.ModelProblem.Version;
|
||||||
import org.apache.maven.model.building.ModelProblemCollector;
|
import org.apache.maven.model.building.ModelProblemCollector;
|
||||||
import org.apache.maven.model.building.ModelProblemCollectorRequest;
|
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 org.codehaus.plexus.util.StringUtils;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
@ -58,6 +58,7 @@ import java.util.Set;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
import javax.inject.Named;
|
import javax.inject.Named;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
|
@ -72,11 +73,6 @@ public class DefaultModelValidator
|
||||||
|
|
||||||
private static final Pattern CI_FRIENDLY_EXPRESSION = Pattern.compile( "\\$\\{(.+?)\\}" );
|
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_FS_CHARS = "\\/:\"<>|?*";
|
||||||
|
|
||||||
private static final String ILLEGAL_VERSION_CHARS = 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 final Set<String> validIds = new HashSet<>();
|
||||||
|
|
||||||
|
private ModelVersionProcessor versionProcessor;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public DefaultModelValidator( ModelVersionProcessor versionProcessor )
|
||||||
|
{
|
||||||
|
this.versionProcessor = versionProcessor;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void validateRawModel( Model m, ModelBuildingRequest request, ModelProblemCollector problems )
|
public void validateRawModel( Model m, ModelBuildingRequest request, ModelProblemCollector problems )
|
||||||
{
|
{
|
||||||
|
@ -930,21 +934,14 @@ public class DefaultModelValidator
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
// Acceptable versions for continuous delivery
|
|
||||||
//
|
|
||||||
// changelist
|
|
||||||
// revision
|
|
||||||
// sha1
|
|
||||||
//
|
|
||||||
Matcher m = CI_FRIENDLY_EXPRESSION.matcher( string.trim() );
|
Matcher m = CI_FRIENDLY_EXPRESSION.matcher( string.trim() );
|
||||||
while ( m.find() )
|
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,
|
addViolation( problems, severity, version, fieldName, null,
|
||||||
"contains an expression but should be a constant.", tracker );
|
"contains an expression but should be a constant.", tracker );
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,7 +54,8 @@ public class StringSearchModelInterpolatorTest
|
||||||
throws Exception
|
throws Exception
|
||||||
{
|
{
|
||||||
super.setUp();
|
super.setUp();
|
||||||
interpolator = new StringSearchModelInterpolator();
|
interpolator =
|
||||||
|
new StringSearchModelInterpolator().setVersionPropertiesProcessor( new DefaultModelVersionProcessor() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -580,6 +581,7 @@ public class StringSearchModelInterpolatorTest
|
||||||
|
|
||||||
SimpleProblemCollector problems = new SimpleProblemCollector();
|
SimpleProblemCollector problems = new SimpleProblemCollector();
|
||||||
StringSearchModelInterpolator interpolator = new StringSearchModelInterpolator();
|
StringSearchModelInterpolator interpolator = new StringSearchModelInterpolator();
|
||||||
|
interpolator.setVersionPropertiesProcessor( new DefaultModelVersionProcessor() );
|
||||||
interpolator.interpolateObject( new ClassWithFinalField(), new Model(), null, request, problems );
|
interpolator.interpolateObject( new ClassWithFinalField(), new Model(), null, request, problems );
|
||||||
|
|
||||||
assertProblemFree( problems );
|
assertProblemFree( problems );
|
||||||
|
@ -605,6 +607,7 @@ public class StringSearchModelInterpolatorTest
|
||||||
|
|
||||||
SimpleProblemCollector problems = new SimpleProblemCollector();
|
SimpleProblemCollector problems = new SimpleProblemCollector();
|
||||||
StringSearchModelInterpolator interpolator = new StringSearchModelInterpolator();
|
StringSearchModelInterpolator interpolator = new StringSearchModelInterpolator();
|
||||||
|
interpolator.setVersionPropertiesProcessor( new DefaultModelVersionProcessor() );
|
||||||
interpolator.interpolateObject( model, model, null, request, problems );
|
interpolator.interpolateObject( model, model, null, request, problems );
|
||||||
|
|
||||||
assertProblemFree( problems );
|
assertProblemFree( problems );
|
||||||
|
|
|
@ -26,6 +26,7 @@ import org.apache.maven.model.Model;
|
||||||
import org.apache.maven.model.building.DefaultModelBuildingRequest;
|
import org.apache.maven.model.building.DefaultModelBuildingRequest;
|
||||||
import org.apache.maven.model.building.ModelBuildingRequest;
|
import org.apache.maven.model.building.ModelBuildingRequest;
|
||||||
import org.apache.maven.model.building.SimpleProblemCollector;
|
import org.apache.maven.model.building.SimpleProblemCollector;
|
||||||
|
import org.apache.maven.model.interpolation.DefaultModelVersionProcessor;
|
||||||
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
|
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
@ -95,7 +96,7 @@ public class DefaultModelValidatorTest
|
||||||
{
|
{
|
||||||
super.setUp();
|
super.setUp();
|
||||||
|
|
||||||
validator = new DefaultModelValidator();
|
validator = new DefaultModelValidator(new DefaultModelVersionProcessor() );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue