Skip to content

Commit

Permalink
Using the DependencyBuilder across the project
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejj0 committed Sep 13, 2022
1 parent a6cf8ce commit 5cf148c
Show file tree
Hide file tree
Showing 18 changed files with 302 additions and 290 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.artifact.filter.PatternExcludesArtifactFilter;
import org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter;
import org.codehaus.mojo.versions.utils.DependencyBuilder;

/**
* Base class for a mojo that updates dependency versions.
Expand Down Expand Up @@ -239,13 +240,13 @@ protected Artifact toArtifact( Dependency dependency )
protected Artifact toArtifact( Parent model )
throws MojoExecutionException
{
Dependency d = new Dependency();
d.setArtifactId( model.getArtifactId() );
d.setGroupId( model.getGroupId() );
d.setVersion( model.getVersion() );
d.setType( "pom" );
d.setScope( Artifact.SCOPE_COMPILE );
return this.toArtifact( d );
return this.toArtifact( DependencyBuilder.newBuilder()
.withGroupId( model.getGroupId() )
.withArtifactId( model.getArtifactId() )
.withVersion( model.getVersion() )
.withType( "pom" )
.withScope( Artifact.SCOPE_COMPILE )
.build() );
}

protected String toString( MavenProject project )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.api.PropertyVersions;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
import org.codehaus.mojo.versions.utils.DependencyBuilder;

/**
* Compare dependency versions of the current project to dependencies or dependency management of a remote repository
Expand Down Expand Up @@ -146,12 +147,10 @@ protected void update( ModifiedPomXMLEventReader pom )
String rArtifactId = remotePomParts[1];
String rVersion = remotePomParts[2];

Dependency remoteDependency = new Dependency();
remoteDependency.setGroupId( rGroupId );
remoteDependency.setArtifactId( rArtifactId );
remoteDependency.setVersion( rVersion );

Artifact remoteArtifact = this.toArtifact( remoteDependency );
Artifact remoteArtifact = this.toArtifact( DependencyBuilder.newBuilder()
.withGroupId( rGroupId )
.withArtifactId( rArtifactId )
.withVersion( rVersion ).build() );
MavenProject remoteMavenProject = null;
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
import org.codehaus.mojo.versions.utils.DependencyBuilder;

/**
* Displays any updates of the project's parent project
Expand Down Expand Up @@ -75,13 +75,12 @@ public void execute()
throw new MojoExecutionException( "Invalid version range specification: " + version, e );
}

Dependency dependency = new Dependency();
dependency.setGroupId( getProject().getParent().getGroupId() );
dependency.setArtifactId( getProject().getParent().getArtifactId() );
dependency.setVersion( version );
dependency.setType( "pom" );

Artifact artifact = getHelper().createDependencyArtifact( dependency );
Artifact artifact = getHelper().createDependencyArtifact( DependencyBuilder.newBuilder()
.withGroupId( getProject().getParent().getGroupId() )
.withArtifactId( getProject().getParent().getArtifactId() )
.withVersion( version )
.withType( "pom" )
.build() );

ArtifactVersion artifactVersion;
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
import org.apache.maven.lifecycle.LifecycleExecutionException;
import org.apache.maven.lifecycle.LifecycleExecutor;
import org.apache.maven.lifecycle.mapping.LifecycleMapping;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.Prerequisites;
Expand Down Expand Up @@ -92,6 +91,7 @@
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.ordering.MavenVersionComparator;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
import org.codehaus.mojo.versions.utils.DependencyBuilder;
import org.codehaus.mojo.versions.utils.PluginComparator;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.util.IOUtil;
Expand Down Expand Up @@ -427,15 +427,13 @@ public void execute()
ArtifactVersion minRequires = null;
for ( int j = newerVersions.length - 1; j >= 0; j-- )
{

Dependency dependency = new Dependency();
dependency.setGroupId( groupId );
dependency.setArtifactId( artifactId );
dependency.setVersion( newerVersions[j].toString() );
dependency.setType( "pom" );
dependency.setScope( "runtime" );

Artifact probe = getHelper().createDependencyArtifact( dependency );
Artifact probe = getHelper().createDependencyArtifact( DependencyBuilder.newBuilder()
.withGroupId( groupId )
.withArtifactId( artifactId )
.withVersion( newerVersions[j].toString() )
.withType( "pom" )
.withScope( Artifact.SCOPE_RUNTIME )
.build() );
try
{
getHelper().resolveArtifact( probe, true );
Expand Down Expand Up @@ -493,14 +491,13 @@ public void execute()
}
if ( effectiveVersion != null )
{
Dependency dependency = new Dependency();
dependency.setGroupId( groupId );
dependency.setArtifactId( artifactId );
dependency.setVersion( effectiveVersion );
dependency.setType( "pom" );
dependency.setScope( "runtime" );

Artifact probe = getHelper().createDependencyArtifact( dependency );
Artifact probe = getHelper().createDependencyArtifact( DependencyBuilder.newBuilder()
.withGroupId( groupId )
.withArtifactId( artifactId )
.withVersion( effectiveVersion )
.withType( "pom" )
.withScope( Artifact.SCOPE_RUNTIME )
.build() );
try
{
getHelper().resolveArtifact( probe, true );
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
import org.codehaus.mojo.versions.utils.DependencyBuilder;

/**
* Sets the parent version to the latest parent version.
Expand Down Expand Up @@ -109,12 +109,12 @@ protected void update( ModifiedPomXMLEventReader pom )
version = parentVersion;
}

Dependency dependency = new Dependency();
dependency.setGroupId( getProject().getParent().getGroupId() );
dependency.setArtifactId( getProject().getParent().getArtifactId() );
dependency.setVersion( version );
dependency.setType( "pom" );
Artifact artifact = getHelper().createDependencyArtifact( dependency );
Artifact artifact = getHelper().createDependencyArtifact( DependencyBuilder.newBuilder()
.withGroupId( getProject().getParent().getGroupId() )
.withArtifactId( getProject().getParent().getArtifactId() )
.withVersion( version )
.withType( "pom" )
.build() );

VersionRange versionRange;
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.codehaus.mojo.versions.ordering.MajorMinorIncrementalFilter;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
import org.codehaus.mojo.versions.utils.DependencyBuilder;

import static java.util.Collections.singletonList;

/**
* Replaces any release versions with the latest release version.
Expand Down Expand Up @@ -111,14 +114,12 @@ protected void update( ModifiedPomXMLEventReader pom )
}
if ( getProject().getParent() != null && isProcessingParent() )
{
Dependency dependency = new Dependency();
dependency.setArtifactId( getProject().getParent().getArtifactId() );
dependency.setGroupId( getProject().getParent().getGroupId() );
dependency.setVersion( getProject().getParent().getVersion() );
dependency.setType( "pom" );
List list = new ArrayList();
list.add( dependency );
useLatestReleases( pom, list );
useLatestReleases( pom, singletonList( DependencyBuilder.newBuilder()
.withGroupId( getProject().getParent().getArtifactId() )
.withArtifactId( getProject().getParent().getArtifactId() )
.withVersion( getProject().getParent().getVersion() )
.withType( "pom" )
.build() ) );
}
}
catch ( ArtifactMetadataRetrievalException e )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -43,6 +42,9 @@
import org.codehaus.mojo.versions.ordering.MajorMinorIncrementalFilter;
import org.codehaus.mojo.versions.ordering.VersionComparator;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
import org.codehaus.mojo.versions.utils.DependencyBuilder;

import static java.util.Collections.singletonList;

/**
* Replaces any release versions with the latest snapshot version (if it has been deployed).
Expand Down Expand Up @@ -110,14 +112,12 @@ protected void update( ModifiedPomXMLEventReader pom )
}
if ( getProject().getParent() != null && isProcessingParent() )
{
Dependency dependency = new Dependency();
dependency.setArtifactId( getProject().getParent().getArtifactId() );
dependency.setGroupId( getProject().getParent().getGroupId() );
dependency.setVersion( getProject().getParent().getVersion() );
dependency.setType( "pom" );
List list = new ArrayList();
list.add( dependency );
useLatestSnapshots( pom, list );
useLatestSnapshots( pom, singletonList( DependencyBuilder.newBuilder()
.withGroupId( getProject().getParent().getGroupId() )
.withArtifactId( getProject().getParent().getArtifactId() )
.withVersion( getProject().getParent().getVersion() )
.withType( "pom" )
.build() ) );
}
}
catch ( ArtifactMetadataRetrievalException e )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
Expand All @@ -40,6 +39,9 @@
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.codehaus.mojo.versions.ordering.MajorMinorIncrementalFilter;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
import org.codehaus.mojo.versions.utils.DependencyBuilder;

import static java.util.Collections.singletonList;

/**
* Replaces any version with the latest version.
Expand Down Expand Up @@ -129,12 +131,12 @@ protected void update( ModifiedPomXMLEventReader pom )
}
if ( getProject().getParent() != null && isProcessingParent() )
{
Dependency dependency = new Dependency();
dependency.setArtifactId( getProject().getParent().getArtifactId() );
dependency.setGroupId( getProject().getParent().getGroupId() );
dependency.setVersion( getProject().getParent().getVersion() );
dependency.setType( "pom" );
useLatestVersions( pom, Collections.singletonList( dependency ) );
useLatestVersions( pom, singletonList( DependencyBuilder.newBuilder()
.withGroupId( getProject().getParent().getGroupId() )
.withArtifactId( getProject().getParent().getArtifactId() )
.withVersion( getProject().getParent().getVersion() )
.withType( "pom" )
.build() ) );
}
}
catch ( ArtifactMetadataRetrievalException | IOException e )
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/org/codehaus/mojo/versions/UseReleasesMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
import org.codehaus.mojo.versions.utils.DependencyBuilder;

/**
* Replaces any -SNAPSHOT versions with the corresponding release version (if it has been released).
Expand Down Expand Up @@ -119,13 +120,12 @@ private void useReleases( ModifiedPomXMLEventReader pom, MavenProject project )

final MavenProject parent = getProject().getParent();

Dependency dependency = new Dependency();
dependency.setGroupId( parent.getGroupId() );
dependency.setArtifactId( parent.getArtifactId() );
dependency.setVersion( releaseVersion );
dependency.setType( "pom" );

Artifact artifact = getHelper().createDependencyArtifact( dependency );
Artifact artifact = getHelper().createDependencyArtifact( DependencyBuilder.newBuilder()
.withGroupId( parent.getGroupId() )
.withArtifactId( parent.getArtifactId() )
.withVersion( releaseVersion )
.withType( "pom" )
.build() );
if ( !isIncluded( artifact ) )
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import org.codehaus.mojo.versions.model.io.xpp3.RuleXpp3Reader;
import org.codehaus.mojo.versions.ordering.VersionComparator;
import org.codehaus.mojo.versions.ordering.VersionComparators;
import org.codehaus.mojo.versions.utils.DependencyBuilder;
import org.codehaus.mojo.versions.utils.DependencyComparator;
import org.codehaus.mojo.versions.utils.PluginComparator;
import org.codehaus.mojo.versions.utils.RegexUtils;
Expand Down Expand Up @@ -641,15 +642,15 @@ public Artifact createPluginArtifact( String groupId, String artifactId, String
public Artifact createDependencyArtifact( String groupId, String artifactId, String version, String type,
String classifier, String scope, boolean optional )
{
Dependency dependency = new Dependency();
dependency.setGroupId( groupId );
dependency.setArtifactId( artifactId );
dependency.setType( type );
dependency.setClassifier( classifier );
dependency.setScope( scope );
dependency.setOptional( optional );
dependency.setVersion( StringUtils.isNotBlank( version ) ? version : "[0,]" );
return repositorySystem.createDependencyArtifact( dependency );
return repositorySystem.createDependencyArtifact( DependencyBuilder.newBuilder()
.withGroupId( groupId )
.withArtifactId( artifactId )
.withType( type )
.withClassifier( classifier )
.withScope( scope )
.withOptional( optional )
.withVersion( StringUtils.isNotBlank( version ) ? version : "[0,]" )
.build() );
}

@Override
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/org/codehaus/mojo/versions/api/PomHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
import org.codehaus.mojo.versions.utils.DependencyBuilder;
import org.codehaus.mojo.versions.utils.RegexUtils;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
Expand Down Expand Up @@ -1667,13 +1668,13 @@ public static List<Dependency> readImportedPOMsFromDependencyManagementSection(
{
if ( "pom".equals( depData.get( typeElement ) ) && "import".equals( depData.get( scopeElement ) ) )
{
Dependency dependency = new Dependency();
dependency.setGroupId( depData.get( groupIdElement ) );
dependency.setArtifactId( depData.get( artifactIdElement ) );
dependency.setVersion( depData.get( versionElement ) );
dependency.setType( depData.get( typeElement ) );
dependency.setScope( depData.get( scopeElement ) );
importedPOMs.add( dependency );
importedPOMs.add( DependencyBuilder.newBuilder()
.withGroupId( depData.get( groupIdElement ) )
.withArtifactId( depData.get( artifactIdElement ) )
.withVersion( depData.get( versionElement ) )
.withType( depData.get( typeElement ) )
.withScope( depData.get( scopeElement ) )
.build() );
}
depData.clear();
}
Expand Down
Loading

0 comments on commit 5cf148c

Please sign in to comment.