【每日鲜蘑】SpringBoot之Maven常用插件

1,578 阅读1分钟

SpringBoot项目比较简单,一般涉及到的插件不外乎“打包”、“编译”、“测试”、“编码规范检查”。

SpringBoot打包插件

mainClass 指明启动的main方法的类 addResources 指明是否添加 resource

<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                        <goal>build-info</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>com.jiaomatech.eurake.EurakeApplication</mainClass>
                <addResources>true</addResources>
            </configuration>
        </plugin>
    </plugins>
</build>

编译插件

maven-compiler-plugin 用来编译Java代码, maven-resources-plugin 用于处理资源文件, versions-maven-plugin 用来管理版本

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <inherited>true</inherited>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <parameters>true</parameters>
    </configuration>
</plugin>

执行测试的插件

测试类遵循通用的命令约定(以Test结尾、以TestCase结尾、或者以Test开头)

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${maven-surefire-plugin.version}</version>
    <inherited>true</inherited>
    <configuration>
        <forkCount>1</forkCount>
        <reuseForks>false</reuseForks>
    </configuration>
</plugin>

阿里巴巴代码检测

maven-pmd-plugin 阿里巴巴提供的插件

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-pmd-plugin</artifactId>
    <version>3.8</version>
    <configuration>
        <rulesets>
            <ruleset>rulesets/java/ali-comment.xml</ruleset>
            <ruleset>rulesets/java/ali-concurrent.xml</ruleset>
            <ruleset>rulesets/java/ali-constant.xml</ruleset>
            <ruleset>rulesets/java/ali-exception.xml</ruleset>
            <ruleset>rulesets/java/ali-flowcontrol.xml</ruleset>
            <ruleset>rulesets/java/ali-naming.xml</ruleset>
            <ruleset>rulesets/java/ali-oop.xml</ruleset>
            <ruleset>rulesets/java/ali-orm.xml</ruleset>
            <ruleset>rulesets/java/ali-other.xml</ruleset>
            <ruleset>rulesets/java/ali-set.xml</ruleset>
        </rulesets>
        <printFailingErrors>true</printFailingErrors>
        <excludeRoots>
            <excludeRoot>target/generated-sources/annotations</excludeRoot>
        </excludeRoots>
    </configuration>
    <executions>
        <execution>
            <phase>verify</phase>
            <goals>
                <goal>check</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>com.alibaba.p3c</groupId>
            <artifactId>p3c-pmd</artifactId>
            <version>1.3.0</version>
        </dependency>
    </dependencies>
</plugin>