首页 > 代码库 > sonarqube插件开发(三) 调试插件

sonarqube插件开发(三) 调试插件

 环境

windows + eclipse +sonarqube server 5.6.4

准备

新建一个maven项目,其中加入了一些插件所用的jar

<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</groupId>    <artifactId>sonarqube-debug</artifactId>    <version>5.6.4</version>    <packaging>jar</packaging>    <name>sonarqube-debug</name>    <url>http://maven.apache.org</url>    <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-server</artifactId>            <version>5.6.4</version>            <scope>provided</scope>        </dependency>        <dependency>            <groupId>commons-httpclient</groupId>            <artifactId>commons-httpclient</artifactId>            <version>3.1</version>            <exclusions>                <exclusion>                    <groupId>commons-logging</groupId>                    <artifactId>commons-logging</artifactId>                </exclusion>            </exclusions>        </dependency>        <dependency>            <groupId>com.google.code.findbugs</groupId>            <artifactId>jsr305</artifactId>            <version>3.0.0</version>            <scope>provided</scope>        </dependency>        <dependency>            <groupId>javax.servlet</groupId>            <artifactId>javax.servlet-api</artifactId>            <version>3.0.1</version>            <scope>provided</scope>        </dependency>        <dependency>            <groupId>com.google.code.gson</groupId>            <artifactId>gson</artifactId>            <version>2.3.1</version>        </dependency>        <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>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.12</version>            <scope>test</scope>        </dependency>        <dependency>            <groupId>org.mockito</groupId>            <artifactId>mockito-core</artifactId>            <version>1.10.19</version>            <scope>test</scope>            <exclusions>                <exclusion>                    <groupId>org.hamcrest</groupId>                    <artifactId>hamcrest-core</artifactId>                </exclusion>            </exclusions>        </dependency>        <dependency>            <groupId>org.assertj</groupId>            <artifactId>assertj-core</artifactId>            <version>2.2.0</version>            <scope>test</scope>        </dependency>        <dependency>            <groupId>com.squareup.okhttp</groupId>            <artifactId>mockwebserver</artifactId>            <version>2.7.5</version>            <scope>test</scope>        </dependency>        <dependency>            <groupId>javax.servlet</groupId>            <artifactId>javax.servlet-api</artifactId>            <version>3.0.1</version>            <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.auth.keystone.KeystoneFilterPlugin</pluginClass>                    <!-- advanced properties can be set here. See paragraph "Advanced Build                         Properties". -->                </configuration>            </plugin>        </plugins>    </build>    <repositories>        <repository>            <id>public</id>            <name>Public Repositories</name>            <url>http://maven.aliyun.com/nexus/content/groups/public</url>            <releases>                <enabled>true</enabled>            </releases>            <snapshots>                <enabled>true</enabled>            </snapshots>        </repository>    </repositories></project>

新建java文件,方便启动服务器和添加参数

package com.ice.sonar.debug;import org.sonar.application.App;public class SonarDebug {    public static void main(String[] args) throws Exception {        App.main(args);    }}

配置启动参数

1. 配置刚加的java为主程序

技术分享

2 在Arguments里添加以下参数,

        -Dsonar.log.console=true 第一行是将debug log输出到控制台,否则好多信息看不到        另外三个是数据库配置,方便数据查询

-Dsonar.log.console=true-Dsonar.jdbc.username=root-Dsonar.jdbc.password=123456-Dsonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8

3 修改sonarqube配置文件

        编辑配置文件conf/sonar.properties

        sonar.web.dev=true 是启动开发模式, 在这个模式下才可以debug,也可能是我的错觉
        sonar.web.javaAdditionalOpts 是web server插件调试,即后台@ServerSide 注解的类
        sonar.ce.javaAdditionalOpts     是Compute Engine插件调试,即后台@ComputeEngineSide注解的类
        注意红色的两个端口号,这个在eclipse中remote debug时会用到
        这两个建议debug哪一个,加哪一个配置   

sonar.web.dev=truesonar.web.javaAdditionalOpts=-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=8000sonar.ce.javaAdditionalOpts=-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=8001

4 远程调试配置

        配置时,需要注意端口号,要用上一步配置的,用哪个配那个,如果两个都用则配置2个

技术分享

调试插件

以debug方式启动刚才的java文件

启动过程中会看到以下信息,这时,根据端口号启动相应的remote java application(上边第4步,远程调试配置)

Listening for transport dt_socket at address: 8000

 

通过以上方式启动成功,然后就可以像本地代码一样debug了。

另外推荐以link的方式把插件加载到一开始新建的项目中,调试和修改比较方便。

sonarqube插件开发(三) 调试插件