@@ -11,6 +11,7 @@ import com.sourcegraph.scip_java.BuildInfo
1111import org .gradle .api .DefaultTask
1212import org .gradle .api .Plugin
1313import org .gradle .api .Project
14+ import org .gradle .api .artifacts .Configuration
1415import org .gradle .api .artifacts .component .ModuleComponentIdentifier
1516import org .gradle .api .provider .Property
1617import org .gradle .api .publish .PublishingExtension
@@ -65,15 +66,16 @@ class SemanticdbGradlePlugin extends Plugin[Project] {
6566 triggers += " compileJava"
6667 triggers += " compileTestJava"
6768
68- val hasAnnotationPath = {
69- val apConfig = project
70- .getConfigurations()
71- .getByName(" annotationProcessor" )
72- if (apConfig.isCanBeResolved()) {
73- apConfig.getDependencies().size() > 0
74- } else
75- false
76- }
69+ val hasAnnotationPath = Try (
70+ project.getConfigurations().getByName(" annotationProcessor" )
71+ ).map(apConfig =>
72+ if (apConfig.isCanBeResolved()) {
73+ apConfig.getDependencies().size() > 0
74+ } else
75+ false
76+ )
77+ .toOption
78+ .contains(true )
7779
7880 val compilerPluginAdded =
7981 try {
@@ -93,22 +95,23 @@ class SemanticdbGradlePlugin extends Plugin[Project] {
9395 // If the `compileOnly` configuration has already been evaluated
9496 // by the build, we need to fallback on agent injected into javac
9597 warn(
96- s " Failed to add compiler plugin to javac, will go through the agent route: ${exc.getMessage()}"
98+ s " Failed to add compiler plugin to javac, will go through the agent route ( ${exc
99+ .getClass()}): ${exc.getMessage()}"
97100 )
98101 false
99102 }
100103
101104 project
102105 .getTasks()
103106 .withType(classOf [JavaCompile ])
104- .configureEach { task =>
107+ .all { task =>
105108 // If we run on JDK 17, we need to add special flags to the JVM
106109 // to allow access to the compiler.
107110
108111 // JDK 17 support was only introduced in 7.3 so
109112 // we don't need to do it for earlier versions
110113 // https://docs.gradle.org/current/userguide/compatibility.html
111- if (! gradle.is5 && ! gradle.is6) {
114+ if (! gradle.is3 && ! gradle.is2 && ! gradle. is5 && ! gradle.is6) {
112115 type JavaCompiler = {
113116 type Metadata = {
114117 type LangVersion = {
@@ -118,6 +121,7 @@ class SemanticdbGradlePlugin extends Plugin[Project] {
118121 }
119122 def getMetadata (): Metadata
120123 }
124+
121125 type HasCompilerProperty = {
122126 def getJavaCompiler (): Property [JavaCompiler ]
123127 }
@@ -320,7 +324,7 @@ class SemanticdbGradlePlugin extends Plugin[Project] {
320324 // classpath is murky
321325 //
322326 // We also don't want to bundle kotlin plugin with this one as it
323- // can cause all sorts of troubles.
327+ // can cause all sorts of troubles) .
324328 //
325329 // Instead, we commit the sins of reflection for our limited
326330 // needs.
@@ -368,7 +372,7 @@ class SemanticdbGradlePlugin extends Plugin[Project] {
368372 }
369373 }
370374
371- tasks.register (
375+ tasks.create (
372376 " scipCompileAll" ,
373377 { task =>
374378 triggers
@@ -380,31 +384,34 @@ class SemanticdbGradlePlugin extends Plugin[Project] {
380384 }
381385 )
382386
383- tasks.register (" scipPrintDependencies" , classOf [WriteDependencies ])
387+ tasks.create (" scipPrintDependencies" , classOf [WriteDependencies ])
384388
385389 }
386390
387391 }
388392
389- class GradleVersion (ver : String ) {
390- override def toString (): String = s " [GradleVersion: $ver] "
391- def is7 = ver.startsWith(" 7." )
392- def is8 = ver.startsWith(" 8." )
393- def is6 = ver.startsWith(" 6." )
394- // 6.7 introduced toolchains support https://blog.gradle.org/java-toolchains
395- // And javaCompiler property
396- def is6_7_plus = {
397- ver match {
398- case s " 6. $x. $y" if x.toInt >= 7 =>
399- true
400- case s " 6. $x" if x.toInt >= 7 =>
401- true
402- case _ =>
403- false
404- }
393+ }
394+
395+ class GradleVersion (ver : String ) {
396+ override def toString (): String = s " [GradleVersion: $ver] "
397+ def is7 = ver.startsWith(" 7." )
398+ def is8 = ver.startsWith(" 8." )
399+ def is6 = ver.startsWith(" 6." )
400+ // 6.7 introduced toolchains support https://blog.gradle.org/java-toolchains
401+ // And javaCompiler property
402+ def is6_7_plus = {
403+ ver match {
404+ case s " 6. $x. $y" if x.toInt >= 7 =>
405+ true
406+ case s " 6. $x" if x.toInt >= 7 =>
407+ true
408+ case _ =>
409+ false
405410 }
406- def is5 = ver.startsWith(" 5." )
407411 }
412+ def is5 = ver.startsWith(" 5." )
413+ def is3 = ver.startsWith(" 3." )
414+ def is2 = ver.startsWith(" 2." )
408415}
409416
410417class WriteDependencies extends DefaultTask {
@@ -424,6 +431,8 @@ class WriteDependencies extends DefaultTask {
424431 val project = getProject()
425432 val projectName = project.getName()
426433
434+ val gradle = new GradleVersion (project.getGradle().getGradleVersion())
435+
427436 // List the project itself as a dependency so that we can assign project name/version to symbols that are defined in this project.
428437 // The code below is roughly equivalent to the following with Groovy:
429438 // deps += "$publication.groupId $publication.artifactId $publication.version $sourceSets.main.output.classesDirectory"
@@ -448,7 +457,7 @@ class WriteDependencies extends DefaultTask {
448457 warn(s """
449458 |Failed to extract Maven publication from the project ` $projectName`.
450459 $crossRepoBanner
451- |Here's the raw error message:
460+ |Here's the raw error message ( ${exception.getClass()} ) :
452461 | " ${exception.getMessage()}"
453462 |Continuing without cross-repository support.
454463 """ .stripMargin.trim())
@@ -500,10 +509,16 @@ class WriteDependencies extends DefaultTask {
500509 }
501510 }
502511
512+ def canBeResolved (conf : Configuration ) =
513+ if (gradle.is2)
514+ ! conf.isEmpty()
515+ else
516+ conf.isCanBeResolved()
517+
503518 project
504519 .getConfigurations()
505520 .forEach { conf =>
506- if (conf.isCanBeResolved( )) {
521+ if (canBeResolved(conf )) {
507522 try {
508523 val resolved = conf.getResolvedConfiguration()
509524 resolved
0 commit comments