Commit e75d8eaf authored by Phillip Webb's avatar Phillip Webb

Switch to spring-javaformat plugin

Use `spring-javaformat` to format and check code. Code formatting can
now be applied using the `spring-javaformat-maven-plugin` from the
command line. Existing checkstyle rules have also been replaced and
the CONTRIBUTING.adoc file has been updated.

Closes gh-13255
parent e69296d7
...@@ -47,12 +47,14 @@ given the ability to merge pull requests. ...@@ -47,12 +47,14 @@ given the ability to merge pull requests.
None of these is essential for a pull request, but they will all help. They can also be None of these is essential for a pull request, but they will all help. They can also be
added after the original pull request but before a merge. added after the original pull request but before a merge.
* Use the Spring Framework code format conventions. If you use Eclipse and you follow * We use the https://github.com/spring-io/spring-javaformat/[Spring JavaFormat] project
the '`Importing into eclipse`' instructions below you should get project specific to apply code formatting conventions. If you use Eclipse and you follow the '`Importing
formatting automatically. You can also import formatter settings using the into eclipse`' instructions below you should get project specific formatting
`eclipse-code-formatter.xml` file from the `eclipse` folder. If using IntelliJ IDEA, you automatically. You can also install the https://github.com/spring-io/spring-javaformat/#intellij-idea[Spring JavaFormat IntelliJ Plugin]
can use the http://plugins.jetbrains.com/plugin/6546[Eclipse Code Formatter Plugin] or format the code from the Maven build by running
to import the same file. `./mvnw io.spring.javaformat:spring-javaformat-maven-plugin:apply`.
* The build includes checkstyle rules for many of our code conventions. Run
`./mvnw validate` if you want to check you changes are compliant.
* Make sure all new `.java` files to have a simple Javadoc class comment with at least an * Make sure all new `.java` files to have a simple Javadoc class comment with at least an
`@author` tag identifying you, and preferably at least a paragraph on what the class is `@author` tag identifying you, and preferably at least a paragraph on what the class is
for. for.
...@@ -80,13 +82,15 @@ should also work without issue. ...@@ -80,13 +82,15 @@ should also work without issue.
=== Building from Source === Building from Source
To build the source you will need to install Spring Boot source can be build from the command line using
http://maven.apache.org/run-maven/index.html[Apache Maven] v3.2.3 or above and JDK 1.8. http://maven.apache.org/run-maven/index.html[Apache Maven] on JDK 1.8 or above.
We include '`Maven Wrapper`' scripts (`./mvnw` or `mvnw.bat`) that you can run rather
than needing to install Maven locally.
==== Default Build ==== Default Build
The project can be built from the root directory using the standard maven command: The project can be built from the root directory using the standard Maven command:
[indent=0] [indent=0]
---- ----
...@@ -151,9 +155,9 @@ to Maven by setting a `MAVEN_OPTS` environment variable with the value ...@@ -151,9 +155,9 @@ to Maven by setting a `MAVEN_OPTS` environment variable with the value
=== Importing into Eclipse === Importing into Eclipse
You can import the Spring Boot code into any Eclipse Mars based distribution. The easiest You can import the Spring Boot code into any Eclipse Oxygen based distribution. The easiest
way to setup a new environment is to use the Eclipse Installer with the provided way to setup a new environment is to use the Eclipse Installer with the provided
`.setup` file. `.setup` file (in the `/eclipse` folder).
==== Using the Eclipse Installer ==== Using the Eclipse Installer
...@@ -182,24 +186,21 @@ easier to navigate. ...@@ -182,24 +186,21 @@ easier to navigate.
==== Manual Installation with M2Eclipse ==== Manual Installation with M2Eclipse
If you prefer to install Eclipse yourself we recommend that you use the If you prefer to install Eclipse yourself you should use the
http://eclipse.org/m2e/[M2Eclipse] eclipse plugin. If you don't already have m2eclipse http://eclipse.org/m2e/[M2Eclipse] eclipse plugin. If you don't already have m2eclipse
installed it is available from the "Eclipse marketplace". installed it is available from the "Eclipse marketplace".
Spring Boot includes project specific source formatting settings, in order to have these Spring Boot includes project specific source formatting settings, in order to have these
work with m2eclipse, we provide additional Eclipse plugins that you can install: work with m2eclipse, we provide an additional Eclipse plugin that you can install:
===== Install the m2eclipse-maveneclipse plugin
* Select "`Help`" -> "`Install New Software`".
* Add `https://dl.bintray.com/philwebb/m2eclipse-maveneclipse` as a site.
* Install "Maven Integration for the maven-eclipse-plugin"
===== Install the Spring Formatter plugin ===== Install the Spring Formatter plugin
* Select "`Help`" -> "`Install New Software`". * Select "`Help`" -> "`Install New Software`".
* Add `https://dl.bintray.com/philwebb/spring-eclipse-code-formatter/` as a site. * Add `https://dl.bintray.com/spring/javaformat-eclipse/` as a site.
* Install "Spring Code Formatter" * Install "Spring Java Format"
NOTE: These plugins are optional. Projects can be imported without the plugins, your code NOTE: The plugin is optional. Projects can be imported without the plugins, your code
changes just won't be automatically formatted. changes just won't be automatically formatted.
With the requisite eclipse plugins installed you can select With the requisite eclipse plugins installed you can select
...@@ -208,20 +209,6 @@ need to import the root `spring-boot` pom and the `spring-boot-samples` pom sepa ...@@ -208,20 +209,6 @@ need to import the root `spring-boot` pom and the `spring-boot-samples` pom sepa
==== Importing into Eclipse without M2Eclipse
If you prefer not to use m2eclipse you can generate eclipse project metadata using the
following command:
[indent=0]
----
$ ./mvnw eclipse:eclipse
----
The generated eclipse projects can be imported by selecting `import existing projects`
from the `file` menu.
=== Importing into Other IDEs === Importing into Other IDEs
Maven is well supported by most Java IDEs. Refer to your vendor documentation. Maven is well supported by most Java IDEs. Refer to your vendor documentation.
...@@ -229,7 +216,7 @@ Maven is well supported by most Java IDEs. Refer to your vendor documentation. ...@@ -229,7 +216,7 @@ Maven is well supported by most Java IDEs. Refer to your vendor documentation.
== Integration Tests == Integration Tests
The sample applications are used as integration tests during the build (when you The sample applications are used as integration tests during the build (when you
`mvn install`). Due to the fact that they make use of the `spring-boot-maven-plugin` `./mvnw install`). Due to the fact that they make use of the `spring-boot-maven-plugin`
they cannot be called directly, and so instead are launched via the they cannot be called directly, and so instead are launched via the
`maven-invoker-plugin`. If you encounter build failures running the integration tests, `maven-invoker-plugin`. If you encounter build failures running the integration tests,
check the `build.log` file in the appropriate sample directory. check the `build.log` file in the appropriate sample directory.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -63,7 +63,7 @@ ...@@ -63,7 +63,7 @@
<requirement <requirement
name="AnyEditTools.feature.group"/> name="AnyEditTools.feature.group"/>
<requirement <requirement
name="org.eclipse.m2e.maveneclipse.feature.feature.group"/> name="io.spring.javaformat.eclipse.feature.feature.group"/>
<requirement <requirement
name="org.eclipse.jst.server_adapters.feature.feature.group"/> name="org.eclipse.jst.server_adapters.feature.feature.group"/>
<requirement <requirement
...@@ -85,7 +85,7 @@ ...@@ -85,7 +85,7 @@
<repository <repository
url="http://andrei.gmxhome.de/eclipse/"/> url="http://andrei.gmxhome.de/eclipse/"/>
<repository <repository
url="https://dl.bintray.com/philwebb/m2eclipse-maveneclipse"/> url="https://dl.bintray.com/spring/javaformat-eclipse/"/>
<repository <repository
url="http://download.eclipse.org/egit/github/updates/"/> url="http://download.eclipse.org/egit/github/updates/"/>
<repository <repository
......
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
...@@ -24,6 +26,7 @@ ...@@ -24,6 +26,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.version>3.1.1</maven.version> <maven.version>3.1.1</maven.version>
<spring-javaformat.version>0.0.1</spring-javaformat.version>
</properties> </properties>
<scm> <scm>
<url>http://github.com/spring-projects/spring-boot</url> <url>http://github.com/spring-projects/spring-boot</url>
...@@ -300,12 +303,17 @@ ...@@ -300,12 +303,17 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId> <artifactId>maven-checkstyle-plugin</artifactId>
<version>2.17</version> <version>3.0.0</version>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.puppycrawl.tools</groupId> <groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId> <artifactId>checkstyle</artifactId>
<version>8.5</version> <version>8.8</version>
</dependency>
<dependency>
<groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-checkstyle</artifactId>
<version>${spring-javaformat.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>
</plugin> </plugin>
...@@ -494,6 +502,19 @@ ...@@ -494,6 +502,19 @@
</plugins> </plugins>
</pluginManagement> </pluginManagement>
<plugins> <plugins>
<plugin>
<groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-maven-plugin</artifactId>
<version>${spring-javaformat.version}</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>validate</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId> <artifactId>maven-checkstyle-plugin</artifactId>
...@@ -502,14 +523,8 @@ ...@@ -502,14 +523,8 @@
<id>checkstyle-validation</id> <id>checkstyle-validation</id>
<phase>validate</phase> <phase>validate</phase>
<configuration> <configuration>
<skip>${disable.checks}</skip>
<configLocation>src/checkstyle/checkstyle.xml</configLocation> <configLocation>src/checkstyle/checkstyle.xml</configLocation>
<suppressionsLocation>src/checkstyle/checkstyle-suppressions.xml</suppressionsLocation> <suppressionsLocation>src/checkstyle/checkstyle-suppressions.xml</suppressionsLocation>
<headerLocation>src/checkstyle/checkstyle-header.txt</headerLocation>
<propertyExpansion>checkstyle.build.directory=${project.build.directory}</propertyExpansion>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failOnViolation>true</failOnViolation>
<includeTestSourceDirectory>true</includeTestSourceDirectory> <includeTestSourceDirectory>true</includeTestSourceDirectory>
</configuration> </configuration>
<goals> <goals>
...@@ -526,23 +541,6 @@ ...@@ -526,23 +541,6 @@
<target>${java.version}</target> <target>${java.version}</target>
</configuration> </configuration>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<useProjectReferences>false</useProjectReferences>
<additionalConfig>
<file>
<name>.settings/org.eclipse.jdt.ui.prefs</name>
<location>${main.basedir}/eclipse/org.eclipse.jdt.ui.prefs</location>
</file>
<file>
<name>.settings/org.eclipse.jdt.core.prefs</name>
<location>${main.basedir}/eclipse/org.eclipse.jdt.core.prefs</location>
</file>
</additionalConfig>
</configuration>
</plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId> <artifactId>maven-enforcer-plugin</artifactId>
......
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
...@@ -19,6 +21,7 @@ ...@@ -19,6 +21,7 @@
<properties> <properties>
<main.basedir>${basedir}/..</main.basedir> <main.basedir>${basedir}/..</main.basedir>
<java.version>1.8</java.version> <java.version>1.8</java.version>
<spring-javaformat.version>0.0.1</spring-javaformat.version>
</properties> </properties>
<modules> <modules>
<module>spring-boot-sample-ant</module> <module>spring-boot-sample-ant</module>
...@@ -122,22 +125,49 @@ ...@@ -122,22 +125,49 @@
parent --> parent -->
<build> <build>
<plugins> <plugins>
<plugin>
<groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-maven-plugin</artifactId>
<version>${spring-javaformat.version}</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>validate</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId> <artifactId>maven-checkstyle-plugin</artifactId>
<configuration> <version>3.0.0</version>
<useProjectReferences>false</useProjectReferences> <dependencies>
<additionalConfig> <dependency>
<file> <groupId>com.puppycrawl.tools</groupId>
<name>.settings/org.eclipse.jdt.ui.prefs</name> <artifactId>checkstyle</artifactId>
<location>${main.basedir}/eclipse/org.eclipse.jdt.ui.prefs</location> <version>8.8</version>
</file> </dependency>
<file> <dependency>
<name>.settings/org.eclipse.jdt.core.prefs</name> <groupId>io.spring.javaformat</groupId>
<location>${main.basedir}/eclipse/org.eclipse.jdt.core.prefs</location> <artifactId>spring-javaformat-checkstyle</artifactId>
</file> <version>${spring-javaformat.version}</version>
</additionalConfig> </dependency>
</configuration> </dependencies>
<executions>
<execution>
<id>checkstyle-validation</id>
<phase>validate</phase>
<configuration>
<configLocation>../spring-boot-parent/src/checkstyle/checkstyle.xml</configLocation>
<suppressionsLocation>../spring-boot-parent/src/checkstyle/checkstyle-suppressions.xml</suppressionsLocation>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
</configuration>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
...@@ -181,7 +211,8 @@ ...@@ -181,7 +211,8 @@
</plugins> </plugins>
<pluginManagement> <pluginManagement>
<plugins> <plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.--> <!--This plugin's configuration is used to store Eclipse m2e settings
only. It has no influence on the Maven build itself. -->
<plugin> <plugin>
<groupId>org.eclipse.m2e</groupId> <groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId> <artifactId>lifecycle-mapping</artifactId>
...@@ -227,6 +258,25 @@ ...@@ -227,6 +258,25 @@
<ignore /> <ignore />
</action> </action>
</pluginExecution> </pluginExecution>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-checkstyle-plugin
</artifactId>
<versionRange>
[3.0.0,)
</versionRange>
<goals>
<goal>check</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions> </pluginExecutions>
</lifecycleMappingMetadata> </lifecycleMappingMetadata>
</configuration> </configuration>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment