Skip to content

Commit 4ffbb4a

Browse files
committed
Actually fixing the issue with subdirectories that don't have a build file
1 parent bc7e27e commit 4ffbb4a

File tree

3 files changed

+60
-12
lines changed

3 files changed

+60
-12
lines changed

src/main/kotlin/com/faire/gradle/analyze/AnalyzeDependenciesPlugin.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ class AnalyzeDependenciesPlugin : Plugin<Project> {
1616
override fun apply(project: Project) {
1717
val extension = project.extensions.create<AnalyzeDependenciesPluginExtension>("analyzeDependencies")
1818

19+
// If we have a directory that doesn't have a build-file, the analyze task is going to fail.
20+
// Sometimes gradle thinks there is a project at a location with no build file.
21+
// The situation I had was as follows
22+
// foo (no build file here)
23+
// foo/bar1 (has build file)
24+
// foo/bar2 (has build file)
25+
//
26+
// Running ./gradlew analyzeDependencies would fail at :foo:analyzeDependencies
27+
if (!project.buildFile.exists()) {
28+
return
29+
}
30+
1931
project.configurations.create("permitUnusedDeclared")
2032
project.configurations.create("permitTestUnusedDeclared")
2133

@@ -37,3 +49,4 @@ class AnalyzeDependenciesPlugin : Plugin<Project> {
3749
}
3850
}
3951
}
52+

src/main/kotlin/com/faire/gradle/analyze/AnalyzeDependenciesTask.kt

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import org.gradle.api.tasks.CacheableTask
88
import org.gradle.api.tasks.Input
99
import org.gradle.api.tasks.InputFile
1010
import org.gradle.api.tasks.InputFiles
11-
import org.gradle.api.tasks.Optional
1211
import org.gradle.api.tasks.OutputFile
1312
import org.gradle.api.tasks.PathSensitive
1413
import org.gradle.api.tasks.PathSensitivity
1514
import org.gradle.api.tasks.TaskAction
15+
import java.io.File
1616

1717
open class DependencyAnalysisException(message: String) : GradleException(message)
1818

@@ -28,18 +28,8 @@ open class AnalyzeDependenciesTask : DefaultTask() {
2828

2929
// Using the buildFile as an input so that if you change any dependencies we invalidate analysis cache.
3030
@InputFile
31-
// Sometimes gradle thinks there is a project at a location with no build file. Use optional here to make sure
32-
// it doesn't fail in those cases. It would probably be better to figure out why this was happening, but there's
33-
// no time for that!
34-
// The situation I had was as follows
35-
// foo (no build file here)
36-
// foo/bar1 (has build file)
37-
// foo/bar2 (has build file)
38-
//
39-
// Running ./gradlew analyzeDependencies would fail at :foo:analyzeDependencies
40-
@Optional
4131
@PathSensitive(PathSensitivity.RELATIVE)
42-
var buildFile = project.buildFile
32+
var buildFile: File? = project.buildFile
4333

4434
@Input
4535
var justWarn: Boolean = false

src/test/kotlin/com/faire/gradle/analyze/AnalyzeDependenciesPluginTest.kt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,51 @@ class AnalyzeDependenciesPluginTest {
928928

929929
assertThat(result.output).contains("BUILD SUCCESSFUL")
930930
}
931+
932+
@Test
933+
fun `Parent directory without a build file`() {
934+
val parentDir = File(testProjectDir.root, "parent")
935+
parentDir.mkdirs()
936+
val moduleOne = File(parentDir, "moduleOne")
937+
val moduleTwo = File(parentDir, "moduleTwo")
938+
moduleOne.mkdirs()
939+
moduleTwo.mkdirs()
940+
941+
val srcDirA = File(moduleOne, "src/main/java")
942+
val srcDirB = File(moduleTwo, "src/main/java")
943+
srcDirA.mkdirs()
944+
srcDirB.mkdirs()
945+
File(srcDirA, "AFoo.java").writeText(
946+
"""
947+
package com.faire.a;
948+
public class AFoo {
949+
}
950+
""".trimIndent()
951+
)
952+
createBuildFileWithDependencies(moduleOne, listOf())
953+
954+
File(srcDirB, "BFoo.java").writeText(
955+
"""
956+
package com.faire.b;
957+
import com.faire.a.AFoo;
958+
959+
public class BFoo {
960+
BFoo(AFoo a) {}
961+
}
962+
""".trimIndent()
963+
)
964+
createBuildFileWithDependencies(moduleTwo, listOf(":parent:moduleOne"))
965+
966+
createGradleSettingsFileWithModuleIncludes(testProjectDir.root, listOf(":parent:moduleOne", ":parent:moduleTwo"))
967+
968+
val result = GradleRunner.create()
969+
.withProjectDir(testProjectDir.root)
970+
.withArguments("analyzeDependencies")
971+
.withPluginClasspath()
972+
.build()
973+
974+
assertThat(result.output).contains("BUILD SUCCESSFUL")
975+
}
931976
}
932977

933978
internal fun createGradleSettingsFileWithModuleIncludes(directory: File, modules: List<String>) {

0 commit comments

Comments
 (0)