Reference: http://younian.net.cn/article/143 and https://github.com/mrprince/sonar-p3c-pmd project made a package, throw it into sonar [version 6.5 (build 27846)] and it works fine .
Download address: sonar-pmd-plugin-2.7-SNAPSHOT.jar.zip
Another disadvantage is that the description files provided in the sonar-p3c-pmd project are not detailed enough, and there is no Chinese.
It is a bit tiring to redo according to the p3c-pmd, but it is good to have it, and the basic effect is not bad with the IDE plug-in.
The descriptions are all in: p3c/p3c-pmd/src/main/resources/messages.xml (PS: If you have time, you can write a program to transfer
sonar effect
Bottom) I
dea p3c protocol scan effect
。
。
。
。
。
。
Finally, copy the article [http://younian.net.cn/article/143]
Ready to make changes:
- Add POM dependencies:
<!-- https://mvnrepository.com/artifact/com.alibaba.p3c/p3c-pmd -->
<dependency>
<groupId>com.alibaba.p3c</groupId>
<artifactId>p3c-pmd</artifactId>
<version>1.3.0</version>
</dependency>
-
add pmd.properties
rule.pmd.CommentsMustBeJavadocFormatRule.name=CommentsMustBeJavadocFormatRule
-
add rules.xml
<rule key="CommentsMustBeJavadocFormatRule">
<priority>MAJOR</priority>
<configKey><![CDATA[rulesets/java/ali-comment.xml/CommentsMustBeJavadocFormatRule]]> </configKey>
</rule>
-
Add the pmd-model.xml file (the specific meaning is unknown)
<chc>
<rule-repo>p3c</rule-repo>
<rule-key>CommentsMustBeJavadocFormatRule</rule-key>
<prop>
<key>remediationFunction</key>
<txt>CONSTANT_ISSUE</txt>
</prop>
<prop>
<key>offset</key>
<val>2</val>
<txt>min</txt>
</prop>
</chc>
-
Add a description file - CommentsMustBeJavadocFormatRule.html. The content comes from the corresponding xml of p3c for the display of the error details page
<p>Look for qualified this usages in the same class.</p>
<p>Examples:</p>
<pre>
/**
*
* XXX class function description.
*
*/
public class XxClass implements Serializable {
private static final long serialVersionUID = 113323427779853001L;
/**
* id
*/
private Long id;
/**
* title
*/
private String title;
/**
* find by id
*
* @param ruleId rule id
* @param page start from 1
* @return Result<Xxxx>
*/
public Result<Xxxx> funcA(Long ruleId, Integer page) {
return null;
}
}
</pre>
For the time being, it is added on the original basis, and the existing part of the rule is not deleted.
test analysis:
-
Install sonarqube:
download sonarqube and decompress it, java8 is required, and a web server is required:
copy the jar package from the mvn package of the above project to sonarqube-6.5\extensions\plugins and
run sonarqube-6.5\bin\windows-x86-64\StartSonar. bat -
admin/admin login, a string similar to token will be generated
-
Enter Quality Profiles, create a new Quality Profile and save it. Enter the newly created Quality Profiles,
click ActiveMore on the left, enter rules, find the rule you just added, there will be an active button on the right side of the list, and you can activate it.
(The following is the official Usage)Usage
In the quality profile, activate some rules from PMD and run an analysis on your project. Set the sonar.java.source property to tell PMD which version of Java your source code complies to. The default value is 1.5. Possible values: 1.4, 1.5 or 5, 1.6 or 6, 1.7 or 7. Since version 2.2 of the plugin, this property can also be set to 1.8 or 8.
-
Analysis
Enter the project folder to start analysis: Dsonar.login is the token aboveD:\GreenPro\apache-maven-3.3.9\bin\
mvn sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=8f59dbbe29d160a9727038333d33acf31e11caea
-
After switching Profiles
for the first time, there will be a new project in the projects list. At this time, you can go to the project details,Administratoration->Quality Profiles
, switch Profile -
Then run and analyze.