Migrate to spotless (#1748)

* Use spotless for formatting / license check

* Apply formatting

* README

* Apply to example

* google-java-format 1.8 for now

* google-java-format 1.7 for now (last supporting Java 8)
This commit is contained in:
Anuraag Agrawal 2020-10-05 16:41:21 +09:00 committed by GitHub
parent eb4a18febc
commit ed169645f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 46 additions and 53 deletions

View File

@ -14,28 +14,29 @@ Make sure to review the projects [license](LICENSE) and sign the
[CNCF CLA](https://identity.linuxfoundation.org/projects/cncf). A signed CLA will be enforced by an
automatic check once you submit a PR, but you can also sign it after opening your PR.
## Checks
Before submitting a PR, you should make sure the style checks and unit tests pass. You can run these
with the `check` task.
```bash
$ ./gradlew check
```
## Style guideline
We follow the [Google Java Style Guide](https://google.github.io/styleguide/javaguide.html).
Our build will fail if source code is not formatted according to that style.
Our build will fail if source code is not formatted according to that style. To fix any
style failures the above [checks](#checks) show, automatically apply the formatting with
```bash
$ ./gradlew spotlessApply
```
To verify code style manually run the following command,
which uses [google-java-format](https://github.com/google/google-java-format) library:
`./gradlew verifyGoogleJavaFormat`
or on Windows
`gradlew.bat verifyGoogleJavaFormat`
Instead of fixing style inconsistencies by hand, you can run gradle task `googleJavaFormat`
to automatically fix all found issues:
`./gradlew googleJavaFormat`
or on Windows
`gradlew.bat googleJavaFormat`
`./gradlew spotless`
### Pre-commit hook
To completely delegate code style formatting to the machine,
@ -43,11 +44,10 @@ you can add [git pre-commit hook](https://git-scm.com/docs/githooks).
We provide an example script in `buildscripts/pre-commit` file.
Just copy or symlink it into `.git/hooks` folder.
### Editorconfig
As additional convenience for IntelliJ Idea users, we provide `.editorconfig` file.
Idea will automatically use it to adjust its code formatting settings.
It does not support all required rules, so you still have to run `googleJavaFormat` from time to time.
It does not support all required rules, so you still have to run `spotlessApply` from time to time.
### Javadoc

View File

@ -19,7 +19,7 @@ init-git-submodules:
.PHONY: verify-format
verify-format:
./gradlew verGJF
./gradlew spotlessCheck
.PHONY: publish-snapshots
publish-snapshots:

View File

@ -1,5 +1,5 @@
plugins {
id "com.github.sherter.google-java-format" apply false
id "com.diffplug.spotless" version "5.6.1"
id "com.jfrog.artifactory" apply false
id "com.jfrog.bintray" version "1.8.4" apply false // Need *specific* version.
id "net.ltgt.errorprone" apply false
@ -137,7 +137,7 @@ configure(opentelemetryProjects) {
apply plugin: 'signing'
apply plugin: 'jacoco'
apply plugin: 'com.github.sherter.google-java-format'
apply plugin: 'com.diffplug.spotless'
apply plugin: 'net.ltgt.errorprone'
repositories {
@ -299,8 +299,11 @@ configure(opentelemetryProjects) {
jacoco { toolVersion = "0.8.5" }
googleJavaFormat {
toolVersion = '1.7'
spotless {
java {
googleJavaFormat("1.7")
licenseHeaderFile rootProject.file('buildscripts/spotless.license.java'), '(package|import|class|// Includes work from:)'
}
}
configurations {
@ -363,17 +366,7 @@ configure(opentelemetryProjects) {
addBooleanOption('Xdoclint:all,-missing', true)
}
afterEvaluate { // Allow subproject to add more source sets.
tasks.googleJavaFormat {
source = sourceSets*.allJava
include '**/*.java'
}
tasks.verifyGoogleJavaFormat {
source = sourceSets*.allJava
include '**/*.java'
}
afterEvaluate {
jar {
inputs.property("moduleName", moduleName)

View File

@ -1,4 +0,0 @@
^/\*$
^ \* Copyright (The OpenTelemetry|\d\d\d\d(-\d\d)?, Guava) Authors$
^ \* SPDX-License-Identifier: Apache-2.0$
^ \*/$

View File

@ -20,12 +20,6 @@
<property name="severity" value="error"/>
<module name="RegexpHeader">
<property name="headerFile" value="${rootDir}/buildscripts/checkstyle.license"/>
<property name="fileExtensions" value="java"/>
</module>
<property name="fileExtensions" value="java, properties, xml"/>
<!-- Checks for whitespace -->
<!-- See http://checkstyle.sf.net/config_whitespace.html -->

View File

@ -1,3 +1,3 @@
#!/usr/bin/env sh
./gradlew googleJavaFormat
./gradlew spotlessApply

View File

@ -0,0 +1,5 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

View File

@ -1,6 +1,5 @@
plugins {
id 'com.github.sherter.google-java-format' apply false
id "com.jfrog.artifactory" apply false
id "com.diffplug.spotless"
}
subprojects {
@ -8,7 +7,7 @@ subprojects {
apply plugin: 'java'
apply plugin: 'java-library'
apply plugin: 'idea'
apply plugin: 'com.github.sherter.google-java-format'
apply plugin: 'com.diffplug.spotless'
group = "io.opentelemetry"
version = "0.1.0-SNAPSHOT"
@ -29,10 +28,11 @@ subprojects {
}
}
googleJavaFormat {
toolVersion = '1.7'
spotless {
java {
googleJavaFormat("1.7")
}
}
compileJava.dependsOn(verifyGoogleJavaFormat)
//create a single Jar with all dependencies
task fatJar(type: Jar) {

View File

@ -1,7 +1,6 @@
pluginManagement {
plugins {
id 'com.github.sherter.google-java-format' version "0.8"
id "com.jfrog.artifactory" version "4.13.0"
id "com.diffplug.spotless" version "5.6.1"
id 'com.google.protobuf' version '0.8.8'
}

View File

@ -1,3 +1,9 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
// Includes work from:
/*
* Copyright 2010, Guava Authors
* SPDX-License-Identifier: Apache-2.0

View File

@ -1,8 +1,8 @@
pluginManagement {
plugins {
id "com.diffplug.spotless" version "5.6.1"
id "com.github.ben-manes.versions" version "0.20.0"
id "com.github.kt3k.coveralls" version "2.0.1"
id 'com.github.sherter.google-java-format' version "0.8"
id "com.google.protobuf" version "0.8.8"
id "com.jfrog.artifactory" version "4.13.0"
id "io.morethan.jmhreport" version "0.9.0"