Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNG-6802] Fix bug in FileProfileActivator (#347) #649

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.model.Activation;
import org.apache.maven.model.ActivationFile;
import org.apache.maven.model.Build;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
Expand All @@ -46,11 +47,13 @@
import org.apache.maven.model.normalization.ModelNormalizer;
import org.apache.maven.model.path.ModelPathTranslator;
import org.apache.maven.model.path.ModelUrlNormalizer;
import org.apache.maven.model.path.ProfileActivationFilePathInterpolator;
import org.apache.maven.model.plugin.LifecycleBindingsInjector;
import org.apache.maven.model.plugin.PluginConfigurationExpander;
import org.apache.maven.model.plugin.ReportConfigurationExpander;
import org.apache.maven.model.plugin.ReportingConverter;
import org.apache.maven.model.profile.DefaultProfileActivationContext;
import org.apache.maven.model.profile.ProfileActivationContext;
import org.apache.maven.model.profile.ProfileInjector;
import org.apache.maven.model.profile.ProfileSelector;
import org.apache.maven.model.resolution.InvalidRepositoryException;
Expand All @@ -59,6 +62,7 @@
import org.apache.maven.model.resolution.WorkspaceModelResolver;
import org.apache.maven.model.superpom.SuperPomProvider;
import org.apache.maven.model.validation.ModelValidator;
import org.codehaus.plexus.interpolation.InterpolationException;
import org.codehaus.plexus.interpolation.MapBasedValueSource;
import org.codehaus.plexus.interpolation.StringSearchInterpolator;
import org.eclipse.sisu.Nullable;
Expand Down Expand Up @@ -142,6 +146,9 @@ public class DefaultModelBuilder
@Inject
private ReportingConverter reportingConverter;

@Inject
private ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator;

public DefaultModelBuilder setModelProcessor( ModelProcessor modelProcessor )
{
this.modelProcessor = modelProcessor;
Expand Down Expand Up @@ -244,6 +251,13 @@ public DefaultModelBuilder setReportingConverter( ReportingConverter reportingCo
return this;
}

public DefaultModelBuilder setProfileActivationFilePathInterpolator(
ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator )
{
this.profileActivationFilePathInterpolator = profileActivationFilePathInterpolator;
return this;
}

@SuppressWarnings( "checkstyle:methodlength" )
@Override
public ModelBuildingResult build( ModelBuildingRequest request )
Expand Down Expand Up @@ -317,7 +331,9 @@ protected ModelBuildingResult build( ModelBuildingRequest request, Collection<St
profileActivationContext, problems );
currentData.setActiveProfiles( activePomProfiles );

Map<String, Activation> interpolatedActivations = getProfileActivations( rawModel, false );
Map<String, Activation> interpolatedActivations = getInterpolatedActivations( rawModel,
profileActivationContext,
problems );
injectProfileActivations( tmpModel, interpolatedActivations );

// profile injection
Expand Down Expand Up @@ -440,6 +456,56 @@ else if ( !parentIds.add( parentData.getId() ) )
return result;
}

private Map<String, Activation> getInterpolatedActivations( Model rawModel,
DefaultProfileActivationContext context,
DefaultModelProblemCollector problems )
{
Map<String, Activation> interpolatedActivations = getProfileActivations( rawModel, true );
for ( Activation activation : interpolatedActivations.values() )
{
if ( activation.getFile() != null )
{
replaceWithInterpolatedValue( activation.getFile(), context, problems );
}
}
return interpolatedActivations;
}

private void replaceWithInterpolatedValue( ActivationFile activationFile, ProfileActivationContext context,
DefaultModelProblemCollector problems )
{
try
{
if ( isNotEmpty( activationFile.getExists() ) )
{
String path = activationFile.getExists();
String absolutePath = profileActivationFilePathInterpolator.interpolate( path, context );
activationFile.setExists( absolutePath );
}
else if ( isNotEmpty( activationFile.getMissing() ) )
{
String path = activationFile.getMissing();
String absolutePath = profileActivationFilePathInterpolator.interpolate( path, context );
activationFile.setMissing( absolutePath );
}
}
catch ( InterpolationException e )
{
String path = isNotEmpty(
activationFile.getExists() ) ? activationFile.getExists() : activationFile.getMissing();

problems.add( new ModelProblemCollectorRequest( Severity.ERROR, Version.BASE ).setMessage(
"Failed to interpolate file location " + path + ": " + e.getMessage() ).setLocation(
activationFile.getLocation( isNotEmpty( activationFile.getExists() ) ? "exists" : "missing" ) )
.setException( e ) );
}
}

private static boolean isNotEmpty( String string )
{
return string != null && !string.isEmpty();
}

@Override
public ModelBuildingResult build( ModelBuildingRequest request, ModelBuildingResult result )
throws ModelBuildingException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.apache.maven.model.path.ModelPathTranslator;
import org.apache.maven.model.path.ModelUrlNormalizer;
import org.apache.maven.model.path.PathTranslator;
import org.apache.maven.model.path.ProfileActivationFilePathInterpolator;
import org.apache.maven.model.path.UrlNormalizer;
import org.apache.maven.model.plugin.DefaultPluginConfigurationExpander;
import org.apache.maven.model.plugin.DefaultReportConfigurationExpander;
Expand Down Expand Up @@ -109,7 +110,13 @@ protected ProfileSelector newProfileSelector()
protected ProfileActivator[] newProfileActivators()
{
return new ProfileActivator[] { new JdkVersionProfileActivator(), new OperatingSystemProfileActivator(),
new PropertyProfileActivator(), new FileProfileActivator().setPathTranslator( newPathTranslator() ) };
new PropertyProfileActivator(), new FileProfileActivator()
.setProfileActivationFilePathInterpolator( newProfileActivationFilePathInterpolator() ) };
}

protected ProfileActivationFilePathInterpolator newProfileActivationFilePathInterpolator()
{
return new ProfileActivationFilePathInterpolator().setPathTranslator( newPathTranslator() );
}

protected UrlNormalizer newUrlNormalizer()
Expand Down Expand Up @@ -225,6 +232,7 @@ public DefaultModelBuilder newInstance()
modelBuilder.setPluginConfigurationExpander( newPluginConfigurationExpander() );
modelBuilder.setReportConfigurationExpander( newReportConfigurationExpander() );
modelBuilder.setReportingConverter( newReportingConverter() );
modelBuilder.setProfileActivationFilePathInterpolator( newProfileActivationFilePathInterpolator() );

return modelBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package org.apache.maven.model.path;

/*
* 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 org.apache.maven.model.ActivationFile;
import org.apache.maven.model.profile.ProfileActivationContext;
import org.codehaus.plexus.interpolation.AbstractValueSource;
import org.codehaus.plexus.interpolation.InterpolationException;
import org.codehaus.plexus.interpolation.MapBasedValueSource;
import org.codehaus.plexus.interpolation.RegexBasedInterpolator;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.io.File;

/**
* Finds an absolute path for {@link ActivationFile#getExists()} or {@link ActivationFile#getMissing()}
*
* @author Ravil Galeyev
*/
@Named
@Singleton
public class ProfileActivationFilePathInterpolator
{

@Inject
private PathTranslator pathTranslator;

public ProfileActivationFilePathInterpolator setPathTranslator( PathTranslator pathTranslator )
{
this.pathTranslator = pathTranslator;
return this;
}

/**
* Interpolates given {@code path}.
*
* @return absolute path or {@code null} if the input was {@code null}
*/
public String interpolate( String path, ProfileActivationContext context ) throws InterpolationException
{
if ( path == null )
{
return null;
}

RegexBasedInterpolator interpolator = new RegexBasedInterpolator();

final File basedir = context.getProjectDirectory();

if ( basedir != null )
{
interpolator.addValueSource( new AbstractValueSource( false )
{
@Override
public Object getValue( String expression )
{
/*
* We intentionally only support ${basedir} and not ${project.basedir} as the latter form
* would suggest that other project.* expressions can be used which is beyond the design.
*/
if ( "basedir".equals( expression ) )
{
return basedir.getAbsolutePath();
}
return null;
}
} );
}
else if ( path.contains( "${basedir}" ) )
{
return null;
}

interpolator.addValueSource( new MapBasedValueSource( context.getProjectProperties() ) );

interpolator.addValueSource( new MapBasedValueSource( context.getUserProperties() ) );

interpolator.addValueSource( new MapBasedValueSource( context.getSystemProperties() ) );

String absolutePath = interpolator.interpolate( path, "" );

return pathTranslator.alignToBaseDirectory( absolutePath, basedir );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,13 @@
import org.apache.maven.model.Activation;
import org.apache.maven.model.ActivationFile;
import org.apache.maven.model.Profile;
import org.apache.maven.model.building.ModelProblemCollector;
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.path.PathTranslator;
import org.apache.maven.model.path.ProfileActivationFilePathInterpolator;
import org.apache.maven.model.profile.ProfileActivationContext;
import org.codehaus.plexus.interpolation.AbstractValueSource;
import org.codehaus.plexus.interpolation.MapBasedValueSource;
import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
import org.codehaus.plexus.interpolation.InterpolationException;
import org.codehaus.plexus.util.StringUtils;

/**
Expand All @@ -58,11 +56,12 @@ public class FileProfileActivator
{

@Inject
private PathTranslator pathTranslator;
private ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator;

public FileProfileActivator setPathTranslator( PathTranslator pathTranslator )
public FileProfileActivator setProfileActivationFilePathInterpolator(
ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator )
{
this.pathTranslator = pathTranslator;
this.profileActivationFilePathInterpolator = profileActivationFilePathInterpolator;
return this;
}

Expand Down Expand Up @@ -101,64 +100,23 @@ else if ( StringUtils.isNotEmpty( file.getMissing() ) )
return false;
}

RegexBasedInterpolator interpolator = new RegexBasedInterpolator();

final File basedir = context.getProjectDirectory();

if ( basedir != null )
{
interpolator.addValueSource( new AbstractValueSource( false )
{
@Override
public Object getValue( String expression )
{
/*
* NOTE: We intentionally only support ${basedir} and not ${project.basedir} as the latter form
* would suggest that other project.* expressions can be used which is however beyond the design.
*/
if ( "basedir".equals( expression ) )
{
return basedir.getAbsolutePath();
}
return null;
}
} );
}
else if ( path.contains( "${basedir}" ) )
{
return false;
}

interpolator.addValueSource( new MapBasedValueSource( context.getProjectProperties() ) );

interpolator.addValueSource( new MapBasedValueSource( context.getUserProperties() ) );

interpolator.addValueSource( new MapBasedValueSource( context.getSystemProperties() ) );

try
{
path = interpolator.interpolate( path, "" );
path = profileActivationFilePathInterpolator.interpolate( path, context );
}
catch ( Exception e )
catch ( InterpolationException e )
{
problems.add( new ModelProblemCollectorRequest( Severity.ERROR, Version.BASE )
.setMessage( "Failed to interpolate file location " + path + " for profile " + profile.getId()
+ ": " + e.getMessage() )
+ ": " + e.getMessage() )
.setLocation( file.getLocation( missing ? "missing" : "exists" ) )
.setException( e ) );
return false;
}

path = pathTranslator.alignToBaseDirectory( path, basedir );

// replace activation value with interpolated value
if ( missing )
if ( path == null )
{
file.setMissing( path );
}
else
{
file.setExists( path );
return false;
}

File f = new File( path );
Expand Down
Loading