From e1e64786aa73910e396eb21b04179b21ece1a07a Mon Sep 17 00:00:00 2001 From: Andrzej Jarmoniuk Date: Mon, 12 Sep 2022 12:28:57 +0200 Subject: [PATCH] #687: Using the DependencyBuilder across the project --- ...AbstractVersionsDependencyUpdaterMojo.java | 15 +- .../versions/CompareDependenciesMojo.java | 11 +- .../versions/DisplayParentUpdatesMojo.java | 15 +- .../versions/DisplayPluginUpdatesMojo.java | 33 ++- .../mojo/versions/UpdateParentMojo.java | 14 +- .../mojo/versions/UseLatestReleasesMojo.java | 17 +- .../mojo/versions/UseLatestSnapshotsMojo.java | 18 +- .../mojo/versions/UseLatestVersionsMojo.java | 16 +- .../mojo/versions/UseReleasesMojo.java | 14 +- .../versions/api/DefaultVersionsHelper.java | 19 +- .../codehaus/mojo/versions/api/PomHelper.java | 15 +- .../versions/utils/DependencyBuilder.java | 211 ++++++++++++++++++ .../mojo/versions/DependencyBuilder.java | 133 ----------- .../versions/DependencyUpdatesReportTest.java | 2 + .../versions/UseLatestVersionsMojoTest.java | 1 + .../filtering/DependencyFilterTest.java | 2 +- .../filtering/TokenizedMatcherTest.java | 2 +- 17 files changed, 310 insertions(+), 228 deletions(-) create mode 100644 src/main/java/org/codehaus/mojo/versions/utils/DependencyBuilder.java delete mode 100644 src/test/java/org/codehaus/mojo/versions/DependencyBuilder.java diff --git a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsDependencyUpdaterMojo.java b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsDependencyUpdaterMojo.java index f3349023a5..6ebbdb09d8 100644 --- a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsDependencyUpdaterMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsDependencyUpdaterMojo.java @@ -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. @@ -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 ) diff --git a/src/main/java/org/codehaus/mojo/versions/CompareDependenciesMojo.java b/src/main/java/org/codehaus/mojo/versions/CompareDependenciesMojo.java index ad0a716887..4b2371769c 100644 --- a/src/main/java/org/codehaus/mojo/versions/CompareDependenciesMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/CompareDependenciesMojo.java @@ -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 @@ -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 { diff --git a/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java b/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java index dca737c981..7ef2a9167f 100644 --- a/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java @@ -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 @@ -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 diff --git a/src/main/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojo.java b/src/main/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojo.java index e4e931e3ee..f7cd5e8e4d 100644 --- a/src/main/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojo.java @@ -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; @@ -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; @@ -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 ); @@ -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 ); diff --git a/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java b/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java index 46db1a384a..50fcfdf27f 100644 --- a/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java @@ -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. @@ -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 diff --git a/src/main/java/org/codehaus/mojo/versions/UseLatestReleasesMojo.java b/src/main/java/org/codehaus/mojo/versions/UseLatestReleasesMojo.java index e4b09dd03b..501e622edf 100644 --- a/src/main/java/org/codehaus/mojo/versions/UseLatestReleasesMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UseLatestReleasesMojo.java @@ -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. @@ -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().getGroupId() ) + .withArtifactId( getProject().getParent().getArtifactId() ) + .withVersion( getProject().getParent().getVersion() ) + .withType( "pom" ) + .build() ) ); } } catch ( ArtifactMetadataRetrievalException e ) diff --git a/src/main/java/org/codehaus/mojo/versions/UseLatestSnapshotsMojo.java b/src/main/java/org/codehaus/mojo/versions/UseLatestSnapshotsMojo.java index 7f2cc749de..17567a4aab 100644 --- a/src/main/java/org/codehaus/mojo/versions/UseLatestSnapshotsMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UseLatestSnapshotsMojo.java @@ -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; @@ -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). @@ -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 ) diff --git a/src/main/java/org/codehaus/mojo/versions/UseLatestVersionsMojo.java b/src/main/java/org/codehaus/mojo/versions/UseLatestVersionsMojo.java index 55640b1c92..25380e4d3e 100644 --- a/src/main/java/org/codehaus/mojo/versions/UseLatestVersionsMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UseLatestVersionsMojo.java @@ -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; @@ -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. @@ -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 ) diff --git a/src/main/java/org/codehaus/mojo/versions/UseReleasesMojo.java b/src/main/java/org/codehaus/mojo/versions/UseReleasesMojo.java index bd25c628f8..0cb8c751c9 100644 --- a/src/main/java/org/codehaus/mojo/versions/UseReleasesMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UseReleasesMojo.java @@ -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). @@ -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; diff --git a/src/main/java/org/codehaus/mojo/versions/api/DefaultVersionsHelper.java b/src/main/java/org/codehaus/mojo/versions/api/DefaultVersionsHelper.java index 60108d96a1..3251311d6e 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/DefaultVersionsHelper.java +++ b/src/main/java/org/codehaus/mojo/versions/api/DefaultVersionsHelper.java @@ -81,6 +81,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; @@ -622,15 +623,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 diff --git a/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java b/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java index e3d625d459..0df81259c5 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java +++ b/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java @@ -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; @@ -1667,13 +1668,13 @@ public static List 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(); } diff --git a/src/main/java/org/codehaus/mojo/versions/utils/DependencyBuilder.java b/src/main/java/org/codehaus/mojo/versions/utils/DependencyBuilder.java new file mode 100644 index 0000000000..a0b4acc384 --- /dev/null +++ b/src/main/java/org/codehaus/mojo/versions/utils/DependencyBuilder.java @@ -0,0 +1,211 @@ +/* + * 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. + */ + +package org.codehaus.mojo.versions.utils; + +import java.util.Optional; + +import org.apache.maven.model.Dependency; + +import static java.util.Optional.empty; +import static java.util.Optional.of; +import static java.util.Optional.ofNullable; + +/** + * Builder class for {@linkplain Dependency} + */ +@SuppressWarnings( "OptionalUsedAsFieldOrParameterType" ) +public class DependencyBuilder +{ + private Optional groupId = empty(); + private Optional artifactId = empty(); + private Optional version = empty(); + private Optional type = empty(); + private Optional classifier = empty(); + private Optional scope = empty(); + private Optional optional = empty(); + + private DependencyBuilder() + { + } + + /** + * Passes groupId to the builder + * @param groupId given groupId + * @return builder instance + */ + public DependencyBuilder withGroupId( String groupId ) + { + this.groupId = ofNullable( groupId ); + return this; + } + + /** + * Passes artifactId to the builder + * @param artifactId given artifactId + * @return builder instance + */ + public DependencyBuilder withArtifactId( String artifactId ) + { + this.artifactId = ofNullable( artifactId ); + return this; + } + + /** + * Passes version to the builder + * @param version given version + * @return builder instance + */ + public DependencyBuilder withVersion( String version ) + { + this.version = ofNullable( version ); + return this; + } + + /** + * Passes type to the builder + * @param type given type + * @return builder instance + */ + public DependencyBuilder withType( String type ) + { + this.type = ofNullable( type ); + return this; + } + + /** + * Passes classifier to the builder + * @param classifier given classifier + * @return builder instance + */ + public DependencyBuilder withClassifier( String classifier ) + { + this.classifier = ofNullable( classifier ); + return this; + } + + /** + * Passes scope to the builder + * @param scope given scope + * @return builder instance + */ + public DependencyBuilder withScope( String scope ) + { + this.scope = ofNullable( scope ); + return this; + } + + /** + * Passes optional to the builder as String + * @param optional given optional as String + * @return builder instance + */ + public DependencyBuilder withOptional( String optional ) + { + this.optional = ofNullable( optional ); + return this; + } + + /** + * Passes optional to the builder as boolean + * @param optional given optional as boolean + * @return builder instance + */ + public DependencyBuilder withOptional( boolean optional ) + { + this.optional = of( String.valueOf( optional ) ); + return this; + } + + /** + * Creates a new instance of the builder + * @return new instance of the builder + */ + public static DependencyBuilder newBuilder() + { + return new DependencyBuilder(); + } + + /** + * Convenience factory method + * @return new instance of the builder + */ + public static DependencyBuilder newBuilder( String groupId, String artifactId, String version, String type, + String classifier, String scope ) + { + return newBuilder() + .withGroupId( groupId ) + .withArtifactId( artifactId ) + .withVersion( version ) + .withType( type ) + .withClassifier( classifier ) + .withScope( scope ); + } + + /** + * Convenience factory method + * @return new instance of the builder + */ + public static DependencyBuilder newBuilder( String groupId, String artifactId, String version ) + { + return newBuilder() + .withGroupId( groupId ) + .withArtifactId( artifactId ) + .withVersion( version ); + } + + /** + * Convenience builder method + * @return new instance of {@linkplain Dependency} + */ + public static Dependency dependencyWith( String groupId, String artifactId, String version ) + { + return newBuilder( groupId, artifactId, version ) + .build(); + } + + /** + * Convenience builder method + * @return new instance of {@linkplain Dependency} + */ + public static Dependency dependencyWith( String groupId, String artifactId, String version, String type, + String classifier, String scope ) + { + return newBuilder( groupId, artifactId, version, type, classifier, scope ) + .build(); + } + + /** + * Builds the {@linkplain Dependency} instance + * @return {@linkplain Dependency} instance + */ + public Dependency build() + { + Dependency inst = new Dependency(); + groupId.ifPresent( inst::setGroupId ); + artifactId.ifPresent( inst::setArtifactId ); + version.ifPresent( inst::setVersion ); + type.ifPresent( inst::setType ); + classifier.ifPresent( inst::setClassifier ); + scope.ifPresent( inst::setScope ); + optional.ifPresent( inst::setOptional ); + + return inst; + } +} diff --git a/src/test/java/org/codehaus/mojo/versions/DependencyBuilder.java b/src/test/java/org/codehaus/mojo/versions/DependencyBuilder.java deleted file mode 100644 index 05bf3817b1..0000000000 --- a/src/test/java/org/codehaus/mojo/versions/DependencyBuilder.java +++ /dev/null @@ -1,133 +0,0 @@ -package org.codehaus.mojo.versions; - -import org.apache.maven.model.Dependency; - -public class DependencyBuilder -{ - - public String getGroupId() - { - return groupId; - } - - public DependencyBuilder withGroupId( String groupId ) - { - this.groupId = groupId; - return this; - } - - public String getArtifactId() - { - return artifactId; - } - - public DependencyBuilder withArtifactId( String artifactId ) - { - this.artifactId = artifactId; - return this; - } - - public String getVersion() - { - return version; - } - - public DependencyBuilder withVersion( String version ) - { - this.version = version; - return this; - } - - public String getType() - { - return type; - } - - public DependencyBuilder withType( String type ) - { - this.type = type; - return this; - } - - public String getClassifier() - { - return classifier; - } - - public DependencyBuilder withClassifier( String classifier ) - { - this.classifier = classifier; - return this; - } - - public String getScope() - { - return scope; - } - - public DependencyBuilder withScope( String scope ) - { - this.scope = scope; - return this; - } - - private String groupId = null; - private String artifactId = null; - private String version = null; - private String type = null; - private String classifier = null; - private String scope = null; - - public static DependencyBuilder newBuilder() - { - return new DependencyBuilder(); - } - - public static DependencyBuilder newBuilder( String groupId, String artifactId, String version, String type, - String classifier, String scope ) - { - return newBuilder() - .withGroupId( groupId ) - .withArtifactId( artifactId ) - .withVersion( version ) - .withType( type ) - .withClassifier( classifier ) - .withScope( scope ); - } - - public static DependencyBuilder newBuilder( String groupId, String artifactId, String version ) - { - return newBuilder() - .withGroupId( groupId ) - .withArtifactId( artifactId ) - .withVersion( version ); - } - - public static Dependency dependencyWith( String groupId, String artifactId, String version ) - { - return newBuilder( groupId, artifactId, version ) - .build(); - } - - public static Dependency dependencyWith( String groupId, String artifactId, String version, String type, - String classifier, String scope ) - { - return newBuilder( groupId, artifactId, version, type, classifier, scope ) - .build(); - } - - - public Dependency build() - { - Dependency dep = new Dependency(); - dep.setGroupId( groupId ); - dep.setArtifactId( artifactId ); - dep.setVersion( version ); - dep.setType( type ); - dep.setClassifier( classifier ); - dep.setScope( scope ); - - return dep; - } - -} diff --git a/src/test/java/org/codehaus/mojo/versions/DependencyUpdatesReportTest.java b/src/test/java/org/codehaus/mojo/versions/DependencyUpdatesReportTest.java index d3ee871aaf..02bd99b3e0 100644 --- a/src/test/java/org/codehaus/mojo/versions/DependencyUpdatesReportTest.java +++ b/src/test/java/org/codehaus/mojo/versions/DependencyUpdatesReportTest.java @@ -39,6 +39,7 @@ import org.apache.maven.project.MavenProject; import org.apache.maven.reporting.MavenReportException; import org.apache.maven.repository.RepositorySystem; +import org.codehaus.mojo.versions.utils.DependencyBuilder; import org.codehaus.mojo.versions.model.RuleSet; import org.codehaus.plexus.i18n.I18N; import org.junit.Test; @@ -64,6 +65,7 @@ public class DependencyUpdatesReportTest { private static class TestDependencyUpdatesReport extends DependencyUpdatesReport { + @SuppressWarnings( "deprecation" ) TestDependencyUpdatesReport() { mockPlexusComponents(); diff --git a/src/test/java/org/codehaus/mojo/versions/UseLatestVersionsMojoTest.java b/src/test/java/org/codehaus/mojo/versions/UseLatestVersionsMojoTest.java index daac6a2106..6072d925ee 100644 --- a/src/test/java/org/codehaus/mojo/versions/UseLatestVersionsMojoTest.java +++ b/src/test/java/org/codehaus/mojo/versions/UseLatestVersionsMojoTest.java @@ -17,6 +17,7 @@ import org.apache.maven.repository.RepositorySystem; import org.codehaus.mojo.versions.api.PomHelper; import org.codehaus.mojo.versions.change.VersionChange; +import org.codehaus.mojo.versions.utils.DependencyBuilder; import org.codehaus.mojo.versions.utils.TestChangeRecorder; import org.hamcrest.core.Is; import org.junit.Before; diff --git a/src/test/java/org/codehaus/mojo/versions/filtering/DependencyFilterTest.java b/src/test/java/org/codehaus/mojo/versions/filtering/DependencyFilterTest.java index aca6d8697a..9458df0b20 100644 --- a/src/test/java/org/codehaus/mojo/versions/filtering/DependencyFilterTest.java +++ b/src/test/java/org/codehaus/mojo/versions/filtering/DependencyFilterTest.java @@ -4,7 +4,7 @@ import java.util.Set; import org.apache.maven.model.Dependency; -import org.codehaus.mojo.versions.DependencyBuilder; +import org.codehaus.mojo.versions.utils.DependencyBuilder; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; diff --git a/src/test/java/org/codehaus/mojo/versions/filtering/TokenizedMatcherTest.java b/src/test/java/org/codehaus/mojo/versions/filtering/TokenizedMatcherTest.java index e5ad0f2249..03b4715b40 100644 --- a/src/test/java/org/codehaus/mojo/versions/filtering/TokenizedMatcherTest.java +++ b/src/test/java/org/codehaus/mojo/versions/filtering/TokenizedMatcherTest.java @@ -1,7 +1,7 @@ package org.codehaus.mojo.versions.filtering; import org.apache.maven.model.Dependency; -import org.codehaus.mojo.versions.DependencyBuilder; +import org.codehaus.mojo.versions.utils.DependencyBuilder; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest;