Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve dependencies from "project" repositories, not buildscript #980

Merged
merged 10 commits into from
Nov 8, 2021
7 changes: 5 additions & 2 deletions gradle/spotless-freshmark.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,16 @@ spotless {
}
}

def versionLast = spotlessChangelog.versionLast
def versionNext = spotlessChangelog.versionNext

// if this freshmark has a changelog file, then it has version-sensitive content
if (tasks.names.contains('changelogCheck')) {
// normally we use versionLast for our freshmark
spotless {
freshmark {
properties {
it.put('versionLast', spotlessChangelog.versionLast)
it.put('versionLast', versionLast)
}
}
}
Expand All @@ -65,7 +68,7 @@ if (tasks.names.contains('changelogCheck')) {
freshmarkSetup.execute(freshmark)
freshmark.properties {
// that uses versionNext as versionLast
it.put('versionLast', spotlessChangelog.versionNext)
it.put('versionLast', versionNext)
}
def changelogBumpFreshmark = freshmark.createIndependentApplyTask('changelogBumpFreshmark')
// freshmark should run after the changelog bump
Expand Down
2 changes: 1 addition & 1 deletion gradle/spotless.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ spotless {
*/
String regex = "import org\\.gradle\\.api\\.internal\\.(?!plugins\\.DslObject)(?!project\\.ProjectInternal)"
if ((text.contains('import org.gradle.internal.') || text.find(regex)) &&
!text.contains('def noInternalDepsClosure')) {
!text.contains('def noInternalDepsClosure')) {
throw new AssertionError("Accidental internal import")
}
}
Expand Down
4 changes: 4 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).

## [Unreleased]
### Changed
* **BREAKING** Previously, many projects required `buildscript { repositories { mavenCentral() }}` at the top of their root project, because Spotless resolved its dependencies using the buildscript repositories. Spotless now resolves its dependencies from the normal project repositories of the root project, which means that you can remove the `buildscript {}` block, but you still need `repositories { mavenCentral() }` (or similar) in the root project.
* Bump minimum required Gradle from `6.1` to `6.1.1`.

## [5.17.1] - 2021-10-26
### Changed
Expand Down Expand Up @@ -44,6 +47,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
* `:spotlessInternalRegisterDependencies task failed.`
* `Cannot add a configuration with name 'spotless-1911100560'`
* Spotless does not [yet](https://github.com/diffplug/spotless/pull/721) support configuration-cache, but now it can never interfere with configuration-cache for other tasks. ([#720](https://github.com/diffplug/spotless/pull/720))
* Bump minimum required Gradle from `5.4` to `6.1`.

## [5.15.0] - 2021-09-04
### Added
Expand Down
2 changes: 1 addition & 1 deletion plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ All the generic steps live in [`FormatExtension`](https://javadoc.io/doc/com.dif

### Requirements

Spotless requires JRE 8+, and Gradle 5.4+. Some steps require JRE 11+, `Unsupported major.minor version` means you're using a step that needs a newer JRE.
Spotless requires JRE 8+, and Gradle 6.1.1+. Some steps require JRE 11+, `Unsupported major.minor version` means you're using a step that needs a newer JRE.

If you're stuck on an older version of Gradle, `id 'com.diffplug.gradle.spotless' version '4.5.1'` supports all the way back to Gradle 2.x`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public Set<File> provisionWithTransitives(boolean withTransitives, Collection<St
if (result != null) {
return result;
} else {
result = GradleProvisioner.fromRootBuildscript(rootProject).provisionWithTransitives(req.withTransitives, req.mavenCoords);
result = GradleProvisioner.forProject(rootProject).provisionWithTransitives(req.withTransitives, req.mavenCoords);
cache.put(req, result);
return result;
}
Expand All @@ -69,23 +69,24 @@ public Set<File> provisionWithTransitives(boolean withTransitives, Collection<St
}
}

static Provisioner fromRootBuildscript(Project project) {
static Provisioner forProject(Project project) {
Objects.requireNonNull(project);
return (withTransitives, mavenCoords) -> {
try {
Configuration config = project.getRootProject().getBuildscript().getConfigurations().create("spotless"
Configuration config = project.getConfigurations().create("spotless"
+ new Request(withTransitives, mavenCoords).hashCode());
mavenCoords.stream()
.map(project.getBuildscript().getDependencies()::create)
.map(project.getDependencies()::create)
.forEach(config.getDependencies()::add);
config.setDescription(mavenCoords.toString());
config.setTransitive(withTransitives);
return config.resolve();
} catch (Exception e) {
String projName = project.getPath();
logger.log(
Level.SEVERE,
"You probably need to add a repository containing the '" + mavenCoords + "' artifact in the 'build.gradle' of your root project.\n" +
"E.g.: 'buildscript { repositories { mavenCentral() }}'",
"You probably need to add a repository containing the '" + mavenCoords + "' artifact in the 'build.gradle' of the " + projName + " project.\n" +
"E.g.: 'repositories { mavenCentral() }'",
e);
throw e;
}
Expand Down Expand Up @@ -125,7 +126,7 @@ public boolean equals(Object obj) {
public String toString() {
String coords = mavenCoords.toString();
StringBuilder builder = new StringBuilder();
builder.append(coords.substring(1, coords.length() - 1)); // strip off []
builder.append(coords, 1, coords.length() - 1); // strip off []
if (withTransitives) {
builder.append(" with transitives");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

public class SpotlessPlugin implements Plugin<Project> {
static final String SPOTLESS_MODERN = "spotlessModern";
static final String MINIMUM_GRADLE = "6.1";
static final String MINIMUM_GRADLE = "6.1.1";

@Override
public void apply(Project project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,10 @@ class Antlr4ExtensionTest extends GradleIntegrationHarness {
@Test
void applyUsingDefaultVersion() throws IOException {
String[] buildScript = {
"buildscript {",
" repositories {",
" mavenCentral()",
" }",
"}",
"plugins {",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
"spotless {",
" antlr4 {",
" target 'src/main/antlr4/**/*.g4'",
Expand All @@ -45,14 +41,10 @@ void applyUsingDefaultVersion() throws IOException {
@Test
void applyUsingCustomVersion() throws IOException {
String[] buildScript = {
"buildscript {",
" repositories {",
" mavenCentral()",
" }",
"}",
"plugins {",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
"spotless {",
" antlr4 {",
" target 'src/main/antlr4/**/*.g4'",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class ConfigAvoidanceTest extends GradleIntegrationHarness {
@Test
void noConfigOnHelp() throws IOException {
setFile("build.gradle").toLines(
"buildscript { repositories { mavenCentral() } }",
"plugins {",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
"apply plugin: 'java'",
"spotless {",
" java {",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ protected void runTasks(String... tasks) throws IOException {
@Test
public void helpConfigures() throws IOException {
setFile("build.gradle").toLines(
"buildscript { repositories { mavenCentral() } }",
"plugins {",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
"apply plugin: 'java'",
"spotless {",
" java {",
Expand All @@ -53,10 +53,10 @@ public void helpConfigures() throws IOException {
@Test
public void helpConfiguresIfTasksAreCreated() throws IOException {
setFile("build.gradle").toLines(
"buildscript { repositories { mavenCentral() } }",
"plugins {",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
"apply plugin: 'java'",
"spotless {",
" java {",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ class FilePermissionsTest extends GradleIntegrationHarness {
@DisabledOnOs(WINDOWS)
void spotlessApplyShouldPreservePermissions() throws IOException {
setFile("build.gradle").toLines(
"buildscript { repositories { mavenCentral() } }",
"plugins {",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
"",
"spotless {",
" java {",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ class FreshMarkExtensionTest extends GradleIntegrationHarness {
@EnabledForJreRange(max = JAVA_14)
void integration() throws IOException {
setFile("build.gradle").toLines(
"buildscript { repositories { mavenCentral() } }",
"plugins {",
" id 'java'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
"spotless {",
" freshmark {",
" target '*.md'",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class GoogleJavaFormatIntegrationTest extends GradleIntegrationHarness {
@Test
void integration() throws IOException {
setFile("build.gradle").toLines(
"buildscript { repositories { mavenCentral() } }",
"plugins {",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
"",
"spotless {",
" java {",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class IndependentTaskTest extends GradleIntegrationHarness {
@Test
void independent() throws IOException {
setFile("build.gradle").toLines(
"buildscript { repositories { mavenCentral() } }",
"plugins {",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
"",
"import com.diffplug.gradle.spotless.JavaExtension",
"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class JavaDefaultTargetTest extends GradleIntegrationHarness {
@Test
void integration() throws IOException {
setFile("build.gradle").toLines(
"buildscript { repositories { mavenCentral() } }",
"plugins {",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
"",
"apply plugin: 'groovy'",
"",
Expand All @@ -49,11 +49,11 @@ void integration() throws IOException {
@Test
void multipleBlocksShouldWork() throws IOException {
setFile("build.gradle").toLines(
"buildscript { repositories { mavenCentral() } }",
"plugins {",
" id 'com.diffplug.spotless'",
" id 'java'",
"}",
"repositories { mavenCentral() }",
"",
"spotless {",
" java { googleJavaFormat() }",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ class JsonExtensionTest extends GradleIntegrationHarness {
@Test
void defaultFormatting() throws IOException {
setFile("build.gradle").toLines(
"buildscript { repositories { mavenCentral() } }",
"plugins {",
" id 'java'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
"spotless {",
" json {",
" target 'examples/**/*.json'",
Expand All @@ -44,11 +44,11 @@ void defaultFormatting() throws IOException {
@Test
void formattingWithCustomNumberOfSpaces() throws IOException {
setFile("build.gradle").toLines(
"buildscript { repositories { mavenCentral() } }",
"plugins {",
" id 'java'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
"spotless {",
" json {",
" target 'src/**/*.json'",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class KotlinExtensionTest extends GradleIntegrationHarness {
void integration() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'nebula.kotlin' version '1.3.72'",
" id 'org.jetbrains.kotlin.jvm' version '1.5.31'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
Expand Down Expand Up @@ -69,7 +69,7 @@ void integrationDiktat() throws IOException {
void integrationKtfmt() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'nebula.kotlin' version '1.3.72'",
" id 'org.jetbrains.kotlin.jvm' version '1.5.31'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
Expand All @@ -88,7 +88,7 @@ void integrationKtfmt() throws IOException {
void integrationKtfmt_dropboxStyle_0_18() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'nebula.kotlin' version '1.3.72'",
" id 'org.jetbrains.kotlin.jvm' version '1.5.31'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
Expand All @@ -107,7 +107,7 @@ void integrationKtfmt_dropboxStyle_0_18() throws IOException {
void integrationKtfmt_dropboxStyle_0_19() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'nebula.kotlin' version '1.3.72'",
" id 'org.jetbrains.kotlin.jvm' version '1.5.31'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
Expand All @@ -125,13 +125,13 @@ void integrationKtfmt_dropboxStyle_0_19() throws IOException {
void testWithIndentation() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'nebula.kotlin' version '1.3.72'",
" id 'org.jetbrains.kotlin.jvm' version '1.5.31'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
"spotless {",
" kotlin {",
" ktlint('0.21.0').userData(['indent_size': '6'])",
" ktlint('0.32.0').userData(['indent_size': '6'])",
" }",
"}");
setFile("src/main/kotlin/basic.kt").toResource("kotlin/ktlint/basic.dirty");
Expand All @@ -143,7 +143,7 @@ void testWithIndentation() throws IOException {
void testWithHeader() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'nebula.kotlin' version '1.3.72'",
" id 'org.jetbrains.kotlin.jvm' version '1.5.31'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
Expand All @@ -163,7 +163,7 @@ void testWithHeader() throws IOException {
void testWithHeaderKtfmt() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'nebula.kotlin' version '1.3.72'",
" id 'org.jetbrains.kotlin.jvm' version '1.5.31'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
Expand All @@ -182,7 +182,7 @@ void testWithHeaderKtfmt() throws IOException {
void testWithCustomHeaderSeparator() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'nebula.kotlin' version '1.3.72'",
" id 'org.jetbrains.kotlin.jvm' version '1.5.31'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
Expand All @@ -202,7 +202,7 @@ void testWithCustomHeaderSeparator() throws IOException {
void testWithCustomHeaderSeparatorKtfmt() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'nebula.kotlin' version '1.3.72'",
" id 'org.jetbrains.kotlin.jvm' version '1.5.31'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
Expand All @@ -221,7 +221,7 @@ void testWithCustomHeaderSeparatorKtfmt() throws IOException {
void testWithNonStandardYearSeparator() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'nebula.kotlin' version '1.3.72'",
" id 'org.jetbrains.kotlin.jvm' version '1.5.31'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
Expand All @@ -248,7 +248,7 @@ void testWithNonStandardYearSeparator() throws IOException {
void testWithNonStandardYearSeparatorKtfmt() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'nebula.kotlin' version '1.3.72'",
" id 'org.jetbrains.kotlin.jvm' version '1.5.31'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
Expand Down
Loading