一、环境准备
java 1.8, maven 3.1
检查自己的环境是否支持 sonarqube的插件开发
java -version
mvn -version
二、创建maven项目
pom.xml 文件如下:
<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>com.ice.sonar.plugin</groupId> <artifactId>sonar-sample-plugin</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>sonar-plugin</packaging> <description>Sample Plugin for SonarQube</description> <inceptionYear>2017</inceptionYear> <name>sonar-sample-plugin</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.sonarsource.sonarqube</groupId> <artifactId>sonar-plugin-api</artifactId> <!-- minimal version of SonarQube to support. Note that the groupId was "org.codehaus.sonar" before version 5.2 --> <version>5.6</version> <!-- mandatory scope --> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <encoding>${project.build.sourceEncoding}</encoding> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <plugin> <groupId>org.sonarsource.sonar-packaging-maven-plugin</groupId> <artifactId>sonar-packaging-maven-plugin</artifactId> <version>1.17</version> <extensions>true</extensions> <configuration> <!-- the entry-point class that extends org.sonar.api.SonarPlugin --> <pluginClass>com.ice.sonar.plugin.SamplePlugin</pluginClass> <!-- advanced properties can be set here. See paragraph "Advanced Build Properties". --> </configuration> </plugin> </plugins> </build> </project>
修改完pom文件后需要 执行 maven->update project
创建SamplePlugin.java文件如下:
package com.ice.sonar.plugin; import org.sonar.api.Plugin; public class SamplePlugin implements Plugin { @Override public void define(Context context) { System.out.println("SamplePlugin is defined"); } }
三、构建
mvn clean package
四、发布
“冷”发布
将构建好的jar复制到 extensions/plugins/ 目录下
重启sonarqube服务
“热”发布
修改配置文件conf/sonar.properties
# 在配置文件中添加剂 sonar.web.dev=true
重启服务
在构建jar包是使用如下参数
mvn package org.codehaus.sonar:sonar-dev-maven-plugin::upload -DsonarHome=/path/to/server/home -DsonarUrl=http://localhost:8080
参考sonarqube官网 Build Plugin