GradleInfoReader implements BuildInfoReader { @Override public BuildInfo getInfo(Path root) { Path buildGradle = root.resolve(BUILD_GRADLE_GROOVY); Path settingsGradle = root.resolve(SETTINGS_GRADLE_GROOVY); boolean kts = root.resolve(BUILD_GRADLE_KTS).toFile().exists(); if (kts) { buildGradle = root.resolve(BUILD_GRADLE_KTS); settingsGradle = root.resolve(SETTINGS_GRADLE_KTS); } String language = detectLanguage(root); Path gradleProperties = root.resolve(GRADLE_PROPERTIES); Map<String, String> properties = new HashMap<>(); properties.putAll(readSettingsGradle(settingsGradle)); properties.putAll(readGradleProperties(gradleProperties)); properties.putAll(readBuildGradle(buildGradle)); String name = properties.getOrDefault(NAME, properties.getOrDefault(BASENAME, root.getFileName().toString())); String version = properties.get(VERSION); String classifier = properties.get(CLASSIFIER); String extension = properties.getOrDefault(EXTENSION, JAR); String destinationDir = properties.getOrDefault(DESTINATION_DIR, BUILD + File.separator + LIBS + File.separator); Path outputDir = root.resolve(destinationDir); StringBuilder sb = new StringBuilder(); sb.append(name); if (Strings.isNotNullOrEmpty(version)) { sb.append(DASH).append(version); } if (Strings.isNotNullOrEmpty(classifier)) { sb.append(DASH).append(classifier); } sb.append(DOT).append(extension); if (version == null) { LOGGER.warning("Could not detect project version. Using 'latest'."); version = "latest"; } return new BuildInfoBuilder() .withName(name) .withVersion(version) .withPackaging(extension) .withBuildTool(GRADLE) .withBuildToolVersion(Gradle.getVersion(root)) .withOutputFile(outputDir.resolve(sb.toString())) .withClassOutputDir(root.resolve(BUILD).resolve(CLASSES).resolve(language).resolve(MAIN)) .withResourceDir(root.resolve(SRC).resolve(MAIN).resolve(RESOURCES)) .build(); }  @Override int order(); @Override boolean isApplicable(Path root); @Override BuildInfo getInfo(Path root);  }
@Test void shouldParsePlainBuildGradle() { URL gradleInitial = GradleInfoReaderTest.class.getClassLoader().getResource(GRADLE_INITIAL); File file = Urls.toFile(gradleInitial); Path root = file.toPath().getParent(); BuildInfo info = new GradleInfoReader().getInfo(root); assertNotNull(info); assertEquals("gradle-initial", info.getName()); assertEquals("jar", info.getPackaging()); assertEquals("latest", info.getVersion()); }
@Test void shouldParseVersionedBuildGradle() { URL gradleInitial = GradleInfoReaderTest.class.getClassLoader().getResource(GRADLE_VERSIONED); File file = Urls.toFile(gradleInitial); Path root = file.toPath().getParent(); BuildInfo info = new GradleInfoReader().getInfo(root); assertNotNull(info); assertEquals("myartifact", info.getName()); assertEquals("jar", info.getPackaging()); assertEquals("1.0.0", info.getVersion()); }
@Test void shouldParseBuildGradleWithSettings() { URL gradleInitial = GradleInfoReaderTest.class.getClassLoader().getResource(GRADLE_WITH_SETTINGS); File file = Urls.toFile(gradleInitial); Path root = file.toPath().getParent(); BuildInfo info = new GradleInfoReader().getInfo(root); assertNotNull(info); assertEquals("with-settings", info.getName()); }
@Test void shouldSetCorrectResourcePath() { URL gradleInitial = GradleInfoReaderTest.class.getClassLoader().getResource(GRADLE_WITH_SETTINGS); File file = Urls.toFile(gradleInitial); Path root = file.toPath().getParent(); BuildInfo info = new GradleInfoReader().getInfo(root); assertNotNull(info); assertNotEquals(info.getResourceDir(), info.getClassOutputDir()); assertThat(info.getResourceDir().toString()).contains("src", "main", "resources"); }