Skip to content

Commit

Permalink
Extension requiring external libraries on demand use the buildscript …
Browse files Browse the repository at this point in the history
…of the root project for lookup.

With #99 the current project was used, which makes the resulting configuration unnecessarily complex for multi-project builds.
  • Loading branch information
fvgh committed Jul 5, 2017
1 parent e05e753 commit c188743
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
14 changes: 14 additions & 0 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ Spotless can check and apply formatting to any plain-text file, using simple rul
* [FreshMark](https://github.com/diffplug/freshmark) (markdown with variables)
* Any user-defined function which takes an unformatted string and outputs a formatted version.

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:
```gradle
buildscript { repositories { mavenCentral() } }
plugins {
id 'com.diffplug.gradle.spotless'
id 'java'
}
spotless {
java { googleJavaFormat() }
}
```

Contributions are welcome, see [the contributing guide](../CONTRIBUTING.md) for development info.

Spotless requires Gradle to be running on JRE 8+.<sup>See [issue #7](https://github.com/diffplug/spotless/issues/7) for details.</sup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;

import com.diffplug.common.base.StringPrinter;
import com.diffplug.spotless.Provisioner;

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

0 comments on commit c188743

Please sign in to comment.