From 127651da2926e87fe62e6c2830d69340db3a6cb7 Mon Sep 17 00:00:00 2001 From: Andrzej Jarmoniuk Date: Fri, 9 Sep 2022 17:42:24 +0200 Subject: [PATCH] Implementing #684: allow providing ruleSet in POM --- .../invoker.properties | 7 +- .../test.properties | 2 - src/it/it-compare-dependencies-001/verify.bsh | 21 -- .../it-compare-dependencies-001/verify.groovy | 2 + .../invoker.properties | 1 + .../pom.xml | 51 ++++ .../verify.groovy | 2 + .../invoker.properties | 6 +- .../verify.bsh | 25 -- .../verify.groovy | 7 + .../verify.groovy | 8 +- .../invoker.properties | 5 + .../pom.xml | 61 +++++ .../verify.groovy | 8 + .../invoker.properties | 7 +- .../it-display-plugin-updates-001/verify.bsh | 25 -- .../verify.groovy | 6 + .../invoker.properties | 5 + .../pom.xml | 57 +++++ .../verify.groovy | 8 + .../invoker.properties | 6 +- .../verify.bsh | 25 -- .../verify.groovy | 6 + .../invoker.properties | 6 + .../pom.xml | 59 +++++ .../verify.groovy | 8 + .../invoker.properties | 1 + .../pom.xml | 88 +++++++ .../verify.groovy | 6 + .../invoker.properties | 1 + .../pom.xml | 88 +++++++ .../verify.groovy | 6 + .../versions/AbstractVersionsDisplayMojo.java | 8 +- .../mojo/versions/AbstractVersionsReport.java | 47 +++- .../versions/AbstractVersionsUpdaterMojo.java | 47 +++- .../versions/DisplayPluginUpdatesMojo.java | 1 - .../versions/api/AbstractVersionDetails.java | 3 - .../api/DefaultArtifactAssociation.java | 7 +- .../versions/api/DefaultVersionsHelper.java | 221 +++++++++++++++++- .../mojo/versions/change/VersionChange.java | 7 +- .../versions/ordering/VersionComparators.java | 3 +- .../recording/ChangeRecorderNull.java | 3 +- src/site/apt/version-rules.apt.vm | 81 ++++++- .../AbstractVersionsUpdaterMojoTest.java | 2 - .../versions/DependencyUpdatesReportTest.java | 66 +++--- .../DisplayDependencyUpdatesMojoTest.java | 80 ++++++- .../versions/PluginUpdatesReportTest.java | 68 +++--- .../mojo/versions/UpdateParentMojoTest.java | 61 +++-- .../versions/UseLatestVersionsMojoTest.java | 35 +-- .../api/DefaultVersionsHelperTest.java | 66 +++++- .../versions/model/TestIgnoreVersions.java | 93 ++++++++ .../mojo/versions/utils/MockUtils.java | 86 +++++++ .../utils/ModifiedPomXMLEventReaderUtils.java | 62 +++++ .../ruleset/pom.xml | 57 +++++ .../display-plugin-updates/ruleset/pom.xml | 45 ++++ 55 files changed, 1480 insertions(+), 283 deletions(-) delete mode 100644 src/it/it-compare-dependencies-001/test.properties delete mode 100644 src/it/it-compare-dependencies-001/verify.bsh create mode 100644 src/it/it-compare-dependencies-001/verify.groovy create mode 100644 src/it/it-dependency-updates-report-issue-684-001/invoker.properties create mode 100644 src/it/it-dependency-updates-report-issue-684-001/pom.xml create mode 100644 src/it/it-dependency-updates-report-issue-684-001/verify.groovy delete mode 100644 src/it/it-display-dependency-updates-001/verify.bsh create mode 100644 src/it/it-display-dependency-updates-001/verify.groovy create mode 100644 src/it/it-display-dependency-updates-issue-684-pom-based-rules/invoker.properties create mode 100644 src/it/it-display-dependency-updates-issue-684-pom-based-rules/pom.xml create mode 100644 src/it/it-display-dependency-updates-issue-684-pom-based-rules/verify.groovy delete mode 100644 src/it/it-display-plugin-updates-001/verify.bsh create mode 100644 src/it/it-display-plugin-updates-001/verify.groovy create mode 100644 src/it/it-display-plugin-updates-issue-684-pom-based-rules/invoker.properties create mode 100644 src/it/it-display-plugin-updates-issue-684-pom-based-rules/pom.xml create mode 100644 src/it/it-display-plugin-updates-issue-684-pom-based-rules/verify.groovy delete mode 100644 src/it/it-display-property-updates-001/verify.bsh create mode 100644 src/it/it-display-property-updates-001/verify.groovy create mode 100644 src/it/it-display-property-updates-issue-684-pom-based-rules/invoker.properties create mode 100644 src/it/it-display-property-updates-issue-684-pom-based-rules/pom.xml create mode 100644 src/it/it-display-property-updates-issue-684-pom-based-rules/verify.groovy create mode 100644 src/it/it-plugin-updates-report-issue-684-001/invoker.properties create mode 100644 src/it/it-plugin-updates-report-issue-684-001/pom.xml create mode 100644 src/it/it-plugin-updates-report-issue-684-001/verify.groovy create mode 100644 src/it/it-property-updates-report-issue-684-001/invoker.properties create mode 100644 src/it/it-property-updates-report-issue-684-001/pom.xml create mode 100644 src/it/it-property-updates-report-issue-684-001/verify.groovy create mode 100644 src/test/java/org/codehaus/mojo/versions/model/TestIgnoreVersions.java create mode 100644 src/test/java/org/codehaus/mojo/versions/utils/MockUtils.java create mode 100644 src/test/java/org/codehaus/mojo/versions/utils/ModifiedPomXMLEventReaderUtils.java create mode 100644 src/test/resources/org/codehaus/mojo/display-dependency-updates/ruleset/pom.xml create mode 100644 src/test/resources/org/codehaus/mojo/display-plugin-updates/ruleset/pom.xml diff --git a/src/it/it-compare-dependencies-001/invoker.properties b/src/it/it-compare-dependencies-001/invoker.properties index d532067cf6..f3599feb2b 100644 --- a/src/it/it-compare-dependencies-001/invoker.properties +++ b/src/it/it-compare-dependencies-001/invoker.properties @@ -1,2 +1,5 @@ -invoker.goals=${project.groupId}:${project.artifactId}:${project.version}:compare-dependencies -invoker.systemPropertiesFile = test.properties +invoker.goals.1 = ${project.groupId}:${project.artifactId}:${project.version}:compare-dependencies +invoker.mavenOpts.1 = -DreportOutputFile=depDiffs-1.txt -DremotePom=localhost:dummy-bom-pom:1.0 + +invoker.goals.2 = ${project.groupId}:${project.artifactId}:${project.version}:compare-dependencies +invoker.mavenOpts.2 = -DreportOutputFile=depDiffs-1.txt -DremotePom=localhost:dummy-bom-pom:1.0 \ No newline at end of file diff --git a/src/it/it-compare-dependencies-001/test.properties b/src/it/it-compare-dependencies-001/test.properties deleted file mode 100644 index f22fd874f8..0000000000 --- a/src/it/it-compare-dependencies-001/test.properties +++ /dev/null @@ -1,2 +0,0 @@ -remotePom=localhost:dummy-bom-pom:1.0 -reportOutputFile=target/depDiffs.txt \ No newline at end of file diff --git a/src/it/it-compare-dependencies-001/verify.bsh b/src/it/it-compare-dependencies-001/verify.bsh deleted file mode 100644 index 7318644353..0000000000 --- a/src/it/it-compare-dependencies-001/verify.bsh +++ /dev/null @@ -1,21 +0,0 @@ -import java.io.*; -import org.codehaus.plexus.util.FileUtils; - -try -{ - File file = new File( basedir, "target/depDiffs.txt" ); - String buf = FileUtils.fileRead( file, "UTF-8" ); - - if ( buf.indexOf( "2.0.10 -> 2.0.9" ) < 0 ) - { - System.err.println( "Version diff in maven artifact not found" ); - return false; - } -} -catch( Throwable t ) -{ - t.printStackTrace(); - return false; -} - -return true; diff --git a/src/it/it-compare-dependencies-001/verify.groovy b/src/it/it-compare-dependencies-001/verify.groovy new file mode 100644 index 0000000000..19e6fd597b --- /dev/null +++ b/src/it/it-compare-dependencies-001/verify.groovy @@ -0,0 +1,2 @@ +output = new File(basedir, "depDiffs-1.txt").text +assert output =~ /\b2.0.10 -> 2.0.9\b/ \ No newline at end of file diff --git a/src/it/it-dependency-updates-report-issue-684-001/invoker.properties b/src/it/it-dependency-updates-report-issue-684-001/invoker.properties new file mode 100644 index 0000000000..5c40bc6580 --- /dev/null +++ b/src/it/it-dependency-updates-report-issue-684-001/invoker.properties @@ -0,0 +1 @@ +invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:dependency-updates-report \ No newline at end of file diff --git a/src/it/it-dependency-updates-report-issue-684-001/pom.xml b/src/it/it-dependency-updates-report-issue-684-001/pom.xml new file mode 100644 index 0000000000..273e1134af --- /dev/null +++ b/src/it/it-dependency-updates-report-issue-684-001/pom.xml @@ -0,0 +1,51 @@ + + 4.0.0 + localhost + it-abstract-versions-report-001 + 1.0 + pom + abstract-versions-report + http://localhost/ + + + Testing command line invocation of dependency- and plugin-updates-report + + + + + localhost + dummy-api + 1.1 + + + + + + + localhost + dummy-maven-plugin + 1.0 + + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + + + 3.0 + + + + + + + + + + diff --git a/src/it/it-dependency-updates-report-issue-684-001/verify.groovy b/src/it/it-dependency-updates-report-issue-684-001/verify.groovy new file mode 100644 index 0000000000..71f272ef12 --- /dev/null +++ b/src/it/it-dependency-updates-report-issue-684-001/verify.groovy @@ -0,0 +1,2 @@ +output = new File( basedir, "target/site/dependency-updates-report.html" ).text +assert ! ( output =~ /\b3.0\b/ ) \ No newline at end of file diff --git a/src/it/it-display-dependency-updates-001/invoker.properties b/src/it/it-display-dependency-updates-001/invoker.properties index d065873965..0fadb3d2cf 100644 --- a/src/it/it-display-dependency-updates-001/invoker.properties +++ b/src/it/it-display-dependency-updates-001/invoker.properties @@ -1 +1,5 @@ -invoker.goals=${project.groupId}:${project.artifactId}:${project.version}:display-dependency-updates +invoker.goals.1 = ${project.groupId}:${project.artifactId}:${project.version}:display-dependency-updates +invoker.mavenOpts.1 = -Dversions.outputFile=./output1.txt -DoutputEncoding=UTF-8 + +invoker.goals.2 = ${project.groupId}:${project.artifactId}:${project.version}:display-dependency-updates +invoker.mavenOpts.2 = -Dversions.outputFile=./output2.txt -DoutputEncoding=UTF-8 -Dmaven.version.ignore=3.0 \ No newline at end of file diff --git a/src/it/it-display-dependency-updates-001/verify.bsh b/src/it/it-display-dependency-updates-001/verify.bsh deleted file mode 100644 index 844bf2d3eb..0000000000 --- a/src/it/it-display-dependency-updates-001/verify.bsh +++ /dev/null @@ -1,25 +0,0 @@ -import java.io.*; -import org.codehaus.plexus.util.FileUtils; -import java.util.regex.*; - -try -{ - File file = new File( basedir, "build.log" ); - String buf = FileUtils.fileRead( file ); - - Pattern p = Pattern.compile( "\\Qlocalhost:dummy-api\\E\\s*\\.*\\s*1\\.1\\s+->\\s+3\\.0" ); - Matcher m = p.matcher( buf.toString() ); - if ( !m.find() ) - { - System.out.println( "Did not suggest updating dummy-api to version 3.0" ); - return false; - } - System.out.println( m.group( 0 ) ); -} -catch( Throwable t ) -{ - t.printStackTrace(); - return false; -} - -return true; diff --git a/src/it/it-display-dependency-updates-001/verify.groovy b/src/it/it-display-dependency-updates-001/verify.groovy new file mode 100644 index 0000000000..e89ba30a0c --- /dev/null +++ b/src/it/it-display-dependency-updates-001/verify.groovy @@ -0,0 +1,7 @@ +output = new File(basedir, "output1.txt").text +assert output =~ /\Qlocalhost:dummy-api\E\s*\.*\s*1\.1\s+->\s+3\.0\b/ + +output = new File(basedir, "output2.txt").text +assert ! ( output =~ /\Qlocalhost:dummy-api\E\s*\.*\s*1\.1\s+->\s+3\.0\b/ ) +assert output =~ /\Qlocalhost:dummy-api\E\s*\.*\s*1\.1\s+->\s+2\.1\b/ + diff --git a/src/it/it-display-dependency-updates-issue-258-dependencyIncludesAndExcludes/verify.groovy b/src/it/it-display-dependency-updates-issue-258-dependencyIncludesAndExcludes/verify.groovy index cdce154d8a..d4e3cc4e8e 100644 --- a/src/it/it-display-dependency-updates-issue-258-dependencyIncludesAndExcludes/verify.groovy +++ b/src/it/it-display-dependency-updates-issue-258-dependencyIncludesAndExcludes/verify.groovy @@ -1,17 +1,17 @@ output = new File(basedir, "output1.txt").text assert output =~ /localhost:dummy-api/ -assert output !=~ /localhost:dummy-impl/ +assert ! ( output =~ /localhost:dummy-impl/ ) output = new File(basedir, "output2.txt").text -assert output !=~ /localhost:dummy-api/ +assert ! ( output =~ /localhost:dummy-api/ ) assert output =~ /localhost:dummy-impl/ output = new File(basedir, "output3.txt").text assert output =~ /localhost:dummy-impl/ -assert output !=~ /localhost:dummy-parent2/ +assert ! ( output =~ /localhost:dummy-parent2/ ) output = new File(basedir, "output4.txt").text -assert output !=~ /localhost:dummy-impl/ +assert ! ( output =~ /localhost:dummy-impl/ ) assert output =~ /localhost:dummy-parent2/ output = new File(basedir, "output5.txt") diff --git a/src/it/it-display-dependency-updates-issue-684-pom-based-rules/invoker.properties b/src/it/it-display-dependency-updates-issue-684-pom-based-rules/invoker.properties new file mode 100644 index 0000000000..437c1608b5 --- /dev/null +++ b/src/it/it-display-dependency-updates-issue-684-pom-based-rules/invoker.properties @@ -0,0 +1,5 @@ +invoker.goals.1 = ${project.groupId}:${project.artifactId}:${project.version}:display-dependency-updates +invoker.mavenOpts.1 = -Dversions.outputFile=./output1.txt -DoutputEncoding=UTF-8 + +invoker.goals.2 = ${project.groupId}:${project.artifactId}:${project.version}:display-dependency-updates +invoker.mavenOpts.2 = -Dversions.outputFile=./output2.txt -DoutputEncoding=UTF-8 -Dmaven.version.ignore=2.1 \ No newline at end of file diff --git a/src/it/it-display-dependency-updates-issue-684-pom-based-rules/pom.xml b/src/it/it-display-dependency-updates-issue-684-pom-based-rules/pom.xml new file mode 100644 index 0000000000..b3023c5055 --- /dev/null +++ b/src/it/it-display-dependency-updates-issue-684-pom-based-rules/pom.xml @@ -0,0 +1,61 @@ + + 4.0.0 + localhost + it-display-dependency-updates-issue-684-pom-based-rules + 1.0 + pom + + + + localhost + dummy-api + 1.1 + + + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + + + 3.0 + + + + + + + localhost + dummy-maven-plugin + 1.0 + + + maven-clean-plugin + 2.2 + + + maven-deploy-plugin + 2.3 + + + maven-install-plugin + 2.2 + + + maven-site-plugin + 2.0 + + + maven-project-info-reports-plugin + 2.1 + + + + + diff --git a/src/it/it-display-dependency-updates-issue-684-pom-based-rules/verify.groovy b/src/it/it-display-dependency-updates-issue-684-pom-based-rules/verify.groovy new file mode 100644 index 0000000000..4c0b020767 --- /dev/null +++ b/src/it/it-display-dependency-updates-issue-684-pom-based-rules/verify.groovy @@ -0,0 +1,8 @@ +output = new File(basedir, "output1.txt").text +assert ! ( output =~ /\Qlocalhost:dummy-api\E\s*\.*\s*1\.1\s+->\s+3\.0\b/ ) +assert output =~ /\Qlocalhost:dummy-api\E\s*\.*\s*1\.1\s+->\s+2\.1\b/ + +output = new File(basedir, "output2.txt").text +assert ! ( output =~ /\Qlocalhost:dummy-api\E\s*\.*\s*1\.1\s+->\s+2\.1\b/ ) +assert output =~ /\Qlocalhost:dummy-api\E\s*\.*\s*1\.1\s+->\s+2\.0\b/ + diff --git a/src/it/it-display-plugin-updates-001/invoker.properties b/src/it/it-display-plugin-updates-001/invoker.properties index 3e9221daf4..bef19502c7 100644 --- a/src/it/it-display-plugin-updates-001/invoker.properties +++ b/src/it/it-display-plugin-updates-001/invoker.properties @@ -1 +1,6 @@ -invoker.goals=${project.groupId}:${project.artifactId}:${project.version}:display-plugin-updates +invoker.goals.1=${project.groupId}:${project.artifactId}:${project.version}:display-plugin-updates +invoker.mavenOpts.1 = -Dversions.outputFile=./output1.txt -DoutputEncoding=UTF-8 + +invoker.goals.2=${project.groupId}:${project.artifactId}:${project.version}:display-plugin-updates +invoker.mavenOpts.2 = -Dversions.outputFile=./output2.txt -DoutputEncoding=UTF-8 -Dmaven.version.ignore=3.0 + diff --git a/src/it/it-display-plugin-updates-001/verify.bsh b/src/it/it-display-plugin-updates-001/verify.bsh deleted file mode 100644 index 6d63188f17..0000000000 --- a/src/it/it-display-plugin-updates-001/verify.bsh +++ /dev/null @@ -1,25 +0,0 @@ -import java.io.*; -import org.codehaus.plexus.util.FileUtils; -import java.util.regex.*; - -try -{ - File file = new File( basedir, "build.log" ); - String buf = FileUtils.fileRead( file ); - - Pattern p = Pattern.compile( "\\Qlocalhost:dummy-maven-plugin\\E\\s*\\.*\\s*1\\.0\\s+->\\s+3\\.0" ); - Matcher m = p.matcher( buf.toString() ); - if ( !m.find() ) - { - System.out.println( "Did not suggest updating dummy-maven-plugin to version 3.0" ); - return false; - } - System.out.println( m.group( 0 ) ); -} -catch( Throwable t ) -{ - t.printStackTrace(); - return false; -} - -return true; diff --git a/src/it/it-display-plugin-updates-001/verify.groovy b/src/it/it-display-plugin-updates-001/verify.groovy new file mode 100644 index 0000000000..765fed6879 --- /dev/null +++ b/src/it/it-display-plugin-updates-001/verify.groovy @@ -0,0 +1,6 @@ +output = new File(basedir, "output1.txt").text +assert output =~ /\Qlocalhost:dummy-maven-plugin\E\s*\.*\s*1\.0\s+->\s+3\.0\b/ + +output = new File(basedir, "output2.txt").text +assert ! ( output =~ /\Qlocalhost:dummy-maven-plugin\E\s*\.*\s*1\.0\s+->\s+3\.0\b/ ) +assert output =~ /\Qlocalhost:dummy-maven-plugin\E\s*\.*\s*1\.0\s+->\s+2\.1\b/ diff --git a/src/it/it-display-plugin-updates-issue-684-pom-based-rules/invoker.properties b/src/it/it-display-plugin-updates-issue-684-pom-based-rules/invoker.properties new file mode 100644 index 0000000000..823d575432 --- /dev/null +++ b/src/it/it-display-plugin-updates-issue-684-pom-based-rules/invoker.properties @@ -0,0 +1,5 @@ +invoker.goals.1 = ${project.groupId}:${project.artifactId}:${project.version}:display-plugin-updates +invoker.mavenOpts.1 = -Dversions.outputFile=./output1.txt -DoutputEncoding=UTF-8 + +invoker.goals.2 = ${project.groupId}:${project.artifactId}:${project.version}:display-plugin-updates +invoker.mavenOpts.2 = -Dversions.outputFile=./output2.txt -DoutputEncoding=UTF-8 -Dmaven.version.ignore=2.1 \ No newline at end of file diff --git a/src/it/it-display-plugin-updates-issue-684-pom-based-rules/pom.xml b/src/it/it-display-plugin-updates-issue-684-pom-based-rules/pom.xml new file mode 100644 index 0000000000..65659c4679 --- /dev/null +++ b/src/it/it-display-plugin-updates-issue-684-pom-based-rules/pom.xml @@ -0,0 +1,57 @@ + + 4.0.0 + + localhost + it-101 + 1.0 + pom + display-plugin-updates + + + 2.0.6 + + + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + + + 3.0 + + + + + + + localhost + dummy-maven-plugin + 1.0 + + + maven-clean-plugin + 2.2 + + + maven-deploy-plugin + 2.3 + + + maven-install-plugin + 2.2 + + + maven-site-plugin + 2.0 + + + + + + diff --git a/src/it/it-display-plugin-updates-issue-684-pom-based-rules/verify.groovy b/src/it/it-display-plugin-updates-issue-684-pom-based-rules/verify.groovy new file mode 100644 index 0000000000..6e0ecd6e65 --- /dev/null +++ b/src/it/it-display-plugin-updates-issue-684-pom-based-rules/verify.groovy @@ -0,0 +1,8 @@ +output = new File(basedir, "output1.txt").text +assert ! ( output =~ /\Qlocalhost:dummy-maven-plugin\E\s*\.*\s*1\.0\s+->\s+3\.0\b/ ) +assert output =~ /\Qlocalhost:dummy-maven-plugin\E\s*\.*\s*1\.0\s+->\s+2\.1\b/ + +output = new File(basedir, "output2.txt").text +assert ! ( output =~ /\Qlocalhost:dummy-maven-plugin\E\s*\.*\s*1\.0\s+->\s+2\.1\b/ ) +assert output =~ /\Qlocalhost:dummy-maven-plugin\E\s*\.*\s*1\.0\s+->\s+2\.0\b/ + diff --git a/src/it/it-display-property-updates-001/invoker.properties b/src/it/it-display-property-updates-001/invoker.properties index 27e7c440cf..2b9ac63e46 100644 --- a/src/it/it-display-property-updates-001/invoker.properties +++ b/src/it/it-display-property-updates-001/invoker.properties @@ -1 +1,5 @@ -invoker.goals=-DautoLinkItems=true ${project.groupId}:${project.artifactId}:${project.version}:display-property-updates +invoker.goals.1 = ${project.groupId}:${project.artifactId}:${project.version}:display-property-updates +invoker.mavenOpts.1 = -Dversions.outputFile=./output1.txt -DoutputEncoding=UTF-8 -DautoLinkItems=true + +invoker.goals.2 = ${project.groupId}:${project.artifactId}:${project.version}:display-property-updates +invoker.mavenOpts.2 = -Dversions.outputFile=./output2.txt -DoutputEncoding=UTF-8 -DautoLinkItems=true -Dmaven.version.ignore=2.0 diff --git a/src/it/it-display-property-updates-001/verify.bsh b/src/it/it-display-property-updates-001/verify.bsh deleted file mode 100644 index d3e8c541e3..0000000000 --- a/src/it/it-display-property-updates-001/verify.bsh +++ /dev/null @@ -1,25 +0,0 @@ -import java.io.*; -import org.codehaus.plexus.util.FileUtils; -import java.util.regex.*; - -try -{ - File file = new File( basedir, "build.log" ); - String buf = FileUtils.fileRead( file ); - - Pattern p = Pattern.compile( "\\Q${api}\\E\\s*\\.*\\s*1\\.0\\s+->\\s+2\\.0" ); - Matcher m = p.matcher( buf.toString() ); - if ( !m.find() ) - { - System.out.println( "Did not suggest updating ${api} to version 2.0" ); - return false; - } - System.out.println( m.group( 0 ) ); -} -catch( Throwable t ) -{ - t.printStackTrace(); - return false; -} - -return true; diff --git a/src/it/it-display-property-updates-001/verify.groovy b/src/it/it-display-property-updates-001/verify.groovy new file mode 100644 index 0000000000..bb33024633 --- /dev/null +++ b/src/it/it-display-property-updates-001/verify.groovy @@ -0,0 +1,6 @@ +output = new File(basedir, "output1.txt").text +assert output =~ /\Q\u0024{api}\E\s*\.*\s*1\.0\s+->\s+2\.0\b/ + +output = new File(basedir, "output2.txt").text +assert ! ( output =~ /\Q\u0024{api}\E\s*\.*\s*1\.0\s+->\s+2\.0\b/ ) + diff --git a/src/it/it-display-property-updates-issue-684-pom-based-rules/invoker.properties b/src/it/it-display-property-updates-issue-684-pom-based-rules/invoker.properties new file mode 100644 index 0000000000..13001f36f9 --- /dev/null +++ b/src/it/it-display-property-updates-issue-684-pom-based-rules/invoker.properties @@ -0,0 +1,6 @@ +invoker.goals.1 = ${project.groupId}:${project.artifactId}:${project.version}:display-property-updates +invoker.mavenOpts.1 = -Dversions.outputFile=./output1.txt -DoutputEncoding=UTF-8 -DautoLinkItems=true + +invoker.goals.2 = ${project.groupId}:${project.artifactId}:${project.version}:display-property-updates +invoker.mavenOpts.2 = -Dversions.outputFile=./output2.txt -DoutputEncoding=UTF-8 -DautoLinkItems=true -Dmaven.version.ignore=1.3 + diff --git a/src/it/it-display-property-updates-issue-684-pom-based-rules/pom.xml b/src/it/it-display-property-updates-issue-684-pom-based-rules/pom.xml new file mode 100644 index 0000000000..27f167411b --- /dev/null +++ b/src/it/it-display-property-updates-issue-684-pom-based-rules/pom.xml @@ -0,0 +1,59 @@ + + 4.0.0 + + localhost + it-004 + 1.0 + pom + update-properties with one property auto-linked and range limited + + + 1.0 + + + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + + + 2.0 + + + + + + + maven-clean-plugin + 2.2 + + + localhost + dummy-api + [${api},2.1-!) + + + + + maven-deploy-plugin + 2.3 + + + maven-install-plugin + 2.2 + + + maven-site-plugin + 2.0 + + + + + + diff --git a/src/it/it-display-property-updates-issue-684-pom-based-rules/verify.groovy b/src/it/it-display-property-updates-issue-684-pom-based-rules/verify.groovy new file mode 100644 index 0000000000..c21a066bd8 --- /dev/null +++ b/src/it/it-display-property-updates-issue-684-pom-based-rules/verify.groovy @@ -0,0 +1,8 @@ +output = new File(basedir, "output1.txt").text +assert ! ( output =~ /\Q\u0024{api}\E\s*\.*\s*1\.0\s+->\s+2\.0\b/ ) +assert output =~ /\Q\u0024{api}\E\s*\.*\s*1\.0\s+->\s+1\.3\b/ + +output = new File(basedir, "output2.txt").text +assert ! ( output =~ /\Q\u0024{api}\E\s*\.*\s*1\.0\s+->\s+2\.0\b/ ) +assert ! ( output =~ /\Q\u0024{api}\E\s*\.*\s*1\.0\s+->\s+1\.3\b/ ) +assert output =~ /\Q\u0024{api}\E\s*\.*\s*1\.0\s+->\s+1\.2.2\b/ \ No newline at end of file diff --git a/src/it/it-plugin-updates-report-issue-684-001/invoker.properties b/src/it/it-plugin-updates-report-issue-684-001/invoker.properties new file mode 100644 index 0000000000..8fd0c8aa9d --- /dev/null +++ b/src/it/it-plugin-updates-report-issue-684-001/invoker.properties @@ -0,0 +1 @@ +invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:plugin-updates-report \ No newline at end of file diff --git a/src/it/it-plugin-updates-report-issue-684-001/pom.xml b/src/it/it-plugin-updates-report-issue-684-001/pom.xml new file mode 100644 index 0000000000..e641ff8f50 --- /dev/null +++ b/src/it/it-plugin-updates-report-issue-684-001/pom.xml @@ -0,0 +1,88 @@ + + 4.0.0 + localhost + it-dependency-updates-report-001 + 1.0 + pom + plugin-updates-report + http://localhost/ + + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + + + 3.1 + + + + + + + localhost + dummy-maven-plugin + 1.0 + + + localhost + dummy-api + 1.1 + + + + + maven-clean-plugin + 2.2 + + + maven-deploy-plugin + 2.3 + + + maven-install-plugin + 2.2 + + + maven-site-plugin + @sitePluginVersion@ + + + maven-project-info-reports-plugin + 2.1 + + + + + + + + + maven-project-info-reports-plugin + 2.1 + + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + + plugin-updates-report + + + + + + + + \ No newline at end of file diff --git a/src/it/it-plugin-updates-report-issue-684-001/verify.groovy b/src/it/it-plugin-updates-report-issue-684-001/verify.groovy new file mode 100644 index 0000000000..e1571cc591 --- /dev/null +++ b/src/it/it-plugin-updates-report-issue-684-001/verify.groovy @@ -0,0 +1,6 @@ +output = new File( basedir, 'target/site/plugin-updates-report.html' ).text + .replaceAll( '<[^>]+>', ' ' ) + .replaceAll( '&[^;]+;', ' ' ) + .replaceAll( '\\s+', ' ' ) +assert ! ( output =~ /\blocalhost dummy-maven-plugin 1.0 3.1\b/ ) +assert output =~ /\blocalhost dummy-maven-plugin 1.0 3.0\b/ \ No newline at end of file diff --git a/src/it/it-property-updates-report-issue-684-001/invoker.properties b/src/it/it-property-updates-report-issue-684-001/invoker.properties new file mode 100644 index 0000000000..8fd0c8aa9d --- /dev/null +++ b/src/it/it-property-updates-report-issue-684-001/invoker.properties @@ -0,0 +1 @@ +invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:plugin-updates-report \ No newline at end of file diff --git a/src/it/it-property-updates-report-issue-684-001/pom.xml b/src/it/it-property-updates-report-issue-684-001/pom.xml new file mode 100644 index 0000000000..e641ff8f50 --- /dev/null +++ b/src/it/it-property-updates-report-issue-684-001/pom.xml @@ -0,0 +1,88 @@ + + 4.0.0 + localhost + it-dependency-updates-report-001 + 1.0 + pom + plugin-updates-report + http://localhost/ + + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + + + 3.1 + + + + + + + localhost + dummy-maven-plugin + 1.0 + + + localhost + dummy-api + 1.1 + + + + + maven-clean-plugin + 2.2 + + + maven-deploy-plugin + 2.3 + + + maven-install-plugin + 2.2 + + + maven-site-plugin + @sitePluginVersion@ + + + maven-project-info-reports-plugin + 2.1 + + + + + + + + + maven-project-info-reports-plugin + 2.1 + + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + + plugin-updates-report + + + + + + + + \ No newline at end of file diff --git a/src/it/it-property-updates-report-issue-684-001/verify.groovy b/src/it/it-property-updates-report-issue-684-001/verify.groovy new file mode 100644 index 0000000000..e1571cc591 --- /dev/null +++ b/src/it/it-property-updates-report-issue-684-001/verify.groovy @@ -0,0 +1,6 @@ +output = new File( basedir, 'target/site/plugin-updates-report.html' ).text + .replaceAll( '<[^>]+>', ' ' ) + .replaceAll( '&[^;]+;', ' ' ) + .replaceAll( '\\s+', ' ' ) +assert ! ( output =~ /\blocalhost dummy-maven-plugin 1.0 3.1\b/ ) +assert output =~ /\blocalhost dummy-maven-plugin 1.0 3.0\b/ \ No newline at end of file diff --git a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsDisplayMojo.java b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsDisplayMojo.java index c7db3a478b..bab4092b22 100644 --- a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsDisplayMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsDisplayMojo.java @@ -47,7 +47,7 @@ public abstract class AbstractVersionsDisplayMojo * @since 2.2 */ @Parameter( property = "versions.outputFile" ) - private File outputFile; + protected File outputFile; /** * Controls whether the display output is logged to the console. @@ -55,7 +55,7 @@ public abstract class AbstractVersionsDisplayMojo * @since 2.2 */ @Parameter( property = "versions.logOutput", defaultValue = "true" ) - private boolean logOutput; + protected boolean logOutput; /** * The character encoding to use when writing to {@link #outputFile}. @@ -63,7 +63,7 @@ public abstract class AbstractVersionsDisplayMojo * @since 2.2 */ @Parameter( property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}" ) - private String outputEncoding; + protected String outputEncoding; /** * Line width which should be used to format the padding of the version info list output. @@ -72,7 +72,7 @@ public abstract class AbstractVersionsDisplayMojo */ @Parameter( property = "versions.outputLineWidth", defaultValue = AbstractVersionsDisplayMojo.DEFAULT_OUTPUT_LINE_WIDTH + "" ) - private int outputLineWidth; + protected int outputLineWidth; private boolean outputFileError = false; diff --git a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsReport.java b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsReport.java index 61fb7b63b8..0714321218 100644 --- a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsReport.java +++ b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsReport.java @@ -22,6 +22,7 @@ import java.io.File; import java.util.List; import java.util.Locale; +import java.util.Set; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.manager.WagonManager; @@ -46,6 +47,7 @@ import org.codehaus.mojo.versions.api.ArtifactVersions; import org.codehaus.mojo.versions.api.DefaultVersionsHelper; import org.codehaus.mojo.versions.api.VersionsHelper; +import org.codehaus.mojo.versions.model.RuleSet; import org.codehaus.plexus.i18n.I18N; /** @@ -169,6 +171,30 @@ public abstract class AbstractVersionsReport @Component protected ArtifactResolver artifactResolver; + /** + *

Allows specifying the {@linkplain RuleSet} object describing rules + * on artifact versions to ignore when considering updates.

+ * + * @since 2.13.0 + */ + @Parameter + protected RuleSet ruleSet; + + /** + *

Allows specifying ignored versions directly as an alternative + * to providing the {@linkplain #ruleSet} parameter; mainly created + * for {@code -D} property usage.

+ * + *

+ * Example: {@code "1\.0\.1,.+-M.,.*-SNAPSHOT"} + *

+ * + *

Currently, this parameter will override the defined {@link #ruleSet}

+ * @since 2.13.0 + */ + @Parameter( property = "maven.version.ignore" ) + protected Set ignoredVersions; + // --------------------- GETTER / SETTER METHODS --------------------- public VersionsHelper getHelper() @@ -178,10 +204,23 @@ public VersionsHelper getHelper() { try { - helper = new DefaultVersionsHelper( repositorySystem, artifactResolver, artifactMetadataSource, - remoteArtifactRepositories, remotePluginRepositories, - localRepository, wagonManager, settings, serverId, rulesUri, - getLog(), session, mojoExecution ); + helper = new DefaultVersionsHelper.Builder() + .withRepositorySystem( repositorySystem ) + .withArtifactResolver( artifactResolver ) + .withArtifactMetadataSource( artifactMetadataSource ) + .withRemoteArtifactRepositories( remoteArtifactRepositories ) + .withRemotePluginRepositories( remotePluginRepositories ) + .withLocalRepository( localRepository ) + .withWagonManager( wagonManager ) + .withSettings( settings ) + .withServerId( serverId ) + .withRulesUri( rulesUri ) + .withRuleSet( ruleSet ) + .withIgnoredVersions( ignoredVersions ) + .withLog( getLog() ) + .withMavenSession( session ) + .withMojoExecution( mojoExecution ) + .build(); } catch ( MojoExecutionException e ) { diff --git a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java index 836579f548..b5c76e3fa7 100644 --- a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java @@ -27,6 +27,7 @@ import java.io.IOException; import java.io.Writer; import java.util.List; +import java.util.Set; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.manager.WagonManager; @@ -55,6 +56,7 @@ import org.codehaus.mojo.versions.api.PomHelper; import org.codehaus.mojo.versions.api.PropertyVersions; import org.codehaus.mojo.versions.api.VersionsHelper; +import org.codehaus.mojo.versions.model.RuleSet; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.recording.ChangeRecorder; import org.codehaus.mojo.versions.recording.ChangeRecorderNull; @@ -216,16 +218,53 @@ public abstract class AbstractVersionsUpdaterMojo */ private ChangeRecorder changeRecorder; + /** + *

Allows specifying the {@linkplain RuleSet} object describing rules + * on artifact versions to ignore when considering updates.

+ * + * @since 2.13.0 + */ + @Parameter + protected RuleSet ruleSet; + + /** + *

Allows specifying ignored versions directly as an alternative + * to providing the {@linkplain #ruleSet} parameter; mainly created + * for {@code -D} property usage.

+ * + *

+ * Example: {@code "1\.0\.1,.+-M.,.*-SNAPSHOT"} + *

+ * + *

Currently, this parameter will override the defined {@link #ruleSet}

+ * @since 2.13.0 + */ + @Parameter( property = "maven.version.ignore" ) + protected Set ignoredVersions; + // --------------------- GETTER / SETTER METHODS --------------------- public VersionsHelper getHelper() throws MojoExecutionException { if ( helper == null ) { - helper = new DefaultVersionsHelper( repositorySystem, artifactResolver, artifactMetadataSource, - remoteArtifactRepositories, remotePluginRepositories, localRepository, - wagonManager, settings, serverId, rulesUri, getLog(), - session, mojoExecution ); + helper = new DefaultVersionsHelper.Builder() + .withRepositorySystem( repositorySystem ) + .withArtifactResolver( artifactResolver ) + .withArtifactMetadataSource( artifactMetadataSource ) + .withRemoteArtifactRepositories( remoteArtifactRepositories ) + .withRemotePluginRepositories( remotePluginRepositories ) + .withLocalRepository( localRepository ) + .withWagonManager( wagonManager ) + .withSettings( settings ) + .withServerId( serverId ) + .withRulesUri( rulesUri ) + .withRuleSet( ruleSet ) + .withIgnoredVersions( ignoredVersions ) + .withLog( getLog() ) + .withMavenSession( session ) + .withMojoExecution( mojoExecution ) + .build(); } return helper; } diff --git a/src/main/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojo.java b/src/main/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojo.java index 67f62ca5ed..e4e931e3ee 100644 --- a/src/main/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojo.java @@ -1813,7 +1813,6 @@ protected void update( ModifiedPomXMLEventReader pom ) // do nothing } - @SuppressWarnings( "unchecked" ) private static int compare( ArtifactVersion a, ArtifactVersion b ) { return a.compareTo( b ); diff --git a/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java b/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java index 9aa9fbf1df..f188ccceef 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java +++ b/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java @@ -378,19 +378,16 @@ public final ArtifactVersion[] getVersions( VersionRange versionRange, ArtifactV } public final ArtifactVersion getOldestUpdate( ArtifactVersion currentVersion, UpdateScope updateScope ) - throws InvalidSegmentException { return getOldestUpdate( currentVersion, updateScope, isIncludeSnapshots() ); } public final ArtifactVersion getNewestUpdate( ArtifactVersion currentVersion, UpdateScope updateScope ) - throws InvalidSegmentException { return getNewestUpdate( currentVersion, updateScope, isIncludeSnapshots() ); } public final ArtifactVersion[] getAllUpdates( ArtifactVersion currentVersion, UpdateScope updateScope ) - throws InvalidSegmentException { return getAllUpdates( currentVersion, updateScope, isIncludeSnapshots() ); } diff --git a/src/main/java/org/codehaus/mojo/versions/api/DefaultArtifactAssociation.java b/src/main/java/org/codehaus/mojo/versions/api/DefaultArtifactAssociation.java index 3b85131ff7..f612462f07 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/DefaultArtifactAssociation.java +++ b/src/main/java/org/codehaus/mojo/versions/api/DefaultArtifactAssociation.java @@ -114,12 +114,7 @@ public boolean equals( Object o ) { return false; } - if ( !getGroupId().equals( that.getGroupId() ) ) - { - return false; - } - - return true; + return getGroupId().equals( that.getGroupId() ); } public int hashCode() 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 cb679d3621..fcc704a4cd 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/DefaultVersionsHelper.java +++ b/src/main/java/org/codehaus/mojo/versions/api/DefaultVersionsHelper.java @@ -44,6 +44,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.regex.Pattern; +import java.util.stream.Collectors; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.ArtifactUtils; @@ -113,60 +114,60 @@ public class DefaultVersionsHelper * @since 1.0-alpha-3 * @deprecated */ - private final RuleSet ruleSet; + private RuleSet ruleSet; /** * The artifact metadata source to use. * * @since 1.0-alpha-3 */ - private final ArtifactMetadataSource artifactMetadataSource; + private ArtifactMetadataSource artifactMetadataSource; /** * The local repository to consult. * * @since 1.0-alpha-3 */ - private final ArtifactRepository localRepository; + private ArtifactRepository localRepository; /** * The remote artifact repositories to consult. * * @since 1.0-alpha-3 */ - private final List remoteArtifactRepositories; + private List remoteArtifactRepositories; /** * The remote plugin repositories to consult. * * @since 1.0-alpha-3 */ - private final List remotePluginRepositories; + private List remotePluginRepositories; - private final RepositorySystem repositorySystem; + private RepositorySystem repositorySystem; /** * The {@link Log} to send log messages to. * * @since 1.0-alpha-3 */ - private final Log log; + private Log log; /** * The maven session. * * @since 1.0-beta-1 */ - private final MavenSession mavenSession; + private MavenSession mavenSession; /** * The artifact resolver. * * @since 1.3 */ - private final ArtifactResolver artifactResolver; + private ArtifactResolver artifactResolver; - private final MojoExecution mojoExecution; + private MojoExecution mojoExecution; /** * A cache mapping artifacts to their best fitting rule, since looking up @@ -192,9 +193,11 @@ public class DefaultVersionsHelper * @param log The {@link org.apache.maven.plugin.logging.Log} to send log messages to. * @param mavenSession The maven session information. * @throws MojoExecutionException if something goes wrong. + * @deprecated please use {@linkplain Builder} instead to create instances * @since 1.0-alpha-3 */ @SuppressWarnings( "checkstyle:ParameterNumber" ) + @Deprecated public DefaultVersionsHelper( RepositorySystem repositorySystem, ArtifactResolver artifactResolver, ArtifactMetadataSource artifactMetadataSource, List remoteArtifactRepositories, @@ -215,6 +218,13 @@ public DefaultVersionsHelper( RepositorySystem repositorySystem, ArtifactResolve this.log = log; } + /** + * Private constructor used by the builder + */ + private DefaultVersionsHelper() + { + } + @Deprecated private static RuleSet getRuleSet( Wagon wagon, String remoteURI ) throws IOException, AuthorizationException, TransferFailedException, ResourceDoesNotExistException @@ -292,6 +302,39 @@ private static RuleSet loadRuleSet( String serverId, Settings settings, WagonMan return ruleSet; } + @SuppressWarnings( "checkstyle:AvoidNestedBlocks" ) + private static RuleSet loadRuleSet( Collection ignoredVersions, RuleSet originalRuleSet ) + { + RuleSet ruleSet = new RuleSet(); + if ( originalRuleSet != null ) + { + if ( originalRuleSet.getRules() != null ) + { + ruleSet.setRules( new ArrayList<>( originalRuleSet.getRules() ) ); + } + if ( originalRuleSet.getIgnoreVersions() != null ) + { + ruleSet.setIgnoreVersions( new ArrayList<>( originalRuleSet.getIgnoreVersions() ) ); + } + } + + if ( ignoredVersions != null ) + { + if ( ruleSet.getIgnoreVersions() == null ) + { + ruleSet.setIgnoreVersions( new ArrayList<>() ); + } + ruleSet.getIgnoreVersions().addAll( ignoredVersions.stream().map( v -> + { + IgnoreVersion ignoreVersion = new IgnoreVersion(); + ignoreVersion.setType( TYPE_REGEX ); + ignoreVersion.setVersion( v ); + return ignoreVersion; + } ).collect( Collectors.toList() ) ); + } + return ruleSet; + } + private static RuleSet getRulesFromClasspath( String uri, Log logger ) throws MojoExecutionException { @@ -1039,4 +1082,162 @@ public PluginPluginUpdatesDetails call() } } + /** + * Builder class for {@linkplain DefaultVersionsHelper} + */ + public static class Builder + { + private RepositorySystem repositorySystem; + private ArtifactResolver artifactResolver; + private ArtifactMetadataSource artifactMetadataSource; + private List remoteArtifactRepositories; + private List remotePluginRepositories; + private ArtifactRepository localRepository; + private Collection ignoredVersions; + private RuleSet ruleSet; + private WagonManager wagonManager; + private Settings settings; + private String serverId; + private String rulesUri; + private Log log; + private MavenSession mavenSession; + private MojoExecution mojoExecution; + + public Builder() + { + } + + public Builder withRepositorySystem( RepositorySystem repositorySystem ) + { + this.repositorySystem = repositorySystem; + return this; + } + + public Builder withArtifactResolver( ArtifactResolver artifactResolver ) + { + this.artifactResolver = artifactResolver; + return this; + } + + public Builder withArtifactMetadataSource( ArtifactMetadataSource artifactMetadataSource ) + { + this.artifactMetadataSource = artifactMetadataSource; + return this; + } + + public Builder withRemoteArtifactRepositories( + List remoteArtifactRepositories ) + { + this.remoteArtifactRepositories = remoteArtifactRepositories; + return this; + } + + public Builder withRemotePluginRepositories( + List remotePluginRepositories ) + { + this.remotePluginRepositories = remotePluginRepositories; + return this; + } + + public Builder withLocalRepository( ArtifactRepository localRepository ) + { + this.localRepository = localRepository; + return this; + } + + public Builder withIgnoredVersions( Collection ignoredVersions ) + { + this.ignoredVersions = ignoredVersions; + return this; + } + + public Builder withRuleSet( RuleSet ruleSet ) + { + this.ruleSet = ruleSet; + return this; + } + + public Builder withWagonManager( WagonManager wagonManager ) + { + this.wagonManager = wagonManager; + return this; + } + + public Builder withSettings( Settings settings ) + { + this.settings = settings; + return this; + } + + public Builder withServerId( String serverId ) + { + this.serverId = serverId; + return this; + } + + public Builder withRulesUri( String rulesUri ) + { + this.rulesUri = rulesUri; + return this; + } + + public Builder withLog( Log log ) + { + this.log = log; + return this; + } + + public Builder withMavenSession( MavenSession mavenSession ) + { + this.mavenSession = mavenSession; + return this; + } + + public Builder withMojoExecution( MojoExecution mojoExecution ) + { + this.mojoExecution = mojoExecution; + return this; + } + + /** + * Builds the constructed {@linkplain DefaultVersionsHelper} object + * @return constructed {@linkplain DefaultVersionsHelper} + * @throws MojoExecutionException should the constructor with the Wagon go wrong + */ + public DefaultVersionsHelper build() throws MojoExecutionException + { + if ( ( ignoredVersions == null || ignoredVersions.isEmpty() ) && ruleSet == null ) + { + return new DefaultVersionsHelper( repositorySystem, artifactResolver, artifactMetadataSource, + remoteArtifactRepositories, remotePluginRepositories, localRepository, wagonManager, settings, + serverId, rulesUri, log, mavenSession, mojoExecution ); + } + + if ( rulesUri != null ) + { + log.warn( "rulesUri is ignored if rules are specified in pom or as parameters" ); + } + + DefaultVersionsHelper instance = new DefaultVersionsHelper(); + instance.repositorySystem = repositorySystem; + instance.artifactResolver = artifactResolver; + instance.mavenSession = mavenSession; + instance.mojoExecution = mojoExecution; + instance.ruleSet = ruleSet; + if ( ignoredVersions != null && !ignoredVersions.isEmpty() ) + { + if ( ruleSet != null ) + { + log.info( "Enriching the existing ruleSet with ignoredVersions" ); + } + instance.ruleSet = loadRuleSet( ignoredVersions, ruleSet ); + } + instance.artifactMetadataSource = artifactMetadataSource; + instance.localRepository = localRepository; + instance.remoteArtifactRepositories = remoteArtifactRepositories; + instance.remotePluginRepositories = remotePluginRepositories; + instance.log = log; + return instance; + } + } } diff --git a/src/main/java/org/codehaus/mojo/versions/change/VersionChange.java b/src/main/java/org/codehaus/mojo/versions/change/VersionChange.java index 1c63d956ba..753f3c81a6 100644 --- a/src/main/java/org/codehaus/mojo/versions/change/VersionChange.java +++ b/src/main/java/org/codehaus/mojo/versions/change/VersionChange.java @@ -90,12 +90,7 @@ public boolean equals( Object o ) { return false; } - if ( !Objects.equals( oldVersion, versionChange.oldVersion ) ) - { - return false; - } - - return true; + return Objects.equals( oldVersion, versionChange.oldVersion ); } public int hashCode() diff --git a/src/main/java/org/codehaus/mojo/versions/ordering/VersionComparators.java b/src/main/java/org/codehaus/mojo/versions/ordering/VersionComparators.java index 0576b0104f..2693bb6a51 100644 --- a/src/main/java/org/codehaus/mojo/versions/ordering/VersionComparators.java +++ b/src/main/java/org/codehaus/mojo/versions/ordering/VersionComparators.java @@ -166,8 +166,7 @@ static ArtifactVersion copySnapshot( ArtifactVersion source, ArtifactVersion des { destination = stripSnapshot( destination ); } - Pattern matchSnapshotRegex = SNAPSHOT_PATTERN; - final Matcher matcher = matchSnapshotRegex.matcher( source.toString() ); + final Matcher matcher = SNAPSHOT_PATTERN.matcher( source.toString() ); if ( matcher.find() ) { return new DefaultArtifactVersion( destination.toString() + "-" + matcher.group( 0 ) ); diff --git a/src/main/java/org/codehaus/mojo/versions/recording/ChangeRecorderNull.java b/src/main/java/org/codehaus/mojo/versions/recording/ChangeRecorderNull.java index e2bf312b54..b3c3c2c67b 100644 --- a/src/main/java/org/codehaus/mojo/versions/recording/ChangeRecorderNull.java +++ b/src/main/java/org/codehaus/mojo/versions/recording/ChangeRecorderNull.java @@ -19,7 +19,6 @@ * under the License. */ -import java.io.IOException; import java.io.OutputStream; import java.util.Objects; @@ -57,7 +56,7 @@ public final void recordUpdate( final String kind, final String groupId, final S } @Override - public final void serialize( final OutputStream outputStream ) throws IOException + public final void serialize( final OutputStream outputStream ) { } diff --git a/src/site/apt/version-rules.apt.vm b/src/site/apt/version-rules.apt.vm index 4c18d16894..b304d5f284 100644 --- a/src/site/apt/version-rules.apt.vm +++ b/src/site/apt/version-rules.apt.vm @@ -60,7 +60,13 @@ Version number rules * Rules.xml - To specify the version schemes to use, you must define a {{{./rule.html}rule-set xml file}}, for example: + To specify the version schemes to use, you may define a {{{./rule.html}rule-set xml file}}, use the <<>> + element in the <<>> plugin configuration, or specify ignored versions via + the <<>> property. + +** Using the <<>> file + + The <<>> file should conform to the xsd specified in the example below. --- >> attribute in the <<>> elements has a lazy <<<.*>>> at the end, such that <<>> will match <<>>, <<>>, <<>>, etc. - + +** Using the <<>> element in the POM + + As an alternative to providing a separate <<>> file, starting with version <<<2.13.0>>> it is possible + to provide the <<>> element directly in the POM. The structure is somewhat simpler to + the <<>> file: + +--- + + ... + + ... + + ... + + org.codehaus.mojo + versions-maven-plugin + ${pluginVersion} + + + + + + + 1.0.0 + + + + regex + (.+-SNAPSHOT|.+-M\d) + + + regex + .+-(alpha|beta) + + + + + + + + + org.codehaus.mojo + + + ... + + + maven + + + +--- + +** Using the <<>> property + + The <<>> property can list *comma-separated* list of global version **regex** patterns, which + will be ignored when considering available versions. + + Examples: + + * <<<1.0.0>>> + + * <<<.+-SNAPSHOT>>> + + * <<<(.+-SNAPSHOT|.+-M\d),.+-(alpha|beta),3.0.0>>> + ** Ignoring certain versions It is possible to ignore versions on a global and on a per-rule basis. + The other methods (via the <<<>>> element and via the <<>>) property have been + explained above. The described + --- - { - Artifact artifact = invocation.getArgument( 0 ); - if ( "artifactA".equals( artifact.getArtifactId() ) && "1.0.0".equals( - artifact.getVersion() ) ) - { - return Arrays.asList( new DefaultArtifactVersion( artifact.getVersion() ), - new DefaultArtifactVersion( "2.0.0" ) ); - } - if ( "artifactB".equals( artifact.getArtifactId() ) && "1.0.0".equals( - artifact.getVersion() ) ) - { - return Arrays.asList( new DefaultArtifactVersion( artifact.getVersion() ), - new DefaultArtifactVersion( "1.1.0" ) ); - } - return Collections.singletonList( new DefaultArtifactVersion( artifact.getVersion() ) ); - } ); - } - catch ( ArtifactMetadataRetrievalException e ) - { - throw new RuntimeException( e ); - } + artifactMetadataSource = mockArtifactMetadataSource(); } public TestDependencyUpdatesReport withDependencies( Dependency... dependencies ) @@ -148,6 +122,20 @@ public TestDependencyUpdatesReport withOnlyProjectDependencies( return this; } + public TestDependencyUpdatesReport withRuleSet( + RuleSet ruleSet ) + { + this.ruleSet = ruleSet; + return this; + } + + public TestDependencyUpdatesReport withIgnoredVersions( + Set ignoredVersions ) + { + this.ignoredVersions = ignoredVersions; + return this; + } + /** *

Mocks some Plexus components to speed up test execution.

*

Note: these components could just as well be injected using @@ -262,4 +250,22 @@ public void testOnlyProjectDependencies() throws IOException, MavenReportExcepti assertThat( output, containsString( "artifactA" ) ); assertThat( output, not( anyOf( containsString( "artifactB" ), containsString( "artifactC" ) ) ) ); } + + @Test + public void testOnlyProjectDependenciesWithIgnoredVersions() throws IOException, MavenReportException + { + OutputStream os = new ByteArrayOutputStream(); + SinkFactory sinkFactory = new Xhtml5SinkFactory(); + new TestDependencyUpdatesReport() + .withDependencies( dependencyOf( "artifactA" ) ) + .withDependencyManagement( dependencyOf( "artifactA" ), dependencyOf( "artifactB" ), + dependencyOf( "artifactC" ) ) + .withProcessDependencyManagement( true ) + .withOnlyProjectDependencies( true ) + .withIgnoredVersions( Collections.singleton( "2.0.0" ) ) + .generate( sinkFactory.createSink( os ), sinkFactory, Locale.getDefault() ); + + String output = os.toString().replaceAll( "\n", "" ); + assertThat( output, containsString( "report.noUpdatesAvailable" ) ); + } } diff --git a/src/test/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojoTest.java b/src/test/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojoTest.java index 5e31adcb42..3b113f7f58 100644 --- a/src/test/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojoTest.java +++ b/src/test/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojoTest.java @@ -19,18 +19,41 @@ * under the License. */ +import java.io.File; +import java.nio.file.Files; import java.util.Arrays; +import java.util.HashMap; +import java.util.List; import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.testing.AbstractMojoTestCase; +import org.apache.maven.plugin.testing.MojoRule; +import org.codehaus.mojo.versions.model.TestIgnoreVersions; +import org.hamcrest.Matchers; +import org.junit.Rule; import org.junit.Test; +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.codehaus.mojo.versions.model.TestIgnoreVersions.TYPE_REGEX; +import static org.codehaus.mojo.versions.model.TestIgnoreVersions.matches; +import static org.codehaus.mojo.versions.utils.MockUtils.mockArtifactMetadataSource; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.notNullValue; + /** * Basic tests for {@linkplain DisplayDependencyUpdatesMojo}. * * @author Andrzej Jarmoniuk */ -public class DisplayDependencyUpdatesMojoTest +public class DisplayDependencyUpdatesMojoTest extends AbstractMojoTestCase { + @Rule + public final MojoRule mojoRule = new MojoRule( this ); + @Test public void testValidateGAVListSuccessful() throws MojoExecutionException { @@ -38,10 +61,57 @@ public void testValidateGAVListSuccessful() throws MojoExecutionException "group:artifact:version" ), 3, "" ); } - @Test( expected = MojoExecutionException.class ) - public void testValidateGAVListFailed() throws MojoExecutionException + @Test + public void testValidateGAVListFailed() + { + try + { + DisplayDependencyUpdatesMojo.validateGAVList( Arrays.asList( "group:artifact:version", + "group:artifact:version:type" ), 3, "" ); + fail( "Method should have thrown a MojoExecutionException" ); + } + catch ( MojoExecutionException e ) + { + } + } + + @Test + public void testRuleSetPresentAndWorking() throws Exception { - DisplayDependencyUpdatesMojo.validateGAVList( Arrays.asList( "group:artifact:version", - "group:artifact:version:type" ), 3, "" ); + 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/ruleset" ), + "display-dependency-updates" ); + assertThat( mojo.ruleSet, notNullValue() ); + assertThat( mojo.ruleSet.getIgnoreVersions(), notNullValue() ); + assertThat( mojo.ruleSet.getIgnoreVersions(), Matchers.hasSize( 3 ) ); + assertThat( mojo.ruleSet.getIgnoreVersions(), hasItem( matches( + new TestIgnoreVersions().withVersion( "1.0.1" ) ) ) ); + assertThat( mojo.ruleSet.getIgnoreVersions(), containsInAnyOrder( + matches( new TestIgnoreVersions().withVersion( "1.0.1" ) ), + matches( new TestIgnoreVersions().withType( TYPE_REGEX ).withVersion( ".+-SNAPSHOT" ) ), + matches( new TestIgnoreVersions().withType( TYPE_REGEX ).withVersion( ".+-M\\d+" ) ) ) ); + + // 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() + {{ + put( "dummy-api", new String[] { "1.0.0", "1.0.1", "1.1.0-M1", "1.2.0-SNAPSHOT" } ); + }} ); + + assertThat( mojo.ruleSet.getIgnoreVersions(), Matchers.hasSize( 3 ) ); + mojo.execute(); + List output = Files.readAllLines( outputFile.toPath(), UTF_8 ); + assertThat( output, not( hasItem( containsString( "1.1.0-M1" ) ) ) ); + } + finally + { + assert outputFile == null || !outputFile.exists() || outputFile.delete(); + } } } diff --git a/src/test/java/org/codehaus/mojo/versions/PluginUpdatesReportTest.java b/src/test/java/org/codehaus/mojo/versions/PluginUpdatesReportTest.java index ce0376a64f..055dbc0ec7 100644 --- a/src/test/java/org/codehaus/mojo/versions/PluginUpdatesReportTest.java +++ b/src/test/java/org/codehaus/mojo/versions/PluginUpdatesReportTest.java @@ -25,12 +25,10 @@ import java.util.Arrays; import java.util.Collections; import java.util.Locale; +import java.util.Set; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.DefaultArtifact; -import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; -import org.apache.maven.artifact.metadata.ArtifactMetadataSource; -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.doxia.module.xhtml5.Xhtml5SinkFactory; import org.apache.maven.doxia.sink.SinkFactory; import org.apache.maven.doxia.tools.SiteTool; @@ -41,15 +39,18 @@ import org.apache.maven.project.MavenProject; import org.apache.maven.reporting.MavenReportException; import org.apache.maven.repository.RepositorySystem; +import org.codehaus.mojo.versions.model.RuleSet; import org.codehaus.plexus.i18n.I18N; import org.junit.Test; import static org.apache.maven.artifact.Artifact.SCOPE_RUNTIME; +import static org.codehaus.mojo.versions.utils.MockUtils.mockArtifactMetadataSource; import static org.hamcrest.CoreMatchers.allOf; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.anyOf; +import static org.hamcrest.Matchers.matchesPattern; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; @@ -64,7 +65,6 @@ public class PluginUpdatesReportTest { private static class TestPluginUpdatesReport extends PluginUpdatesReport { - @SuppressWarnings( "deprecation" ) TestPluginUpdatesReport() { mockPlexusComponents(); @@ -73,32 +73,7 @@ private static class TestPluginUpdatesReport extends PluginUpdatesReport project.setBuild( new Build() ); project.getBuild().setPluginManagement( new PluginManagement() ); - artifactMetadataSource = mock( ArtifactMetadataSource.class ); - try - { - when( artifactMetadataSource.retrieveAvailableVersions( any( Artifact.class ), any(), any() ) ).then( - invocation -> - { - Artifact artifact = invocation.getArgument( 0 ); - if ( "artifactA".equals( artifact.getArtifactId() ) && "1.0.0".equals( - artifact.getVersion() ) ) - { - return Arrays.asList( new DefaultArtifactVersion( artifact.getVersion() ), - new DefaultArtifactVersion( "2.0.0" ) ); - } - if ( "artifactB".equals( artifact.getArtifactId() ) && "1.0.0".equals( - artifact.getVersion() ) ) - { - return Arrays.asList( new DefaultArtifactVersion( artifact.getVersion() ), - new DefaultArtifactVersion( "1.1.0" ) ); - } - return Collections.singletonList( new DefaultArtifactVersion( artifact.getVersion() ) ); - } ); - } - catch ( ArtifactMetadataRetrievalException e ) - { - throw new RuntimeException( e ); - } + artifactMetadataSource = mockArtifactMetadataSource(); } public TestPluginUpdatesReport withPlugins( Plugin... plugins ) @@ -125,6 +100,20 @@ public TestPluginUpdatesReport withOnlyProjectPlugins( boolean onlyProjectPlugin return this; } + public TestPluginUpdatesReport withRuleSet( + RuleSet ruleSet ) + { + this.ruleSet = ruleSet; + return this; + } + + public TestPluginUpdatesReport withIgnoredVersions( + Set ignoredVersions ) + { + this.ignoredVersions = ignoredVersions; + return this; + } + /** *

Mocks some Plexus components to speed up test execution.

*

Note: these components could just as well be injected using @@ -224,4 +213,23 @@ public void testOnlyProjectPlugins() throws IOException, MavenReportException assertThat( output, containsString( "artifactA" ) ); assertThat( output, not( anyOf( containsString( "artifactB" ), containsString( "artifactC" ) ) ) ); } + + @Test + public void testOnlyProjectPluginsWithIgnoredVersions() throws IOException, MavenReportException + { + OutputStream os = new ByteArrayOutputStream(); + SinkFactory sinkFactory = new Xhtml5SinkFactory(); + new TestPluginUpdatesReport() + .withPlugins( pluginOf( "artifactA" ) ) + .withPluginManagement( pluginOf( "artifactA" ), pluginOf( "artifactB" ), + pluginOf( "artifactC" ) ) + .withOnlyUpgradable( true ) + .withOnlyProjectPlugins( true ) + .withIgnoredVersions( Collections.singleton( "2.0.0" ) ) + .generate( sinkFactory.createSink( os ), sinkFactory, Locale.getDefault() ); + + String output = os.toString().replaceAll( "\n", "" ) + .replaceAll( "<[^>]+>", " " ).replaceAll( "&[^;]+;", " " ); + assertThat( output, matchesPattern( ".*\\breport.overview.numNewerVersionAvailable\\s+0\\b.*" ) ); + } } diff --git a/src/test/java/org/codehaus/mojo/versions/UpdateParentMojoTest.java b/src/test/java/org/codehaus/mojo/versions/UpdateParentMojoTest.java index f6e95f52da..f73073f341 100644 --- a/src/test/java/org/codehaus/mojo/versions/UpdateParentMojoTest.java +++ b/src/test/java/org/codehaus/mojo/versions/UpdateParentMojoTest.java @@ -2,15 +2,14 @@ import javax.xml.stream.XMLStreamException; -import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.DefaultArtifact; import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; import org.apache.maven.artifact.metadata.ArtifactMetadataSource; import org.apache.maven.artifact.resolver.ArtifactResolver; -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; import org.apache.maven.artifact.versioning.VersionRange; import org.apache.maven.model.Dependency; @@ -27,14 +26,15 @@ import org.junit.Test; import org.mockito.MockedStatic; +import static java.util.Collections.singleton; import static org.apache.maven.artifact.Artifact.SCOPE_COMPILE; import static org.apache.maven.plugin.testing.ArtifactStubFactory.setVariableValueToObject; +import static org.codehaus.mojo.versions.utils.MockUtils.mockArtifactMetadataSource; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mockStatic; @@ -58,7 +58,12 @@ public class UpdateParentMojoTest public static void setUpStatic() throws ArtifactMetadataRetrievalException { repositorySystem = mockRepositorySystem(); - artifactMetadataSource = mockArtifactMetaDataSource(); + artifactMetadataSource = mockArtifactMetadataSource( new HashMap() + {{ + put( "parent-artifact", new String[] { "1.0.1-SNAPSHOT", "1.0.0", "0.9.0" } ); + put( "issue-670-artifact", new String[] { "0.0.1-1", "0.0.1-1-impl-SNAPSHOT" } ); + put( "unknown-artifact", new String[0] ); + }} ); } @Before @@ -112,35 +117,6 @@ private static RepositorySystem mockRepositorySystem() return repositorySystem; } - @SuppressWarnings( "deprecation" ) - private static ArtifactMetadataSource mockArtifactMetaDataSource() throws ArtifactMetadataRetrievalException - { - ArtifactMetadataSource artifactMetadataSource = mock( ArtifactMetadataSource.class ); - when( artifactMetadataSource.retrieveAvailableVersions( any( Artifact.class ), any(), any() ) ).then( - invocation -> - { - Artifact artifact = invocation.getArgument( 0 ); - if ( "parent-artifact".equals( artifact.getArtifactId() ) ) - { - return Arrays.asList( new DefaultArtifactVersion( "1.0.1-SNAPSHOT" ), - new DefaultArtifactVersion( "1.0.0" ), - new DefaultArtifactVersion( "0.9.0" ) ); - } - else if ( "issue-670-artifact".equals( artifact.getArtifactId() ) ) - { - return Arrays.asList( new DefaultArtifactVersion( "0.0.1-1" ), - new DefaultArtifactVersion( "0.0.1-1-impl-SNAPSHOT" ) ); - } - else if ( "unknown-artifact".equals( artifact.getArtifactId() ) ) - { - return Collections.emptyList(); - } - fail(); - return null; - } ); - return artifactMetadataSource; - } - @Test @SuppressWarnings( "deprecation" ) public void testArtifactIdDoesNotExist() @@ -288,4 +264,23 @@ public void testAllowSnapshotsWithParentVersion() "issue-670-artifact", "0.0.1-1", "0.0.1-1-impl-SNAPSHOT" ) ) ); } + + @Test + public void testIgnoredVersions() + throws MojoExecutionException, XMLStreamException, MojoFailureException, IllegalAccessException + { + mojo.getProject().setParent( new MavenProject() + {{ + setGroupId( "default-group" ); + setArtifactId( "parent-artifact" ); + setVersion( "0.9.0" ); + }} ); + setVariableValueToObject( mojo, "ignoredVersions", singleton( "1.0.0" ) ); + try ( MockedStatic pomHelper = mockStatic( PomHelper.class ) ) + { + pomHelper.when( () -> PomHelper.setProjectParentVersion( any(), any() ) ).thenReturn( true ); + mojo.update( null ); + } + assertThat( changeRecorder.getChanges(), is( empty() ) ); + } } diff --git a/src/test/java/org/codehaus/mojo/versions/UseLatestVersionsMojoTest.java b/src/test/java/org/codehaus/mojo/versions/UseLatestVersionsMojoTest.java index fbd94b2c0e..c57869b2ba 100644 --- a/src/test/java/org/codehaus/mojo/versions/UseLatestVersionsMojoTest.java +++ b/src/test/java/org/codehaus/mojo/versions/UseLatestVersionsMojoTest.java @@ -4,11 +4,10 @@ import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; -import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.DefaultArtifact; import org.apache.maven.artifact.metadata.ArtifactMetadataSource; -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.model.Dependency; import org.apache.maven.model.DependencyManagement; import org.apache.maven.model.Model; @@ -25,6 +24,7 @@ import org.mockito.MockedStatic; import static org.apache.maven.artifact.Artifact.SCOPE_COMPILE; +import static org.codehaus.mojo.versions.utils.MockUtils.mockArtifactMetadataSource; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.hasItem; import static org.mockito.ArgumentMatchers.any; @@ -51,30 +51,13 @@ public void setUp() throws Exception dependency.getClassifier() != null ? dependency.getClassifier() : "default", null ); } ); - ArtifactMetadataSource artifactMetadataSourceMock = mock( ArtifactMetadataSource.class ); - when( artifactMetadataSourceMock.retrieveAvailableVersions( any( Artifact.class ), any(), any() ) ).then( - invocation -> - { - Artifact artifact = invocation.getArgument( 0 ); - if ( "dependency-artifact".equals( artifact.getArtifactId() ) ) - { - return Arrays.asList( new DefaultArtifactVersion( "1.1.1-SNAPSHOT" ), - new DefaultArtifactVersion( "1.1.0" ), new DefaultArtifactVersion( "1.1.0-SNAPSHOT" ), - new DefaultArtifactVersion( "1.0.0" ), new DefaultArtifactVersion( "1.0.0-SNAPSHOT" ), - new DefaultArtifactVersion( "0.9.0" ) ); - } - else if ( "poison-artifact".equals( artifact.getArtifactId() ) ) - { - return Arrays.asList( new DefaultArtifactVersion( "1.1.1.1-SNAPSHOT" ), - new DefaultArtifactVersion( "1.1.1.0" ), - new DefaultArtifactVersion( "1.1.1.0-SNAPSHOT" ), - new DefaultArtifactVersion( "1.0.0.0" ), - new DefaultArtifactVersion( "1.0.0.0-SNAPSHOT" ), - new DefaultArtifactVersion( "0.9.0.0" ) ); - } - fail(); - return null; - } ); + ArtifactMetadataSource artifactMetadataSourceMock = mockArtifactMetadataSource( new HashMap() + {{ + put( "dependency-artifact", new String[] {"1.1.1-SNAPSHOT", "1.1.0", "1.1.0-SNAPSHOT", "1.0.0", + "1.0.0-SNAPSHOT", "0.9.0"} ); + put( "poison-artifact", new String[] {"1.1.1.1-SNAPSHOT", "1.1.1.0", "1.1.1.0-SNAPSHOT", "1.0.0.0", + "1.0.0.0-SNAPSHOT", "0.9.0.0"} ); + }} ); mojo = new UseLatestVersionsMojo() {{ diff --git a/src/test/java/org/codehaus/mojo/versions/api/DefaultVersionsHelperTest.java b/src/test/java/org/codehaus/mojo/versions/api/DefaultVersionsHelperTest.java index 0bf4622945..eea8c453d6 100644 --- a/src/test/java/org/codehaus/mojo/versions/api/DefaultVersionsHelperTest.java +++ b/src/test/java/org/codehaus/mojo/versions/api/DefaultVersionsHelperTest.java @@ -20,8 +20,11 @@ */ import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.manager.WagonManager; @@ -34,19 +37,26 @@ import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.MojoExecution; +import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.logging.Log; +import org.apache.maven.plugin.logging.SystemStreamLog; import org.apache.maven.plugin.testing.AbstractMojoTestCase; import org.apache.maven.project.MavenProject; import org.apache.maven.project.artifact.MavenMetadataSource; import org.apache.maven.repository.RepositorySystem; import org.apache.maven.settings.Settings; import org.codehaus.mojo.versions.Property; +import org.codehaus.mojo.versions.model.IgnoreVersion; +import org.codehaus.mojo.versions.model.Rule; +import org.codehaus.mojo.versions.model.RuleSet; import org.codehaus.mojo.versions.ordering.VersionComparators; import org.hamcrest.CoreMatchers; import org.junit.Test; import static java.util.Arrays.asList; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.core.IsIterableContaining.hasItems; import static org.mockito.Mockito.any; import static org.mockito.Mockito.anyList; @@ -223,13 +233,57 @@ private DefaultVersionsHelper createHelper( String rulesUri, ArtifactMetadataSou // }; DefaultVersionsHelper helper = - new DefaultVersionsHelper( lookup( RepositorySystem.class ), new DefaultArtifactResolver(), metadataSource, - new ArrayList(), - new ArrayList(), - new DefaultArtifactRepository( "", "", new DefaultRepositoryLayout() ), - wagonManager, new Settings(), "", rulesUri, mock( Log.class ), - mock( MavenSession.class ), mock( MojoExecution.class ) ); + new DefaultVersionsHelper( lookup( RepositorySystem.class ), new DefaultArtifactResolver(), + metadataSource, + new ArrayList(), + new ArrayList(), + new DefaultArtifactRepository( "", "", new DefaultRepositoryLayout() ), + wagonManager, new Settings(), "", rulesUri, mock( Log.class ), + mock( MavenSession.class ), mock( MojoExecution.class ) ); return helper; } + @Test + public void testIgnoredVersionsShouldBeTheOnlyPresentInAnEmptyRuleSet() + throws MojoExecutionException, IllegalAccessException + { + DefaultVersionsHelper versionsHelper = new DefaultVersionsHelper.Builder() + .withLog( new SystemStreamLog() ) + .withIgnoredVersions( Arrays.asList( ".*-M.", ".*-SNAPSHOT" ) ) + .build(); + RuleSet ruleSet = (RuleSet) getVariableValueFromObject( versionsHelper, "ruleSet" ); + assertThat( ruleSet.getIgnoreVersions(), hasSize( 2 ) ); + assertThat( ruleSet.getIgnoreVersions().stream().map( IgnoreVersion::getVersion ) + .collect( Collectors.toList() ), containsInAnyOrder( ".*-M.", ".*-SNAPSHOT" ) ); + } + + @Test + public void testIgnoredVersionsShouldExtendTheRuleSet() throws MojoExecutionException, IllegalAccessException + { + DefaultVersionsHelper versionsHelper = new DefaultVersionsHelper.Builder() + .withLog( new SystemStreamLog() ) + .withRuleSet( new RuleSet() + {{ + setIgnoreVersions( new ArrayList<>( Collections.singletonList( new IgnoreVersion() + {{ + setVersion( "1.0.0" ); + }} ) ) ); + setRules( Collections.singletonList( new Rule() + {{ + setGroupId( "org.slf4j" ); + setArtifactId( "slf4j-api" ); + setIgnoreVersions( Collections.singletonList( new IgnoreVersion() + {{ + setType( "regex" ); + setVersion( "^[^1]\\.*" ); + }} ) ); + }} ) ); + }} ) + .withIgnoredVersions( Arrays.asList( ".*-M.", ".*-SNAPSHOT" ) ) + .build(); + RuleSet ruleSet = (RuleSet) getVariableValueFromObject( versionsHelper, "ruleSet" ); + assertThat( ruleSet.getIgnoreVersions(), hasSize( 3 ) ); + assertThat( ruleSet.getIgnoreVersions().stream().map( IgnoreVersion::getVersion ) + .collect( Collectors.toList() ), containsInAnyOrder( ".*-M.", ".*-SNAPSHOT", "1.0.0" ) ); + } } diff --git a/src/test/java/org/codehaus/mojo/versions/model/TestIgnoreVersions.java b/src/test/java/org/codehaus/mojo/versions/model/TestIgnoreVersions.java new file mode 100644 index 0000000000..e70da476ea --- /dev/null +++ b/src/test/java/org/codehaus/mojo/versions/model/TestIgnoreVersions.java @@ -0,0 +1,93 @@ +/* + * 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.model; + +import java.util.Objects; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeMatcher; + +/** + * Convenience class for quickly creating {@link IgnoreVersion} instances. + */ +public class TestIgnoreVersions extends IgnoreVersion +{ + public static final String TYPE_REGEX = "regex"; + + public static final String TYPE_EXACT = "exact"; + + /** + * Sets the given type returning the modified instance. + * + * @param type required type + * @return modified instance + */ + public TestIgnoreVersions withType( String type ) + { + setType( type ); + return this; + } + + /** + * Sets the given version returning the modified instance. + * + * @param version required version + * @return modified instance + */ + public TestIgnoreVersions withVersion( String version ) + { + setVersion( version ); + return this; + } + + /** + * Produces a {@linkplain Matcher} instance to match against {@linkplain IgnoreVersion} instances, + * which don't have {@linkplain #equals(Object)} and {@linkplain #hashCode()} + * @param ignoreVersion this ignoreVersion object + * @return Matcher returning true if another object matches the given instance + * @param

class of the ignoreVersion instance + */ + public static

Matcher

matches( P ignoreVersion ) + { + return new TypeSafeMatcher

() + { + @Override + public void describeTo( Description description ) + { + description.appendText( Objects.toString( ignoreVersion ) ); + } + + @Override + protected void describeMismatchSafely( P other, Description description ) + { + description.appendText( Objects.toString( other ) ); + } + + @Override + protected boolean matchesSafely( P other ) + { + return Objects.equals( ignoreVersion.getType(), other.getType() ) + && Objects.equals( ignoreVersion.getVersion(), other.getVersion() ); + } + }; + } + +} diff --git a/src/test/java/org/codehaus/mojo/versions/utils/MockUtils.java b/src/test/java/org/codehaus/mojo/versions/utils/MockUtils.java new file mode 100644 index 0000000000..0d9ea6234e --- /dev/null +++ b/src/test/java/org/codehaus/mojo/versions/utils/MockUtils.java @@ -0,0 +1,86 @@ +/* + * 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.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; +import org.apache.maven.artifact.metadata.ArtifactMetadataSource; +import org.apache.maven.artifact.versioning.DefaultArtifactVersion; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Various mock creating utilities + */ +public class MockUtils +{ + private static final Map DEFAULT_VERSION_MAP = new HashMap() + {{ + put( "artifactA", new String[] {"1.0.0", "2.0.0"} ); + put( "artifactB", new String[] {"1.0.0", "1.1.0"} ); + put( "artifactC", new String[] {"1.0.0"} ); + }}; + + /** + * Creates a mocked {@linkplain ArtifactMetadataSource}, providing the default version set + * @return mocked {@linkplain ArtifactMetadataSource} + */ + public static ArtifactMetadataSource mockArtifactMetadataSource() + { + return mockArtifactMetadataSource( DEFAULT_VERSION_MAP ); + } + + /** + * Creates a mocked {@linkplain ArtifactMetadataSource}, providing the version map given in the argument + * @param versionMap requested version map + * @return mocked {@linkplain ArtifactMetadataSource} + */ + public static ArtifactMetadataSource mockArtifactMetadataSource( Map versionMap ) + { + ArtifactMetadataSource artifactMetadataSource = mock( ArtifactMetadataSource.class ); + try + { + when( artifactMetadataSource.retrieveAvailableVersions( any( Artifact.class ), any(), any() ) ).then( + invocation -> + { + Artifact artifact = invocation.getArgument( 0 ); + return versionMap.entrySet().stream() + .filter( e -> e.getKey().equals( artifact.getArtifactId() ) ) + .findAny() + .map( e -> Arrays.stream( e.getValue() ) + .map( DefaultArtifactVersion::new ) + .collect( ArrayList::new, ArrayList::add, ArrayList::add ) ) + .orElse( null ); // should tell us if we haven't populated all cases in the test + } ); + } + catch ( ArtifactMetadataRetrievalException e ) + { + throw new RuntimeException( e ); + } + return artifactMetadataSource; + } +} diff --git a/src/test/java/org/codehaus/mojo/versions/utils/ModifiedPomXMLEventReaderUtils.java b/src/test/java/org/codehaus/mojo/versions/utils/ModifiedPomXMLEventReaderUtils.java new file mode 100644 index 0000000000..fb8a37a3b3 --- /dev/null +++ b/src/test/java/org/codehaus/mojo/versions/utils/ModifiedPomXMLEventReaderUtils.java @@ -0,0 +1,62 @@ +package org.codehaus.mojo.versions.utils; + +/* + * 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. + */ + +import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeMatcher; + +/** + *

Utilities for the {@link ModifiedPomXMLEventReader} class

+ * + * @author Andrzej Jarmoniuk + */ +public class ModifiedPomXMLEventReaderUtils +{ + public static

Matcher

matches( String pattern ) + { + return new TypeSafeMatcher

() + { + @Override + public void describeTo( Description description ) + { + description.appendText( pattern ); + } + + @Override + protected void describeMismatchSafely( P pom, Description description ) + { + description.appendText( asString( pom ) ); + } + + @Override + protected boolean matchesSafely( P pom ) + { + return pattern.matches( asString( pom ) ); + } + + private String asString( P pom ) + { + return pom.asStringBuilder().toString().replaceAll( "\\s", "" ); + } + }; + } +} diff --git a/src/test/resources/org/codehaus/mojo/display-dependency-updates/ruleset/pom.xml b/src/test/resources/org/codehaus/mojo/display-dependency-updates/ruleset/pom.xml new file mode 100644 index 0000000000..827b86548f --- /dev/null +++ b/src/test/resources/org/codehaus/mojo/display-dependency-updates/ruleset/pom.xml @@ -0,0 +1,57 @@ + + 4.0.0 + default-group + default-artifact + 1.0 + pom + + + + + localhost + dummy-api + 1.0.0 + + + + + + + localhost + dummy-api + + + + + + + org.codehaus.mojo + versions-maven-plugin + ${project.version} + + display-dependency-updates + + + + + + + + regex + .+-SNAPSHOT + + + regex + .+-M\d+ + + + 1.0.1 + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/org/codehaus/mojo/display-plugin-updates/ruleset/pom.xml b/src/test/resources/org/codehaus/mojo/display-plugin-updates/ruleset/pom.xml new file mode 100644 index 0000000000..7a8dfd8dbb --- /dev/null +++ b/src/test/resources/org/codehaus/mojo/display-plugin-updates/ruleset/pom.xml @@ -0,0 +1,45 @@ + + 4.0.0 + default-group + default-artifact + 1.0 + pom + + + + + org.codehaus.mojo + versions-maven-plugin + ${project.version} + + display-plugin-updates + + + + + + + + regex + .+-SNAPSHOT + + + regex + .+-M\d+ + + + 1.0.1 + + + + + + + localhost + dummy-api + 1.0.0 + + + + \ No newline at end of file