Skip to content

Commit

Permalink
Merge pull request spotbugs#899 from hazendaz/maven-api
Browse files Browse the repository at this point in the history
Use project from maven session and fix call to getProject to do same
  • Loading branch information
hazendaz authored Oct 20, 2024
2 parents 535da14 + 0c0eeac commit d6413ac
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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<File> 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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -136,7 +131,7 @@ class SpotBugsGui extends AbstractMojo implements SpotBugsPluginsTrait {

AntBuilder ant = new AntBuilder()

List<String> auxClasspathElements = project.compileClasspathElements
List<String> auxClasspathElements = session.getCurrentProject().compileClasspathElements

if (debug) {
log.debug(" Plugin Artifacts to be added ->" + pluginArtifacts.toString())
Expand Down
34 changes: 9 additions & 25 deletions src/main/groovy/org/codehaus/mojo/spotbugs/SpotBugsMojo.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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.
*
Expand All @@ -786,7 +770,7 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait {
return true
}

List reportPlugins = getProject().getReportPlugins()
List reportPlugins = session.getCurrentProject().getReportPlugins()

boolean isEnabled

Expand Down Expand Up @@ -996,9 +980,9 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait {
List<String> 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
Expand All @@ -1008,7 +992,7 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait {
auxClasspathFile.deleteOnExit()
log.debug(" AuxClasspath Elements ->" + auxClasspathElements)

List<String> auxClasspathList = auxClasspathElements.findAll { project.build.outputDirectory != it.toString() }
List<String> auxClasspathList = auxClasspathElements.findAll { session.getCurrentProject().getBuild().outputDirectory != it.toString() }
if (auxClasspathList.size() > 0) {
log.debug(" Last AuxClasspath is ->" + auxClasspathList[auxClasspathList.size() - 1])

Expand Down Expand Up @@ -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}")
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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]) {

Expand Down

0 comments on commit d6413ac

Please sign in to comment.