From 89ab3fce5cfb7b8a51c1a6a48b308cfca82833ad Mon Sep 17 00:00:00 2001 From: Jeremy Landis Date: Sun, 20 Oct 2024 03:12:08 -0400 Subject: [PATCH 1/2] [maven] Get project from session object --- .../spotbugs/BaseViolationCheckMojo.groovy | 5 --- .../mojo/spotbugs/SourceFileIndexer.groovy | 20 ++++++------ .../codehaus/mojo/spotbugs/SpotBugsGui.groovy | 7 +--- .../mojo/spotbugs/SpotBugsMojo.groovy | 32 +++++-------------- 4 files changed, 18 insertions(+), 46 deletions(-) diff --git a/src/main/groovy/org/codehaus/mojo/spotbugs/BaseViolationCheckMojo.groovy b/src/main/groovy/org/codehaus/mojo/spotbugs/BaseViolationCheckMojo.groovy index 2ecbebd5..7c27277b 100644 --- a/src/main/groovy/org/codehaus/mojo/spotbugs/BaseViolationCheckMojo.groovy +++ b/src/main/groovy/org/codehaus/mojo/spotbugs/BaseViolationCheckMojo.groovy @@ -31,7 +31,6 @@ import org.apache.maven.plugins.annotations.LifecyclePhase import org.apache.maven.plugins.annotations.Mojo import org.apache.maven.plugins.annotations.Parameter import org.apache.maven.plugins.annotations.ResolutionScope -import org.apache.maven.project.MavenProject import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver import org.codehaus.plexus.resource.ResourceManager @@ -121,10 +120,6 @@ abstract class BaseViolationCheckMojo extends AbstractMojo { @Parameter (defaultValue = '${session}', required = true, readonly = true) MavenSession session - /** Maven Project. */ - @Parameter(property = "project", required = true, readonly = true) - MavenProject project - /** Encoding used for xml files. Default value is UTF-8. */ @Parameter(defaultValue = "UTF-8", readonly = true) String xmlEncoding diff --git a/src/main/groovy/org/codehaus/mojo/spotbugs/SourceFileIndexer.groovy b/src/main/groovy/org/codehaus/mojo/spotbugs/SourceFileIndexer.groovy index d5ead41a..d059becc 100644 --- a/src/main/groovy/org/codehaus/mojo/spotbugs/SourceFileIndexer.groovy +++ b/src/main/groovy/org/codehaus/mojo/spotbugs/SourceFileIndexer.groovy @@ -17,7 +17,6 @@ package org.codehaus.mojo.spotbugs import org.apache.maven.execution.MavenSession import org.apache.maven.model.Resource -import org.apache.maven.project.MavenProject import java.nio.file.Paths @@ -36,38 +35,37 @@ class SourceFileIndexer { * @param project Reference to the Maven project to get the list of source directories * @param session Reference to the Maven session used to get the location of the root directory */ - protected void buildListSourceFiles(MavenProject project, MavenSession session) { + protected void buildListSourceFiles(MavenSession session) { - //String basePath = project.basedir.absolutePath String basePath = normalizePath(session.getExecutionRootDirectory()) List allSourceFiles = new ArrayList<>() // Resource - for (Resource r in project.getResources()) { + for (Resource r in session.getCurrentProject().getResources()) { scanDirectory(new File(r.directory), allSourceFiles, basePath) } - for (Resource r in project.getTestResources()) { + for (Resource r in session.getCurrentProject().getTestResources()) { scanDirectory(new File(r.directory), allSourceFiles, basePath) } // Source files - for (String sourceRoot in project.getCompileSourceRoots()) { + for (String sourceRoot in session.getCurrentProject().getCompileSourceRoots()) { scanDirectory(new File(sourceRoot), allSourceFiles, basePath) } - for (String sourceRoot in project.getTestCompileSourceRoots()) { + for (String sourceRoot in session.getCurrentProject().getTestCompileSourceRoots()) { scanDirectory(new File(sourceRoot), allSourceFiles, basePath) } - for (String sourceRoot in project.getScriptSourceRoots()) { + for (String sourceRoot in session.getCurrentProject().getScriptSourceRoots()) { scanDirectory(new File(sourceRoot), allSourceFiles, basePath) } //While not perfect, add the following paths will add basic support for Kotlin and Groovy - scanDirectory(new File(project.getBasedir(),"src/main/webapp"), allSourceFiles, basePath) - scanDirectory(new File(project.getBasedir(),"src/main/groovy"), allSourceFiles, basePath) - scanDirectory(new File(project.getBasedir(),"src/main/kotlin"), allSourceFiles, basePath) + scanDirectory(new File(session.getCurrentProject().getBasedir(),"src/main/webapp"), allSourceFiles, basePath) + scanDirectory(new File(session.getCurrentProject().getBasedir(),"src/main/groovy"), allSourceFiles, basePath) + scanDirectory(new File(session.getCurrentProject().getBasedir(),"src/main/kotlin"), allSourceFiles, basePath) this.allSourceFiles = allSourceFiles } diff --git a/src/main/groovy/org/codehaus/mojo/spotbugs/SpotBugsGui.groovy b/src/main/groovy/org/codehaus/mojo/spotbugs/SpotBugsGui.groovy index e94d758d..d51f3926 100644 --- a/src/main/groovy/org/codehaus/mojo/spotbugs/SpotBugsGui.groovy +++ b/src/main/groovy/org/codehaus/mojo/spotbugs/SpotBugsGui.groovy @@ -24,7 +24,6 @@ import org.apache.maven.plugin.AbstractMojo import org.apache.maven.plugins.annotations.Mojo import org.apache.maven.plugins.annotations.Parameter import org.apache.maven.plugins.annotations.ResolutionScope -import org.apache.maven.project.MavenProject import org.apache.maven.repository.RepositorySystem import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver import org.codehaus.plexus.resource.ResourceManager @@ -85,10 +84,6 @@ class SpotBugsGui extends AbstractMojo implements SpotBugsPluginsTrait { @Parameter (defaultValue = '${session}', required = true, readonly = true) MavenSession session - /** Maven Project. */ - @Parameter(property="project", required = true, readonly = true) - MavenProject project - /** Resource bundle for a specific locale. */ @Parameter(readonly = true) ResourceBundle bundle @@ -136,7 +131,7 @@ class SpotBugsGui extends AbstractMojo implements SpotBugsPluginsTrait { AntBuilder ant = new AntBuilder() - List auxClasspathElements = project.compileClasspathElements + List auxClasspathElements = session.getCurrentProject().compileClasspathElements if (debug) { log.debug(" Plugin Artifacts to be added ->" + pluginArtifacts.toString()) diff --git a/src/main/groovy/org/codehaus/mojo/spotbugs/SpotBugsMojo.groovy b/src/main/groovy/org/codehaus/mojo/spotbugs/SpotBugsMojo.groovy index c7228d57..d5878537 100644 --- a/src/main/groovy/org/codehaus/mojo/spotbugs/SpotBugsMojo.groovy +++ b/src/main/groovy/org/codehaus/mojo/spotbugs/SpotBugsMojo.groovy @@ -29,7 +29,6 @@ import org.apache.maven.plugin.MojoExecutionException import org.apache.maven.plugins.annotations.Mojo import org.apache.maven.plugins.annotations.Parameter import org.apache.maven.plugins.annotations.ResolutionScope -import org.apache.maven.project.MavenProject import org.apache.maven.reporting.AbstractMavenReport import org.apache.maven.reporting.MavenReport import org.apache.maven.repository.RepositorySystem @@ -190,10 +189,6 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait { @Parameter (defaultValue = '${session}', required = true, readonly = true) MavenSession session - /** Maven Project. */ - @Parameter(property = "project", required = true, readonly = true) - MavenProject project - /** Encoding used for xml files. Default value is UTF-8. */ @Parameter(defaultValue = "UTF-8", readonly = true) String xmlEncoding @@ -665,7 +660,7 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait { } else { log.debug("Generating Spotbugs HTML") - SpotbugsReportGenerator generator = new SpotbugsReportGenerator(getSink(), getBundle(locale), this.project.getBasedir(), siteTool) + SpotbugsReportGenerator generator = new SpotbugsReportGenerator(getSink(), getBundle(locale), this.session.getCurrentProject().getBasedir(), siteTool) boolean isJxrPluginEnabled = isJxrPluginEnabled() @@ -753,17 +748,6 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait { return outputDirectory.getAbsolutePath() } - /** - * Return the project. - * - * @return the project. - * @see AbstractMavenReport#getProject() - */ - @Override - protected MavenProject getProject() { - return this.project - } - /** * Return the Site Renderer. * @@ -996,9 +980,9 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait { List auxClasspathElements if (testClassFilesDirectory.isDirectory() && includeTests) { - auxClasspathElements = project.testClasspathElements + auxClasspathElements = session.getCurrentProject().testClasspathElements } else if (classFilesDirectory.isDirectory()) { - auxClasspathElements = project.compileClasspathElements + auxClasspathElements = session.getCurrentProject().compileClasspathElements } File auxClasspathFile = null @@ -1008,7 +992,7 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait { auxClasspathFile.deleteOnExit() log.debug(" AuxClasspath Elements ->" + auxClasspathElements) - List auxClasspathList = auxClasspathElements.findAll { project.build.outputDirectory != it.toString() } + List auxClasspathList = auxClasspathElements.findAll { session.getCurrentProject().getBuild().outputDirectory != it.toString() } if (auxClasspathList.size() > 0) { log.debug(" Last AuxClasspath is ->" + auxClasspathList[auxClasspathList.size() - 1]) @@ -1065,10 +1049,10 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait { log.debug("****** Executing SpotBugsMojo *******") - resourceManager.addSearchPath(FileResourceLoader.ID, project.getFile().getParentFile().getAbsolutePath()) + resourceManager.addSearchPath(FileResourceLoader.ID, session.getCurrentProject().getFile().getParentFile().getAbsolutePath()) resourceManager.addSearchPath(SpotBugsInfo.URL, "") - resourceManager.setOutputDirectory(new File(project.getBuild().getDirectory())) + resourceManager.setOutputDirectory(new File(session.getCurrentProject().getBuild().directory)) if (log.isDebugEnabled()) { log.debug("resourceManager.outputDirectory is ${resourceManager.outputDirectory}") @@ -1183,7 +1167,7 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait { path.SpotbugsResults.FindBugsSummary.'total_bugs' = bugCount xmlProject.appendNode { - WrkDir(project.build.directory) + WrkDir(session.getCurrentProject().getBuild().directory) } StreamingMarkupBuilder xmlBuilder = new StreamingMarkupBuilder() @@ -1229,7 +1213,7 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait { SourceFileIndexer indexer = new SourceFileIndexer() - indexer.buildListSourceFiles(getProject(),getSession()) + indexer.buildListSourceFiles(session) for (result in slurpedResult.runs.results[0]) { From 0c0eeac8069d2f4411fe248962350ad582172fc6 Mon Sep 17 00:00:00 2001 From: Jeremy Landis Date: Sun, 20 Oct 2024 03:34:26 -0400 Subject: [PATCH 2/2] [maven] Cleanup call to getProject and call directly --- src/main/groovy/org/codehaus/mojo/spotbugs/SpotBugsMojo.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/groovy/org/codehaus/mojo/spotbugs/SpotBugsMojo.groovy b/src/main/groovy/org/codehaus/mojo/spotbugs/SpotBugsMojo.groovy index d5878537..cd04f19b 100644 --- a/src/main/groovy/org/codehaus/mojo/spotbugs/SpotBugsMojo.groovy +++ b/src/main/groovy/org/codehaus/mojo/spotbugs/SpotBugsMojo.groovy @@ -770,7 +770,7 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait { return true } - List reportPlugins = getProject().getReportPlugins() + List reportPlugins = session.getCurrentProject().getReportPlugins() boolean isEnabled