-
Notifications
You must be signed in to change notification settings - Fork 5
feat: a script to find obsolete code #486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,246 @@ | ||||||||||||||||||||||||||||||||||||||
| <# | ||||||||||||||||||||||||||||||||||||||
| .SYNOPSIS | ||||||||||||||||||||||||||||||||||||||
| Lists obsoletions, deprecations, and outdated dependencies for a Maven project. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| .DESCRIPTION | ||||||||||||||||||||||||||||||||||||||
| This script runs various Maven plugins to gather information about: | ||||||||||||||||||||||||||||||||||||||
| 1. Code deprecations (calls to @Deprecated methods/classes). | ||||||||||||||||||||||||||||||||||||||
| 2. Outdated dependencies (using versions-maven-plugin). | ||||||||||||||||||||||||||||||||||||||
| 3. Outdated plugins (using versions-maven-plugin). | ||||||||||||||||||||||||||||||||||||||
| 4. Unused declared dependencies (using maven-dependency-plugin). | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| .PARAMETER IncludeTransitive | ||||||||||||||||||||||||||||||||||||||
| If set, includes transitive dependencies in the outdated dependencies report. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| .PARAMETER OnlyDeprecations | ||||||||||||||||||||||||||||||||||||||
| If set, only checks for code deprecations and skips dependency/plugin checks. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| .PARAMETER PomFile | ||||||||||||||||||||||||||||||||||||||
| Path to the pom.xml file. Defaults to "pom.xml" in the script's directory. | ||||||||||||||||||||||||||||||||||||||
| #> | ||||||||||||||||||||||||||||||||||||||
| param( | ||||||||||||||||||||||||||||||||||||||
| [switch]$IncludeTransitive, | ||||||||||||||||||||||||||||||||||||||
| [switch]$OnlyDeprecations, | ||||||||||||||||||||||||||||||||||||||
| [string]$PomFile | ||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| $ErrorActionPreference = "Continue" | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| if ($PomFile) { | ||||||||||||||||||||||||||||||||||||||
| $pomPath = Resolve-Path $PomFile | ||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||
| $pomPath = Join-Path $PSScriptRoot "pom.xml" | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| # Check Java version mismatch | ||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||
| $javaExe = (Get-Command java -ErrorAction Stop).Source | ||||||||||||||||||||||||||||||||||||||
| # Resolve symlinks if possible or just take the path | ||||||||||||||||||||||||||||||||||||||
| $javaItem = Get-Item $javaExe | ||||||||||||||||||||||||||||||||||||||
| if ($javaItem.LinkType -eq "SymbolicLink") { | ||||||||||||||||||||||||||||||||||||||
| $javaExe = $javaItem.Target | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
| $javaExe = $javaItem.Target | |
| $target = $javaItem.Target | |
| if ($target -is [Array]) { | |
| $javaExe = $target[0] | |
| } else { | |
| $javaExe = $target | |
| } |
Copilot
AI
Dec 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable $IsWindows is not available in Windows PowerShell 5.1 and below (only in PowerShell Core 6+). This will cause the script to fail on older Windows systems. Consider adding a check to define $IsWindows if it doesn't exist, such as: if (-not (Test-Path variable:IsWindows)) { $IsWindows = $true }
Copilot
AI
Dec 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The regex pattern assumes transitive dependencies come from "Dependency Management" section, but the versions-maven-plugin output may vary. Some transitive dependencies might appear in different sections or not be clearly marked. Consider documenting this assumption or verifying it handles all cases correctly.
Copilot
AI
Dec 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The regex pattern "^[WARNING]\s+.:.:.*" is too permissive and may capture unrelated warning messages that happen to contain colons. A more specific pattern matching Maven artifact coordinates (groupId:artifactId:type:version:scope) would be more reliable, such as "^[WARNING]\s+[^:]+:[^:]+:[^:]+:[^:]+:[^:]+".
| # Artifact lines in this section usually start with [WARNING] and contain colons like group:artifact:ver | |
| if ($line -match "^\[WARNING\]\s+.*:.*:.*") { | |
| # Artifact lines in this section usually start with [WARNING] and contain Maven coordinates like groupId:artifactId:type:version:scope | |
| if ($line -match "^\[WARNING\]\s+[^:]+:[^:]+:[^:]+:[^:]+:[^:]+") { |
Copilot
AI
Dec 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The capturing logic may stop prematurely when encountering an INFO line. If there are INFO-level messages interspersed with the unused dependencies warnings (which can happen in Maven output), this will miss some unused dependencies. Consider a more robust approach that only stops when a clear section boundary is detected.
| # Stop capturing if we hit another section or INFO log that isn't an artifact | |
| # Artifact lines in this section usually start with [WARNING] and contain colons like group:artifact:ver | |
| if ($line -match "^\[WARNING\]\s+.*:.*:.*") { | |
| $unusedDeps += ($line -replace "^\[WARNING\]\s*", "") | |
| } elseif ($line -match "^\[INFO\]" -or ($line -match "^\[WARNING\]" -and $line -notmatch "dependencies found")) { | |
| # If we hit a new warning header or info, stop | |
| # Stop capturing only when we clearly leave the unused-dependencies section. | |
| # Artifact lines in this section usually start with [WARNING] and contain colons like group:artifact:ver | |
| if ($line -match "^\[WARNING\]\s+.*:.*:.*") { | |
| $unusedDeps += ($line -replace "^\[WARNING\]\s*", "") | |
| } elseif ( | |
| # Non-artifact WARNING (e.g., a new warning header) ends this section | |
| ($line -match "^\[WARNING\]" -and $line -notmatch "dependencies found") -or | |
| # Clear Maven section boundaries / build summary also end this section | |
| $line -match "^\[INFO\]\s+---" -or # new plugin goal section | |
| $line -match "^\[INFO\]\s+BUILD\s+(SUCCESS|FAILURE)" -or | |
| $line -match "^\[INFO\]\s+-{10,}" # long separator line | |
| ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PomFile parameter is not validated for existence before being used. If a user provides a non-existent file path, Resolve-Path will throw an error that will be difficult to understand. Consider adding validation to check if the file exists and provide a clear error message if it doesn't.