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

#614 resolve version from model properties if necessary #797

Merged
merged 1 commit into from
Oct 28, 2022
Merged
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 @@ -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;
}

/**
* Builder class for {@linkplain DefaultVersionsHelper}
*/
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 @@ -419,4 +419,14 @@ public VersionPropertiesMapRequest build()
*/
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 @@ -399,4 +399,34 @@ public void testDetermineUpdatedSegment() throws Exception
assert outputFile == null || !outputFile.exists() || outputFile.delete();
}
}

@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>