Skip to content

Commit c188743

Browse files
committed
Extension requiring external libraries on demand use the buildscript of the root project for lookup.
With #99 the current project was used, which makes the resulting configuration unnecessarily complex for multi-project builds.
1 parent e05e753 commit c188743

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

plugin-gradle/README.md

+14
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@ Spotless can check and apply formatting to any plain-text file, using simple rul
8383
* [FreshMark](https://github.com/diffplug/freshmark) (markdown with variables)
8484
* Any user-defined function which takes an unformatted string and outputs a formatted version.
8585

86+
Formatters relying on external libraries (like Eclipse and Google formatters), use the repositories specified in the **root project** build script to download missing libraries on demand. All external libraries are available via [Maven Central](http://search.maven.org/), so a typical script looks like:
87+
```gradle
88+
buildscript { repositories { mavenCentral() } }
89+
90+
plugins {
91+
id 'com.diffplug.gradle.spotless'
92+
id 'java'
93+
}
94+
95+
spotless {
96+
java { googleJavaFormat() }
97+
}
98+
```
99+
86100
Contributions are welcome, see [the contributing guide](../CONTRIBUTING.md) for development info.
87101

88102
Spotless requires Gradle to be running on JRE 8+.<sup>See [issue #7](https://github.com/diffplug/spotless/issues/7) for details.</sup>

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GradleProvisioner.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.gradle.api.artifacts.Configuration;
2424
import org.gradle.api.artifacts.Dependency;
2525

26+
import com.diffplug.common.base.StringPrinter;
2627
import com.diffplug.spotless.Provisioner;
2728

2829
/** Gradle integration for Provisioner. */
@@ -36,11 +37,16 @@ public static Provisioner fromProject(Project project) {
3637
Dependency[] deps = mavenCoords.stream()
3738
.map(project.getBuildscript().getDependencies()::create)
3839
.toArray(Dependency[]::new);
39-
Configuration config = project.getBuildscript().getConfigurations().detachedConfiguration(deps);
40+
Configuration config = project.getRootProject().getBuildscript().getConfigurations().detachedConfiguration(deps);
4041
config.setDescription(mavenCoords.toString());
4142
return config.resolve();
4243
} catch (Exception e) {
43-
logger.log(Level.SEVERE, "You probably need to add a repository containing the '" + mavenCoords + "' artifact, e.g.: 'buildscript { repositories { mavenCentral() }}'", e);
44+
logger.log(Level.SEVERE,
45+
StringPrinter.buildStringFromLines("You probably need to add a repository containing the '" + mavenCoords + "' artifact in the 'build.gradle' of your root project.",
46+
"E.g.: 'buildscript { repositories { mavenCentral() }}'",
47+
"Note that included buildscripts (using 'apply from') do not share their buildscript repositories with the underlying project.",
48+
"You have to specify the missing repository explicitly in the buildscript of the root project."),
49+
e);
4450
throw e;
4551
}
4652
};

0 commit comments

Comments
 (0)