Skip to content

Commit

Permalink
Resolves mojohaus#793: Added includeParent to DisplayPropertyUpdates …
Browse files Browse the repository at this point in the history
…and PropertyUpdatesReport
  • Loading branch information
jarmoniuk committed Oct 25, 2022
1 parent 1826b64 commit 869bc1d
Show file tree
Hide file tree
Showing 16 changed files with 163 additions and 108 deletions.

This file was deleted.

88 changes: 0 additions & 88 deletions src/it/it-property-updates-report-issue-684-001/pom.xml

This file was deleted.

6 changes: 0 additions & 6 deletions src/it/it-property-updates-report-issue-684-001/verify.groovy

This file was deleted.

Expand Up @@ -206,7 +206,8 @@ protected void update( ModifiedPomXMLEventReader pom )
if ( updatePropertyVersions )
{
Map<Property, PropertyVersions> versionProperties =
this.getHelper().getVersionPropertiesMap( getProject(), null, null, null, true );
this.getHelper().getVersionPropertiesMap( getProject(), null, null, null,
true, true );
List<String> diff = updatePropertyVersions( pom, versionProperties, remoteDepsMap );
propertyDiffs.addAll( diff );
}
Expand Down
Expand Up @@ -128,6 +128,14 @@ public class DisplayPropertyUpdatesMojo
@Parameter( property = "allowIncrementalUpdates", defaultValue = "true" )
private boolean allowIncrementalUpdates;

/**
* <p>Whether to include property updates from parent.</p>
*
* @since 2.14.0
*/
@Parameter( property = "includeParent", defaultValue = "true" )
protected boolean includeParent = true;

// -------------------------- STATIC METHODS --------------------------

// -------------------------- OTHER METHODS --------------------------
Expand All @@ -151,7 +159,7 @@ public void execute()

Map<Property, PropertyVersions> propertyVersions =
this.getHelper().getVersionPropertiesMap( getProject(), properties, includeProperties, excludeProperties,
autoLinkItems );
includeParent, autoLinkItems );
for ( Map.Entry<Property, PropertyVersions> entry : propertyVersions.entrySet() )
{
Property property = entry.getKey();
Expand Down
Expand Up @@ -84,6 +84,14 @@ public class PropertyUpdatesReportMojo extends AbstractVersionsReport<PropertyUp
@Parameter( property = "autoLinkItems", defaultValue = "true" )
private boolean autoLinkItems;

/**
* <p>Whether to include property updates from parent.</p>
*
* @since 2.14.0
*/
@Parameter( property = "includeParent", defaultValue = "true" )
private boolean includeParent = true;

@Inject
protected PropertyUpdatesReportMojo( I18N i18n, RepositorySystem repositorySystem,
ArtifactResolver artifactResolver,
Expand Down Expand Up @@ -121,7 +129,7 @@ protected void doGenerateReport( Locale locale, Sink sink )
try
{
updateSet.putAll( getHelper().getVersionPropertiesMap( getProject(), properties, includeProperties,
excludeProperties, autoLinkItems ) );
excludeProperties, includeParent, autoLinkItems ) );
}
catch ( MojoExecutionException e )
{
Expand Down
Expand Up @@ -304,7 +304,8 @@ private void resolvePropertyRanges( ModifiedPomXMLEventReader pom )
{

Map<Property, PropertyVersions> propertyVersions =
this.getHelper().getVersionPropertiesMap( getProject(), null, includeProperties, excludeProperties, true );
this.getHelper().getVersionPropertiesMap( getProject(), null, includeProperties, excludeProperties,
true, true );
for ( Map.Entry<Property, PropertyVersions> entry : propertyVersions.entrySet() )
{
Property property = entry.getKey();
Expand Down
Expand Up @@ -155,7 +155,7 @@ private void update( ModifiedPomXMLEventReader pom, Property[] propertiesConfig,
{
Map<Property, PropertyVersions> propertyVersions =
this.getHelper().getVersionPropertiesMap( getProject(), propertiesConfig, properties, "",
autoLinkItems );
false, autoLinkItems );
for ( Map.Entry<Property, PropertyVersions> entry : propertyVersions.entrySet() )
{
Property currentProperty = entry.getKey();
Expand Down
Expand Up @@ -160,6 +160,7 @@ protected void update( ModifiedPomXMLEventReader pom )
properties,
includeProperties,
excludeProperties,
false,
autoLinkItems );
for ( Map.Entry<Property, PropertyVersions> entry : propertyVersions.entrySet() )
{
Expand Down
Expand Up @@ -159,7 +159,7 @@ protected void update( ModifiedPomXMLEventReader pom )
propertyConfig.setVersion( newVersion );
Map<Property, PropertyVersions> propertyVersions =
this.getHelper().getVersionPropertiesMap( getProject(), new Property[] {propertyConfig}, property, "",
autoLinkItems );
false, autoLinkItems );
for ( Map.Entry<Property, PropertyVersions> entry : propertyVersions.entrySet() )
{
Property property = entry.getKey();
Expand Down
Expand Up @@ -785,7 +785,9 @@ public ExpressionEvaluator getExpressionEvaluator( MavenProject project )
@Override
public Map<Property, PropertyVersions> getVersionPropertiesMap( MavenProject project,
Property[] propertyDefinitions,
String includeProperties, String excludeProperties,
String includeProperties,
String excludeProperties,
boolean includeParent,
boolean autoLinkItems )
throws MojoExecutionException
{
Expand All @@ -803,7 +805,7 @@ public Map<Property, PropertyVersions> getVersionPropertiesMap( MavenProject pro
final PropertyVersionsBuilder[] propertyVersionsBuilders;
try
{
propertyVersionsBuilders = PomHelper.getPropertyVersionsBuilders( this, project );
propertyVersionsBuilders = PomHelper.getPropertyVersionsBuilders( this, project, includeParent );
}
catch ( ExpressionEvaluationException | IOException e )
{
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/org/codehaus/mojo/versions/api/PomHelper.java
Expand Up @@ -72,6 +72,7 @@
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

import static java.util.Collections.singletonMap;
import static java.util.stream.IntStream.range;

/**
Expand Down Expand Up @@ -1018,16 +1019,20 @@ private static Map<MavenProject, Model> getRawModelWithParents( MavenProject pro
*
* @param helper Our versions helper.
* @param project The project to examine.
* @param includeParent whether parent POMs should be included
* @return An array of properties that are associated within the project.
* @throws ExpressionEvaluationException if an expression cannot be evaluated.
* @throws IOException if the project's pom file cannot be parsed.
* @since 1.0-alpha-3
*/
public static PropertyVersionsBuilder[] getPropertyVersionsBuilders( VersionsHelper helper, MavenProject project )
public static PropertyVersionsBuilder[] getPropertyVersionsBuilders( VersionsHelper helper, MavenProject project,
boolean includeParent )
throws ExpressionEvaluationException, IOException
{
ExpressionEvaluator expressionEvaluator = helper.getExpressionEvaluator( project );
Map<MavenProject, Model> reactorModels = getRawModelWithParents( project );
Map<MavenProject, Model> reactorModels = includeParent
? getRawModelWithParents( project )
: singletonMap( project, getRawModel( project ) );

Map<String, PropertyVersionsBuilder> propertiesMap = new TreeMap<>();

Expand Down Expand Up @@ -1081,7 +1086,9 @@ public static PropertyVersionsBuilder[] getPropertyVersionsBuilders( VersionsHel
reactorModels.values().forEach( model -> addProperties( helper, propertiesMap, null, model.getProperties() ) );


for ( MavenProject currentPrj = project; currentPrj != null; currentPrj = currentPrj.getParent() )
for ( MavenProject currentPrj = project; currentPrj != null; currentPrj = includeParent
? currentPrj.getParent()
: null )
{
Model model = reactorModels.get( currentPrj );

Expand Down
Expand Up @@ -225,14 +225,15 @@ PluginUpdatesDetails lookupPluginUpdates( Plugin plugin, boolean allowSnapshots
* @param propertyDefinitions Any extra hints about properties.
* @param includeProperties A comma separated list of properties to include.
* @param excludeProperties A comma separated list of properties to exclude.
* @param includeParent whether to include parent POMs
* @param autoLinkItems whether to automatically infer associations
* @return a map of {@link org.codehaus.mojo.versions.api.PropertyVersions} values keyed by
* {@link org.codehaus.mojo.versions.Property} instances.
* @throws MojoExecutionException if something goes wrong.
*/
Map<Property, PropertyVersions> getVersionPropertiesMap( MavenProject project, Property[] propertyDefinitions,
String includeProperties, String excludeProperties,
boolean autoLinkItems )
boolean includeParent, boolean autoLinkItems )
throws MojoExecutionException;

/**
Expand Down
Expand Up @@ -35,6 +35,7 @@
import static org.apache.commons.codec.CharEncoding.UTF_8;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.matchesPattern;
import static org.hamcrest.Matchers.not;

/**
* Unit tests for {@link DisplayPropertyUpdatesMojo}
Expand Down Expand Up @@ -85,4 +86,25 @@ public void testPropertiesFromParent() throws Exception
assertThat( String.join( "", Files.readAllLines( tempFile ) ),
matchesPattern( ".*\\$\\{ver} \\.* 1\\.0\\.0 -> 2\\.0\\.0.*" ) );
}

@Test
public void testDisablePropertiesFromParent() throws Exception
{
Path tempFile = Files.createTempFile( tempDir, "output", "" );

TestUtils.copyDir( Paths.get( "src/test/resources/org/codehaus/mojo/display-property-updates/issue-367" ),
tempDir );
DisplayPropertyUpdatesMojo mojo =
(DisplayPropertyUpdatesMojo) mojoRule.lookupConfiguredMojo( tempDir.resolve( "child" ).toFile(),
"display-property-updates" );
mojo.outputEncoding = UTF_8;
mojo.outputFile = tempFile.toFile();
mojo.setPluginContext( new HashMap<>() );
mojo.artifactMetadataSource = MockUtils.mockArtifactMetadataSource();
mojo.includeParent = false;
mojo.execute();

assertThat( String.join( "", Files.readAllLines( tempFile ) ),
not( matchesPattern( ".*\\$\\{ver} \\.* 1\\.0\\.0 -> 2\\.0\\.0.*" ) ) );
}
}

0 comments on commit 869bc1d

Please sign in to comment.