spotless: Cannot resolve external dependency com.google.googlejavaformat:google-java-format:1.3

Recently I have upgraded to spotless 3.4.0. Gradle 3.5 and Java 1.8 My spotless config

buildscript {
  repositories {
    mavenCentral()
    maven { url 'https://plugins.gradle.org/m2/' }
  }
  dependencies {
    classpath 'com.diffplug.spotless:spotless-plugin-gradle:3.4.0'
    classpath 'com.google.googlejavaformat:google-java-format:1.3'
  }
}

apply plugin: com.diffplug.gradle.spotless.SpotlessPlugin

spotless {
  java {
    googleJavaFormat('1.3')
    custom 'Lambda fix', { it.replace('} )', '})').replace('} ,', '},') }
    importOrder(['java', 'javax', 'org', 'com'])
    trimTrailingWhitespace()
    removeUnusedImports()
  }
  format 'misc', {
    target '**/.gitignore', '**/*.gradle', '**/*.md', '**/*.sh'
    indentWithSpaces(2)
    trimTrailingWhitespace()
    endWithNewline()
  }
}

I am getting following error.

:spotlessJava
You probably need to add a repository containing the '[com.google.googlejavaformat:google-java-format:1.3]' artifact, e.g.: 'buildscript { repositories { mavenCentral() }}'
org.gradle.api.artifacts.ResolveException: Could not resolve all dependencies for configuration ':detachedConfiguration1'.
        at org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration.rethrowFailure(DefaultLenientConfiguration.java:192)
        at 
 (tons more)

If I downgrade to 3.0.0 its just working fine for the same config.


EDIT by @nedtwigg: this is a long thread, but the resolution was that the buildscript block above must appear in build.gradle, it’s not enough for it to be appear in a separate file, such as spotless.gradle, which gets included.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 21 (20 by maintainers)

Most upvoted comments

The code you linked to does not have buildscript { repositories { mavenCentral() } }. If I add that little snippet to the code, then it works.

Sorry Ned, I am unsure now what your final word is. In your previous comment you stated that security concerns should not block for my proposal. And you mentioned CI. My experience with CI was actually the key point why I think my proposal is not a hack. My proxy config looks like this:

allprojects {
	buildscript {
	    repositories {
		maven {
			url "http://<ip>:<port>/apache-archiva/repository/internal/"
		}
	    }
	}		
	repositories {
		maven {
			url "http://<ip>:<port>/apache-archiva/repository/internal/"
		}
	}
}

This is in my opinion how gradle wants a proxy to be configured. If the proxy can can not resolve the extensions and direct access of Maven Central is denied, than you get an error like you do now. I had the problem that my proxy forwards request to Maven Central. So when #99 was solved, I needed no changes in my build scripts. But when I committed the JUnit changes, JUnit Travis failed. This would not have happen with my proposed solution. When I said that disallowing access to all Maven Central versions (via proxy or not), I meant ‘rare’ in the sense that it only affect less than 5% of the users. 95% of the users would be able just to use spotless without any further configuration, regardless whether they use multi-project, or configured spotless in a dedicated gradle file. Just let me now what the final solution shall be.

rootProject is possible, but still does not help (at least according to my understanding of the Gradle Doc) if you use apply plugin and have your spotless config in a separate script, which I think is quite common scenario. Let me look for the right hook and the solution will cover (I’ll add tests 😄 ) both scenarios.

Does your project have subprojects? Your project file as written above works for me. If you have subprojects, you might need a block like this:

subprojects {
  buildscript {
    repositories {
      mavenCentral()
    }
  }
}