1. The configuration

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>
Copy the code

2. Specify the JDK version of the project

When building projects using Maven, it is recommended that you specify a JDK version to build. First, because Maven’s default build version of the JDK is generally low, the current default is 1.5, which is used by default, rather than the JDK version specified in your project. Second, the project code is usually compiled and packaged by the packer, then deployed to the test environment and finally deployed to production, and we are not sure what the default Maven compiler version of the JDK is.

<build>
    <pluginManagement>
	<plugins>
            <plugin>
                <! -- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-compiler-plugin</artifactId>
                <! As of this writing, the latest version is 3.8.1. You can use the default value or use the default value, but it is not recommended. -->
                <version>3.8.1</version>
		<configuration>
                    <! -- JDK version used for source code -->
                    <source>${java.version}</source>
                    <! -- The compiled version of the target class file to be generated -->
                    <target>${java.version}</target>
                    <! -- Character set encoding -->
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <compilerVersion>${java.version}</compilerVersion>
                    <! -- Skip test -->
                    <skipTests>true</skipTests>
                    <showWarnings>true</showWarnings>
                    <! -- Initial memory used by the compiler -->
                    <meminitial>128m</meminitial>
                    <! -- Maximum memory used by compiler -->
                    <maxmem>512m</maxmem>
		</configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
Copy the code

The above configuration method is only applicable to a project. In some cases, you can configure the default compiled JDK version of Maven if you don’t want to repeat the configuration. Go to the conf/settings. XML file in the Maven installation directory and add the configuration under the TAB:

<profile>
    <id>JDK - 1.8 -</id>
    <activation>
        <activeByDefault>true</activeByDefault>
        <jdk>1.8</jdk>
    </activation>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
    </properties>
</profile>
Copy the code

3. Add the Checkstyle check

Everyone on the team writes code in a very different style, and in order for everyone to submit code in a uniform style, it is not enough to talk about uniformity, it is necessary to use tools. Checkstyle is one such tool that does code formatting checks. It also provides the Maven-checkstyle-plugin, which is integrated into our Maven project and automatically does code style checks during build execution to ensure that the exported code complies with the code specification if there is a direct build failure that is not standardized.

3.1 Preparing a specification configuration file

Checkstyle can configure many rules, some of which are necessary and some of which I feel are too strict. Specific what meaning each rule can be reference: checkstyle.org/checks.html…


      
<! DOCTYPEmodule PUBLIC "- / / Checkstyle / / DTD Checkstyle Configuration / 1.3 / EN" "https://checkstyle.org/dtds/configuration_1_3.dtd">
<! -- All the following modules specify the name, without adding the property configuration, select the default configuration parameters on the official website. -->
<module name="Checker">
    <! -- Character set encoding -->
    <property name="charset" value="UTF-8"/>
    <! -- Violation level -->
    <property name="severity" value="error"/>
    <! -- Check the file extension -->
    <property name="fileExtensions" value="java, properties, xml"/>

    <! -- Does the file contain tabs -->
    <module name="FileTabCharacter">
        <property name="eachLine" value="true"/>
    </module>

    <! -- Length -->
    <module name="LineLength">
        <property name="fileExtensions" value="java"/>
        <property name="max" value="200"/>
        <property name="ignorePattern" value="^implements.*|^extends.*|^package.*|^import.*|a href|href|http://|https://|ftp://"/>
    </module>

    <! -- Property file has the same key -->
    <module name="Translation"/>

    <! Java source files and define some properties suitable for checking such files.
    <module name="TreeWalker">
        <! Comment a method. You must comment every method. You can comment the parameters in the method.
   	<module name="JavadocMethod">
        <! Public can check only public modified methods. Private can check all methods.
        <property name="scope" value="puplic"/>
        <property name="allowUndeclaredRTE" value="true"/>
        <! -- Ignore the check for parameter comments -->
        <property name="allowMissingParamTags" value="true"/>
        <property name="allowMissingJavadoc" value="false"/>
        <! Throws throws throws comment check -->
        <property name="allowMissingThrowsTags" value="false"/>
        <! -- Ignore check for return comment -->
        <property name="allowMissingReturnTag" value="false"/>
        <! Allow get/set methods without comments -->
        <property name="allowMissingPropertyJavadoc" value="true"/>
        </module>
        <! -- External type name and filename match -->
        <module name="OuterTypeFilename"/>
        <! -- Package name naming convention -->
        <module name="PackageName" />
        <! Naming conventions for class names -->
        <module name="TypeName"/>
        <module name="MemberName"/>
        <module name="ParameterName"/>
        <module name="LambdaParameterName"/>
        <module name="CatchParameterName"/>
        <module name="LocalVariableName"/>
        <module name="ClassTypeParameterName"/>
        <module name="MethodTypeParameterName"/>
        <module name="InterfaceTypeParameterName"/>
        <! Naming conventions for method names -->
        <module name="MethodName"/>
        <! Naming conventions for constants -->
        <module name="ConstantName"/>
        <! Static variable naming convention -->
        <module name="StaticVariableName"/>
        <! -- The full path of the class must be imported, i.e. the required class cannot be imported with * --> 
        <module name="AvoidStarImport"/>
        <! -- Check whether the imported package is not used -->
        <module name="UnusedImports"/>
        <! -- Check whether extra packages are imported -->
        <module name="RedundantImport"/>
        <! -- Modifier check -->
        <! -- Check that the order of modifiers follows the Java language specification, Default public, protected, private, abstract, static, final, transient, volatile, synchronized, native, strictfp -->
        <module name="ModifierOrder"/>
        <! -- Check for extra modifiers in interfaces and annotations, such as interface methods that don't need to use public -->
        <module name="RedundantModifier"/>
        <! -- Whether there are nested code blocks -->
        <module name="AvoidNestedBlocks"/>
        <! -- Is there an empty code block -->
        <module name="EmptyBlock"/>
        <! -- Is the code block missing {} -->
        <module name="NeedBraces"/>
        <! -- Open brace position -->
        <module name="LeftCurly"/>
        <! -- Close curly bracket position -->
        <module name="RightCurly"/>
        <! Check the style of array type definition -->
        <module name="ArrayTypeStyle" />
        <! -- Check whether switch statement has "default" clause -->
        <module name="MissingSwitchDefault" />
        <! Return, break, throw, continue -->
        <module name="FallThrough" />
        <! -- Check if long definition has uppercase "L" -->
        <module name="UpperEll" />
        <module name="EmptyForInitializerPad"/>
        <module name="EmptyForIteratorPad"/>
        <module name="EmptyLineSeparator">
            <property name="allowNoEmptyLineBetweenFields" value="true" />
        </module>
        <module name="GenericWhitespace"/>
        <module name="MethodParamPad"/>
        <module name="NoLineWrap"/>
        <module name="OperatorWrap"/>
        <module name="ParenPad"/>
        <module name="SingleSpaceSeparator"/>
        <module name="TypecastParenPad"/>
        <module name="WhitespaceAfter"/>
        <module name="WhitespaceAround"/>
    </module>
</module>
Copy the code

Save the configuration file to a path in the project, such as: style/checkstyle.xml

3.2 Adding a Maven plug-in

<build>
    <plugins>
        <plugin>
            <! -- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-checkstyle-plugin -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>x.y.z</version>
            <configuration>
                <! -- Specifies the relative path to the rule file that the code specification checks -->
                <configLocation>style/checkStyle.xml</configLocation>
                <includeTestSourceDirectory>false</includeTestSourceDirectory>
                <encoding>${project.build.sourceEncoding}</encoding>
                <! Check whether the result is output on the console -->
                <consoleOutput>true</consoleOutput>
                <failsOnError>true</failsOnError>
            </configuration>
            <executions>
                <execution>
                    <id>validate</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>check</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Copy the code