Skip to content

Commit

Permalink
fix gradle 7 deprecation warnings (fixes #30)
Browse files Browse the repository at this point in the history
  • Loading branch information
xvik committed Nov 7, 2021
1 parent 3b39cb7 commit ac86851
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Fix gradle 7 deprecation warnings (#30)
* Update spotbugs plugin 4.7.1 -> 4.7.5
* Deprecate quality.pmdIncremental property because incremental analysis is enabled by default
since gradle 6.4 (property only useful for enabling it in gradle 5.6 - 6.3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.gradle.api.execution.TaskExecutionGraph
import org.gradle.api.plugins.GroovyPlugin
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.plugins.quality.*
import org.gradle.api.reporting.Report
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.SourceTask
import org.gradle.api.tasks.TaskProvider
Expand Down Expand Up @@ -152,10 +153,8 @@ class QualityPlugin implements Plugin<Project> {
configLoader.resolveCheckstyleConfig()
applyExcludes(it, extension)
}
reports {
xml.enabled = true
html.enabled = extension.htmlReports
}
enableReport(reports.xml)
enableReport(reports.html, extension.htmlReports)
}
}
configurePluginTasks(project, extension, Checkstyle, 'checkstyle', new CheckstyleReporter(configLoader))
Expand Down Expand Up @@ -191,10 +190,8 @@ class QualityPlugin implements Plugin<Project> {
configLoader.resolvePmdConfig()
applyExcludes(it, extension)
}
reports {
xml.enabled = true
html.enabled = extension.htmlReports
}
enableReport(reports.xml)
enableReport(reports.html, extension.htmlReports)
}
}
configurePluginTasks(project, extension, Pmd, 'pmd', new PmdReporter())
Expand Down Expand Up @@ -243,7 +240,7 @@ class QualityPlugin implements Plugin<Project> {
)))
reports {
xml {
enabled true
enableReport(it)
}
}
}
Expand Down Expand Up @@ -272,10 +269,8 @@ class QualityPlugin implements Plugin<Project> {
configLoader.resolveCodenarcConfig()
applyExcludes(it, extension)
}
reports {
xml.enabled = true
html.enabled = extension.htmlReports
}
enableReport(reports.xml)
enableReport(reports.html, extension.htmlReports)
}
}
configurePluginTasks(project, extension, CodeNarc, 'codenarc', new CodeNarcReporter())
Expand Down Expand Up @@ -348,9 +343,7 @@ class QualityPlugin implements Plugin<Project> {
Class<Task> cpdTasksType = plugin.class.classLoader.loadClass('de.aaschmid.gradle.plugins.cpd.Cpd')
// reports applied for all registered cpd tasks
prj.tasks.withType(cpdTasksType).configureEach { task ->
reports {
xml.enabled = true
}
enableReport(reports.xml)
doFirst {
configLoader.resolveCpdXsl()
}
Expand All @@ -370,6 +363,14 @@ class QualityPlugin implements Plugin<Project> {
}
}

private void enableReport(Report report, boolean enable = true) {
if (GradleVersion.current() < GradleVersion.version('7.0')) {
report.enabled = enable
} else {
report.required.set(enable)
}
}

private void applyReporter(Project project, String type, Reporter reporter,
boolean consoleReport, boolean htmlReport) {
boolean generatesHtmlReport = htmlReport && HtmlReportGenerator.isAssignableFrom(reporter.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class CheckstyleReporter implements Reporter<Checkstyle> {
@Override
@CompileStatic(TypeCheckingMode.SKIP)
void report(Checkstyle task, String type) {
File reportFile = task.reports.xml.destination
File reportFile = ReportUtils.getReportFile(task.reports.xml)
if (!reportFile.exists() || reportFile.length() == 0) {
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CodeNarcReporter implements Reporter<CodeNarc> {
@SuppressWarnings('DuplicateStringLiteral')
@CompileStatic(TypeCheckingMode.SKIP)
void report(CodeNarc task, String type) {
File reportFile = task.reports.xml.destination
File reportFile = ReportUtils.getReportFile(task.reports.xml)
if (!reportFile.exists() || reportFile.length() == 0) {
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class CpdReporter implements Reporter<SourceTask>, HtmlReportGenerator<SourceTas
@Override
@CompileStatic(TypeCheckingMode.SKIP)
void report(SourceTask task, String type) {
File reportFile = task.reports.xml.destination
File reportFile = ReportUtils.getReportFile(task.reports.xml)

if (!reportFile.exists() || reportFile.length() == 0) {
return
Expand Down Expand Up @@ -75,7 +75,7 @@ class CpdReporter implements Reporter<SourceTask>, HtmlReportGenerator<SourceTas
@Override
@CompileStatic(TypeCheckingMode.SKIP)
void generateHtmlReport(SourceTask task, String type) {
File reportFile = task.reports.xml.destination
File reportFile = ReportUtils.getReportFile(task.reports.xml)
if (!reportFile.exists()) {
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class PmdReporter implements Reporter<Pmd> {
@Override
@CompileStatic(TypeCheckingMode.SKIP)
void report(Pmd task, String type) {
File reportFile = task.reports.xml.destination
File reportFile = ReportUtils.getReportFile(task.reports.xml)
if (!reportFile.exists() || reportFile.length() == 0) {
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import groovy.transform.CompileStatic
import groovy.transform.TypeCheckingMode
import org.gradle.api.Project
import org.gradle.api.plugins.JavaBasePlugin
import org.gradle.api.reporting.Report
import org.gradle.api.tasks.SourceSet
import org.gradle.util.GradleVersion

/**
* Reporting utils.
Expand Down Expand Up @@ -98,6 +100,21 @@ class ReportUtils {
return "file:///${noRootFilePath(file)}"
}

/**
* Required because destination property was deprecated since gradle 7, but it still must be used for
* older gradle versions.
*
* @param report report instance
* @return report destination file
*/
static File getReportFile(Report report) {
if (report == null) {
return null
}
return GradleVersion.current() < GradleVersion.version('7.0')
? report.destination : report.outputLocation.get().asFile
}

private static String matchRoot(SourceSet set, String filePath) {
Closure search = { Iterable<File> files ->
files*.canonicalPath.find { String s -> filePath.startsWith(s) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class SpotbugsReporter implements Reporter<SpotBugsTask>, HtmlReportGenerator<Sp
@Override
@CompileStatic(TypeCheckingMode.SKIP)
void report(SpotBugsTask task, String type) {
File reportFile = task.reports.findByName(XML)?.destination
// report may not exists
File reportFile = ReportUtils.getReportFile(task.reports.findByName(XML))
if (reportFile == null || !reportFile.exists() || reportFile.length() == 0) {
return
}
Expand Down Expand Up @@ -72,7 +73,7 @@ class SpotbugsReporter implements Reporter<SpotBugsTask>, HtmlReportGenerator<Sp
@Override
@CompileStatic(TypeCheckingMode.SKIP)
void generateHtmlReport(SpotBugsTask task, String type) {
File reportFile = task.reports.findByName(XML)?.destination
File reportFile = ReportUtils.getReportFile(task.reports.findByName(XML))
if (reportFile == null || !reportFile.exists()) {
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import groovy.xml.XmlUtil
import org.gradle.api.Project
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.util.GradleVersion
import org.slf4j.Logger
import ru.vyarus.gradle.plugin.quality.QualityExtension

Expand Down Expand Up @@ -82,8 +83,10 @@ class SpotbugsUtils {
return
}
// apt is a special dir, not mentioned in sources!
File aptGenerated = (task.project.tasks.findByName(set.compileJavaTaskName) as JavaCompile)
.options.annotationProcessorGeneratedSourcesDirectory
JavaCompile javaCompile = task.project.tasks.findByName(set.compileJavaTaskName) as JavaCompile
File aptGenerated = GradleVersion.current() < GradleVersion.version('7.0')
? javaCompile.options.annotationProcessorGeneratedSourcesDirectory
: javaCompile.options.generatedSourceOutputDirectory.get().asFile

Set<File> ignored = FileUtils.resolveIgnoredFiles(task.sourceDirs.asFileTree, extension.exclude)
// exclude all apt-generated files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.gradle.testkit.runner.TaskOutcome
class UpstreamKitTest extends AbstractKitTest {


public static final String GRADLE = '7.1'
public static final String GRADLE = '7.2'

def "Check java checks"() {
setup:
Expand All @@ -39,7 +39,7 @@ class UpstreamKitTest extends AbstractKitTest {
fileFromClasspath('src/main/java/sample/Sample2.java', '/ru/vyarus/gradle/plugin/quality/java/sample/Sample2.java')

when: "run check task"
BuildResult result = runVer(GRADLE,'check')
BuildResult result = runVer(GRADLE,'check', '--warning-mode', 'all')

then: "all plugins detect violations"
result.task(":check").outcome == TaskOutcome.SUCCESS
Expand Down Expand Up @@ -75,7 +75,7 @@ class UpstreamKitTest extends AbstractKitTest {
fileFromClasspath('src/main/groovy/sample/GSample2.groovy', '/ru/vyarus/gradle/plugin/quality/groovy/sample/GSample2.groovy')

when: "run check task with both sources"
BuildResult result = runVer(GRADLE,'check')
BuildResult result = runVer(GRADLE,'check', '--warning-mode', 'all')

then: "all plugins detect violations"
result.task(":check").outcome == TaskOutcome.SUCCESS
Expand Down

0 comments on commit ac86851

Please sign in to comment.