Skip to content

Commit

Permalink
interpolate version from model properties if necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
TorstenKruse committed Oct 27, 2022
1 parent 1826b64 commit 5e9b753
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,10 @@ && getProject().getOriginalModel().getDependencyManagement().getDependencies() !
// TODO: I'm not 100% sure if this will work correctly in all cases.
for ( Dependency dep : getProject().getOriginalModel().getDependencyManagement().getDependencies() )
{
dep = getHelper().interpolateVersion( dep, getProject() );

getLog().debug( "Original Dpmg: " + dep.getGroupId() + ":" + dep.getArtifactId() + ":"
+ dep.getVersion() + ":" + dep.getType() + ":" + dep.getScope() );
+ dep.getVersion() + ":" + dep.getType() + ":" + dep.getScope() );
}
dependencyManagement.addAll(
getProject().getOriginalModel().getDependencyManagement().getDependencies() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ public void execute()
}
else
{
dependency = getHelper().interpolateVersion( dependency, getProject() );
dependencyManagement.add( dependency );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,24 @@ else if ( !excludePropertiesList.isEmpty() && excludePropertiesList.contains( pr
return propertyVersions;
}

@Override
public Dependency interpolateVersion( final Dependency dependency, final MavenProject project )
{

// resolve version from model properties if necessary (e.g. "${mycomponent.myversion}"
if ( dependency.getVersion().startsWith( "${" ) )
{
final String resolvedVersion = project.getOriginalModel()
.getProperties().getProperty(
dependency.getVersion().substring( 2, dependency.getVersion().length() - 1 ) );
if ( resolvedVersion != null && !resolvedVersion.isEmpty() )
{
dependency.setVersion( resolvedVersion );
}
}
return dependency;
}

private List<String> getSplitProperties( String commaSeparatedProperties )
{
List<String> propertiesList = Collections.emptyList();
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/codehaus/mojo/versions/api/VersionsHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,14 @@ Map<Property, PropertyVersions> getVersionPropertiesMap( MavenProject project, P
*/
void resolveArtifact( Artifact artifact, boolean usePluginRepositories )
throws ArtifactResolutionException, ArtifactNotFoundException;

/**
* Attempts to interpolate the version from model properties.
*
* @param dependency the dependency
* @param project the maven project
* @return the dependency with interpolated property (as far as possible)
* @since 2.14.0
*/
Dependency interpolateVersion( Dependency dependency, MavenProject project );
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ public TestDependencyUpdatesReportMojo withAllowSnapshots( boolean allowSnapshot
return this;
}

public TestDependencyUpdatesReportMojo withOriginalProperty( String name, String value )
{
project.getOriginalModel().getProperties().put( name, value );
return this;
}

private static RepositorySystem mockRepositorySystem()
{
RepositorySystem repositorySystem = mock( RepositorySystem.class );
Expand Down Expand Up @@ -367,4 +373,27 @@ public void testIt001Overview() throws IOException, MavenReportException
assertThat( "Did not generate summary correctly", output,
containsString( "groupA test-artifact 1.1 compile pom default 1.1.0-2 1.1.3 1.3 3.0" ) );
}

@Test
public void testResolvedVersionsWithoutTransitiveDependencyManagement() throws IOException, MavenReportException
{
OutputStream os = new ByteArrayOutputStream();
SinkFactory sinkFactory = new Xhtml5SinkFactory();
new TestDependencyUpdatesReportMojo()
.withOriginalDependencyManagement(
dependencyOf( "artifactA", "1.0.0" ),
dependencyOf( "artifactB", "${mycomponent.version}" ) )
.withDependencyManagement(
dependencyOf( "artifactA", "1.0.0" ),
dependencyOf( "artifactB", "${mycomponent.version}" ) )
.withProcessDependencyManagement( true )
.withProcessDependencyManagementTransitive( false )
.withOnlyUpgradable( false )
.withOriginalProperty( "mycomponent.version", "1.2.3" )
.generate( sinkFactory.createSink( os ), sinkFactory, Locale.getDefault() );

String output = os.toString();
assertThat( output, Matchers
.stringContainsInOrder( "artifactA", "1.0.0", "artifactB", "1.2.3" ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,34 @@ null, mockArtifactMetadataSource( new HashMap<String, String[]>()
}
}
}

@Test
public void testVersionInterpolation() throws Exception
{
File outputFile = null;
try
{
outputFile = File.createTempFile( "display-dependency-updates", "" );
assert outputFile.exists();

DisplayDependencyUpdatesMojo mojo = (DisplayDependencyUpdatesMojo) mojoRule.lookupConfiguredMojo(
new File( "target/test-classes/org/codehaus/mojo/display-dependency-updates/version-interpolation" ),
"display-dependency-updates" );

// This is just an example of how to create it-style tests as unit tests; the advantage is easier debugging
mojo.outputFile = outputFile;
mojo.artifactMetadataSource = mockArtifactMetadataSource( new HashMap<String, String[]>()
{{
put( "dummy-api", new String[] { "2.0.1" } );
}} );
setVariableValueToObject( mojo, "processDependencyManagementTransitive", false );
mojo.execute();
List<String> output = Files.readAllLines( outputFile.toPath(), UTF_8 );
assertThat( output, not( hasItem( containsString( "mycomponent.version" ) ) ) );
}
finally
{
assert outputFile == null || !outputFile.exists() || outputFile.delete();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>default-group</groupId>
<artifactId>default-artifact</artifactId>
<version>1.0</version>
<packaging>pom</packaging>

<properties>
<mycomponent.version>1.2.3</mycomponent.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-api</artifactId>
<version>"${mycomponent.version}"</version>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-api</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>${project.version}</version>
<goals>
<goal>display-dependency-updates</goal>
</goals>
<configuration>
<!-- pluginContext is necessary: it's not being injected by lookupConfiguredMojo -->
<pluginContext/>
</configuration>
</plugin>
</plugins>
</build>
</project>

0 comments on commit 5e9b753

Please sign in to comment.