maven-pmd-plugin集成p3c规范

岳出野
2023-12-01

感觉用的人很少一样,都没什么参考资料,但确实对于小公司来说,是一个养成代码规范习惯的好工具,特记录下来
只需在父级依赖中配置以下插件代码,即可实现代码检查
maven方式:

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>role_demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>
    <!-- 属性配置 -->
    <build>
        <plugins>

            <!-- PMD插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>3.8</version>
                <configuration>
                    <sourceEncoding>${project.build.sourceEncoding}</sourceEncoding>
                    <targetJdk>${java.version}</targetJdk>
                    <printFailingErrors>true</printFailingErrors>
                    <!-- 代码检查规则,用不着全部,按需 -->
                    <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>
                </configuration>
                <executions>
                    <!--绑定pmd:check到verify生命周期-->
                    <execution>
                        <id>pmd-check-package</id>
                        <phase>package</phase>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                    <!-- 绑定pmd:check到verify生命周期 -->
                    <execution>
                        <id>pmd-check-verify</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                    <!-- 绑定pmd:pmd到site生命周期 -->
                    <execution>
                        <id>pmd-pmd-site</id>
                        <phase>site</phase>
                        <goals>
                            <goal>pmd</goal>
                        </goals>
                    </execution>
                </executions>
                <!-- p3c依赖 -->
                <dependencies>
                    <dependency>
                        <groupId>com.alibaba.p3c</groupId>
                        <artifactId>p3c-pmd</artifactId>
                        <version>1.3.5</version>
                    </dependency>
                </dependencies>
            </plugin>
            <!-- 编译插件 Spring-Boot 项目不需要设置,因为parent中已设置 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.7.1</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-project-info-reports-plugin</artifactId>
                <version>3.0.0</version>
            </plugin>
        </plugins>
    </build>

    <!--    &lt;!&ndash; 用于生成错误到代码内容的链接 &ndash;&gt;-->
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jxr-plugin</artifactId>
                <version>2.3</version>
            </plugin>
        </plugins>
    </reporting>
</project>

gradle方式:

//统一 Spring Cloud/Alibaba Cloud 依赖以及代码规范
subprojects {
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'java'
    apply plugin: "com.diffplug.gradle.spotless"
    apply plugin: 'pmd'
    apply plugin: 'docker'


    project.afterEvaluate {
        if (project.plugins.hasPlugin('java-library')) {
            jar.enabled = true
            bootJar.mainClassName = ""
        }
        if (project.plugins.hasPlugin('maven-publish')) {
            //打包源码
            task sourceJar(type: Jar) {
                from sourceSets.main.allJava
            }
            publishing {
                publications {
                    maven(MavenPublication) {
//                        artifactId "${project.name.replaceAll("jz-cloud-","").replaceAll("-",".")}"
                        //如果是war包填写components.web,如果是jar包填写components.java
                        from components.java

                        //配置上传源码
                        artifact sourceJar {
                            classifier "src"
                        }

                    }
                }
                repositories {
                    maven {
                        def releasesRepoUrl = RELEASE_URL
                        def snapshotsRepoUrl = SNAPSHOT_URL
                        //指定要上传的maven私服仓库
                        url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
                        //认证用户和密码
                        credentials {
                            username = MAVEN_USERNAME
                            password = MAVEN_PASSWORD
                        }
                    }
                }
            }
        }

        def bootTasks = project.getTasksByName("bootJar", false);
        if (bootTasks.size() > 0) {
            if (project.subprojects.size() != 0) {
                println "bootJar task skiped in ${project.name} with subprojects"
            }
            bootTasks[0].onlyIf { project.subprojects.size() == 0 }
        }

        def dockerTasks = project.getTasksByName("dockerBuilder", false);
        if (dockerTasks.size() > 0) {
            dockerTasks[0].shouldRunAfter(compileJava)
            dockerTasks[0].onlyIf { project.subprojects.size() == 0 }
            if (project.subprojects.size() != 0) {
                dockerTasks[0].enabled(false)
                println "dockerBuilder task skiped in ${project.name} with subprojects"
            }
            if (project.plugins.hasPlugin("java-library")) {
                dockerTasks[0].enabled(false)
                println "dockerBuilder task skiped in ${project.name} with library project"
            }
        }

        def dockerDistTasks = project.getTasksByName("distDocker", false);
        if (dockerDistTasks.size() > 0) {
            dockerDistTasks[0].onlyIf { project.subprojects.size() == 0 }
            if (project.subprojects.size() != 0) {
                dockerDistTasks[0].enabled(false)
                println "distDocker task skiped in ${project.name} with subprojects"
            }
            if (project.plugins.hasPlugin("java-library")) {
                dockerDistTasks[0].enabled(false)
                println "distDocker task skiped in ${project.name} with library project"
            }
        }


    }

    group = GROUP_ID
    version = SERVICE_VERSION

    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    dependencyManagement {
        imports {
            mavenBom "com.sgcc.jz.cloud:spring-alibaba-cloud-dependencies:2.1.0.RELEASE"
        }

    }


    dependencies {
        compileOnly 'org.projectlombok:lombok:1.18.10'
        annotationProcessor 'org.projectlombok:lombok:1.18.10'
        compile "net.logstash.logback:logstash-logback-encoder"
        compile 'org.springframework.boot:spring-boot-starter-logging'
        pmd "com.alibaba.p3c:p3c-pmd:2.1.0"
    }

    pmd {
        consoleOutput = true
        reportsDir = file("build/reports/pmd")

        //阿里巴巴编码规范引入
        ruleSets = [
//               "java-ali-naming",
"java-ali-comment",
"java-ali-exception",
"java-ali-constant",
"java-ali-flowcontrol",
"java-ali-oop"
        ]
    }


    spotless {
        java {
            googleJavaFormat()
            indentWithTabs(2)
            indentWithSpaces(4)
            target fileTree('.') {
                include '**/*.java'
                exclude '**/mybatis/**'
                exclude '**/objconver/**'
            }
            licenseHeaderFile "${rootDir}/cnksi.license"
        }
    }

    /* 排除 jackson
     configurations {
         all*.exclude group:"com.fasterxml.jackson.core"
     }*/

    //docker 插件
    docker {
        maintainer = 'cnksi.com'
        baseImage = "${REMOTE_REGISTRY}/jzcloud/java_8_skywaling_agent:8.1.0"
    }

    task dockerBuilder(type: Docker) {
        group 'docker'
        applicationName = jar.baseName
        tagVersion = jar.version
        tag = REMOTE_REGISTRY + '/' + project.group + '/' + jar.baseName
        setEnvironment("PARAMS", "\"\"")
        setEnvironment("SERVIVE_NAME", "\"${jar.baseName}\"")
        setEnvironment("JAR_FILE", "\"${jar.baseName}-${jar.version}.jar\"")
        addFile("${jar.baseName}-${jar.version}.jar", "${jar.baseName}-${jar.version}.jar")
//        entryPoint(["sh","-c","java -Djava.security.egd=file:/dev/./urandom -jar " + jar.baseName+"-"+jar.version+".jar" + ' --server.port=8080 $PARAMS'])
        exposePort(8080)
        push = true
        doFirst {
            copy {
                from jar
                into stageDir
            }
        }
    }
    dockerBuilder.dependsOn(build)
}
 类似资料: