sonarqube环境搭建

知识点

  • 掌握代码左移的基本技术
  • 掌握代码分析的基本技术
  • 了解代码度量的基本指标

环境搭建

临时部署

docker run -d \
    --name sonarqube_hogwarts_dev \
    -p 9000:9000 -p 9092:9092 \
    sonarqube

正式部署

db_instance=postgres_hogwarts_prod
sonarqube_instance=sonarqube_hogwarts_prod
data_dir=/root/sonarqube
mkdir -p $data_dir

#创建网络
docker network create sonarqube
#启动pg数据库
docker run -d \
    --name $db_instance \
    --network sonarqube \
    -p 5432:5432 \
    -e POSTGRES_USER=sonarqube \
    -e POSTGRES_PASSWORD=sonarqube \
    -e PGDATA=/var/lib/postgresql/data/pgdata \
    -v $data_dir/postgresql:/var/lib/postgresql/data \
    postgres

#创建数据目录
mkdir $data_dir/sonarqube_data
mkdir $data_dir/sonarqube_extensions
mkdir $data_dir/sonarqube_logs
chown -R 999:999 $data_dir


sysctl -w vm.max_map_count=524288
sysctl -w fs.file-max=131072
ulimit -n 131072
ulimit -u 8192

#启动soarqube的产品部署
docker run -d \
    --name $sonarqube_instance \
    --network sonarqube \
    -p 9000:9000 -p 9092:9092 \
    -e SONARQUBE_JDBC_USERNAME=sonarqube \
    -e SONARQUBE_JDBC_PASSWORD=sonarqube \
    -e SONARQUBE_JDBC_URL="jdbc:postgresql://$db_instance/sonarqube" \
    -v $data_dir/sonarqube_data:/opt/sonarqube/data \
    -v $data_dir/sonarqube_extensions:/opt/sonarqube/extensions \
    -v $data_dir/sonarqube_logs:/opt/sonarqube/logs \
    sonarqube

scanner

https://docs.sonarqube.org/latest/analysis/languages/java/

scanner支持三种配置风格

  • scanner命令行参数风格
  • sonar-project.properties
  • 构建工具插件 maven gradle
sonar-scanner \
    -Dsonar.host.url=http://sonarqube.ceshiren.com:9000 \
    -Dsonar.login=$SONARQUBE_TOKEN

sonar-scanner \
    -Dsonar.host.url=http://sonarqube.ceshiren.com:9000 \
    -Dsonar.login=$SONARQUBE_TOKEN \
    -Dsonar.projectKey=litemall-core-seveniruby \
    -Dsonar.sources=litemall-core/src/ \
    -Dsonar.java.binaries=litemall-core/target/

docker版本的scanner

SONARQUBE_URL=http://h2.stuq.ceshiren.com:9000/
TOKEN="8a146b0c391b38381c74e65e07b6b8b2b7829d34"
YOUR_REPO="/root/sonarqube/sonar-scanning-examples/sonarqube-scanner"

docker run \
    --rm \
    -e SONAR_HOST_URL="${SONARQUBE_URL}" \
    -e SONAR_LOGIN="${TOKEN}" \
    -v "${YOUR_REPO}:/usr/src" \
    sonarsource/sonar-scanner-cli

演练项目

官方示例

git clone https://github.com/SonarSource/sonar-scanning-examples.git
cd sonar-scanning-examples

临时token
hogwarts: cfc1c1d8bb8007fb2cff6b3cd797adc091698046

SONARQUBE_TOKEN=8a66a9a9bc94af9b877e31110e84c818e1de138e

分析命令

sonar-scanner \
  -Dsonar.host.url=http://sonarqube.ceshiren.com:9000 \
  -Dsonar.login=$SONARQUBE_TOKEN

代码分析实战

开源测试平台

git clone https://github.com/linlinjava/litemall.git
cd litemall
mvn clean package

SONARQUBE_TOKEN=e3ef00cf6fef4e735230b999f7c29dbc229a2eea
mvn  sonar:sonar  \
  -Dsonar.host.url=http://sonarqube.ceshiren.com:9000 \
  -Dsonar.login=$SONARQUBE_TOKEN
  -Dsonar.projectName=litemall_$USER
  -Dsonar.projectKey=litemall_$USER

单元测试与代码覆盖率实战

mvn clean \
  org.jacoco:jacoco-maven-plugin:0.8.6:prepare-agent \
  test \
  org.jacoco:jacoco-maven-plugin:0.8.6:report\
  -Dmaven.test.failure.ignore=true \
  -Dmaven.test.skip=false

利用scanner上传覆盖率数据

find $PWD -name "jacoco.*"

mvn \
  clean \
  org.jacoco:jacoco-maven-plugin:0.8.6:prepare-agent \
  test \
  org.jacoco:jacoco-maven-plugin:0.8.6:report  \
  sonar:sonar  \
  -Dsonar.projectKey=demo   \
  -Dsonar.host.url=http://perf2.hogwarts.ceshiren.com:9000   \
  -Dsonar.login=cfc1c1d8bb8007fb2cff6b3cd797adc091698046  \
  -Dsonar.coverage.jacoco.xmlReportPaths="$PWD/**/**/**/**/jacoco.xml" \
  -Dmaven.test.failure.ignore=true   \
  -Dmaven.test.skip=false

集成测试与代码覆盖率实战

  • 使用tcp模式
  • 精准测试做法,在每次集成测试的用例之后(teardown)中获取覆盖率数据并重置覆盖率数据
  • 传统覆盖率做法,在所有集成测试的用例之后(teardown_session)中获取覆盖率数据
  • 覆盖率数据merge
  • 覆盖率数据的report 生成xml
  • 使用sonarqube上传java专属测试相关数据 sonar.junit.reportPaths sonar.coverage.jacoco.xmlReportPaths
  • 通用测试执行数据与覆盖率数据 sonar.testExecutionReportPaths sonar.coverageReportPaths

相关链接

https://docs.sonarqube.org/latest/

1 个赞

老师我按照你docker安装的部署一步一步执行,还是报了错误信息,如下

root@racknerd-2e0a9b:~# docker logs -f sonarqube_prod
2024.07.08 07:24:48 INFO app[o.s.a.AppFileSystem] Cleaning or creating temp directory /opt/sonarqube/temp
2024.07.08 07:24:48 INFO app[o.s.a.es.EsSettings] Elasticsearch listening on [HTTP: 127.0.0.1:9001, TCP: 127.0.0.1:43903]
2024.07.08 07:24:48 INFO app[o.s.a.ProcessLauncherImpl] Launch process[ELASTICSEARCH] from [/opt/sonarqube/elasticsearch]: /opt/java/openjdk/bin/java -Xms4m -Xmx64m -XX:+UseSerialGC -Dcli.name=server -Dcli.script=./bin/elasticsearch -Dcli.libs=lib/tools/server-cli -Des.path.home=/opt/sonarqube/elasticsearch -Des.path.conf=/opt/sonarqube/temp/conf/es -Des.distribution.type=tar -cp /opt/sonarqube/elasticsearch/lib/:/opt/sonarqube/elasticsearch/lib/cli-launcher/ org.elasticsearch.launcher.CliToolLauncher
2024.07.08 07:24:48 INFO app[o.s.a.SchedulerImpl] Waiting for Elasticsearch to be up and running
2024-07-08 07:25:07,503 main ERROR Unable to create file /opt/sonarqube/logs/es.log java.io.IOException: Permission denied
at java.base/java.io.UnixFileSystem.createFileExclusively(Native Method)
at java.base/java.io.File.createNewFile(Unknown Source)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.appender.rolling.RollingFileManager$RollingFileManagerFactory.createManager(RollingFileManager.java:735)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.appender.rolling.RollingFileManager$RollingFileManagerFactory.createManager(RollingFileManager.java:718)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.appender.AbstractManager.getManager(AbstractManager.java:144)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.appender.OutputStreamManager.getManager(OutputStreamManager.java:100)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.appender.rolling.RollingFileManager.getFileManager(RollingFileManager.java:217)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.appender.RollingFileAppender$Builder.build(RollingFileAppender.java:135)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.appender.RollingFileAppender$Builder.build(RollingFileAppender.java:62)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:124)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:1138)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1063)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1055)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:664)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:258)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:304)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:621)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:285)
at org.elasticsearch.server@8.13.4/org.elasticsearch.common.logging.LogConfigurator.configure(LogConfigurator.java:250)
at org.elasticsearch.server@8.13.4/org.elasticsearch.common.logging.LogConfigurator.configure(LogConfigurator.java:129)
at org.elasticsearch.server@8.13.4/org.elasticsearch.bootstrap.Elasticsearch.initPhase1(Elasticsearch.java:134)
at org.elasticsearch.server@8.13.4/org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:69)

2024-07-08 07:25:07,525 main ERROR Could not create plugin of type class org.apache.logging.log4j.core.appender.RollingFileAppender for element RollingFile: java.lang.IllegalStateException: ManagerFactory [org.apache.logging.log4j.core.appender.rolling.RollingFileManager$RollingFileManagerFactory@350aac89] unable to create manager for [/opt/sonarqube/logs/es.log] with data [org.apache.logging.log4j.core.appender.rolling.RollingFileManager$FactoryData@1c5920df[pattern=/opt/sonarqube/logs/es.%d{yyyy-MM-dd}.log, append=true, bufferedIO=true, bufferSize=8192, policy=CompositeTriggeringPolicy(policies=[TimeBasedTriggeringPolicy(nextRolloverMillis=0, interval=1, modulate=true)]), strategy=DefaultRolloverStrategy(min=-2147483648, max=2147483647, useMax=false), advertiseURI=null, layout=%d{yyyy.MM.dd HH:mm:ss} %-5level es[%logger{1.}] %msg%n, filePermissions=null, fileOwner=null]] java.lang.IllegalStateException: ManagerFactory [org.apache.logging.log4j.core.appender.rolling.RollingFileManager$RollingFileManagerFactory@350aac89] unable to create manager for [/opt/sonarqube/logs/es.log] with data [org.apache.logging.log4j.core.appender.rolling.RollingFileManager$FactoryData@1c5920df[pattern=/opt/sonarqube/logs/es.%d{yyyy-MM-dd}.log, append=true, bufferedIO=true, bufferSize=8192, policy=CompositeTriggeringPolicy(policies=[TimeBasedTriggeringPolicy(nextRolloverMillis=0, interval=1, modulate=true)]), strategy=DefaultRolloverStrategy(min=-2147483648, max=2147483647, useMax=false), advertiseURI=null, layout=%d{yyyy.MM.dd HH:mm:ss} %-5level es[%logger{1.}] %msg%n, filePermissions=null, fileOwner=null]]
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.appender.AbstractManager.getManager(AbstractManager.java:146)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.appender.OutputStreamManager.getManager(OutputStreamManager.java:100)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.appender.rolling.RollingFileManager.getFileManager(RollingFileManager.java:217)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.appender.RollingFileAppender$Builder.build(RollingFileAppender.java:135)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.appender.RollingFileAppender$Builder.build(RollingFileAppender.java:62)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:124)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:1138)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1063)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1055)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:664)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:258)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:304)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:621)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:285)
at org.elasticsearch.server@8.13.4/org.elasticsearch.common.logging.LogConfigurator.configure(LogConfigurator.java:250)
at org.elasticsearch.server@8.13.4/org.elasticsearch.common.logging.LogConfigurator.configure(LogConfigurator.java:129)
at org.elasticsearch.server@8.13.4/org.elasticsearch.bootstrap.Elasticsearch.initPhase1(Elasticsearch.java:134)
at org.elasticsearch.server@8.13.4/org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:69)

2024-07-08 07:25:07,530 main ERROR Unable to invoke factory method in class org.apache.logging.log4j.core.appender.RollingFileAppender for element RollingFile: java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.RollingFileAppender java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.RollingFileAppender
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.findFactoryMethod(PluginBuilder.java:260)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:136)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:1138)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1063)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1055)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:664)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:258)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:304)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:621)
at org.apache.logging.log4j.core@8.13.4/org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:285)
at org.elasticsearch.server@8.13.4/org.elasticsearch.common.logging.LogConfigurator.configure(LogConfigurator.java:250)
at org.elasticsearch.server@8.13.4/org.elasticsearch.common.logging.LogConfigurator.configure(LogConfigurator.java:129)
at org.elasticsearch.server@8.13.4/org.elasticsearch.bootstrap.Elasticsearch.initPhase1(Elasticsearch.java:134)
at org.elasticsearch.server@8.13.4/org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:69)

2024-07-08 07:25:07,534 main ERROR Null object returned for RollingFile in Appenders.
2024-07-08 07:25:07,535 main ERROR Unable to locate appender “file_es” for logger config “root”
2024.07.08 07:25:08 INFO es[o.e.n.NativeAccess] Using [jna] native provider and native methods for [Linux]
2024.07.08 07:25:09 ERROR es[o.e.b.Elasticsearch] fatal exception while booting Elasticsearch
java.lang.IllegalStateException: Unable to access ‘path.data’ (/opt/sonarqube/data/es8)
at org.elasticsearch.bootstrap.FilePermissionUtils.addDirectoryPath(FilePermissionUtils.java:66) ~[elasticsearch-8.13.4.jar:?]
at org.elasticsearch.bootstrap.Security.addFilePermissions(Security.java:236) ~[elasticsearch-8.13.4.jar:?]
at org.elasticsearch.bootstrap.Security.createPermissions(Security.java:178) ~[elasticsearch-8.13.4.jar:?]
at org.elasticsearch.bootstrap.Security.configure(Security.java:125) ~[elasticsearch-8.13.4.jar:?]
at org.elasticsearch.bootstrap.Elasticsearch.initPhase2(Elasticsearch.java:199) ~[elasticsearch-8.13.4.jar:?]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:73) ~[elasticsearch-8.13.4.jar:?]
Caused by: java.nio.file.AccessDeniedException: /opt/sonarqube/data/es8
at sun.nio.fs.UnixException.translateToIOException(Unknown Source) ~[?:?]
at sun.nio.fs.UnixException.rethrowAsIOException(Unknown Source) ~[?:?]
at sun.nio.fs.UnixException.rethrowAsIOException(Unknown Source) ~[?:?]
at sun.nio.fs.UnixFileSystemProvider.createDirectory(Unknown Source) ~[?:?]
at java.nio.file.Files.createDirectory(Unknown Source) ~[?:?]
at java.nio.file.Files.createAndCheckIsDirectory(Unknown Source) ~[?:?]
at java.nio.file.Files.createDirectories(Unknown Source) ~[?:?]
at org.elasticsearch.bootstrap.Security.ensureDirectoryExists(Security.java:330) ~[elasticsearch-8.13.4.jar:?]
at org.elasticsearch.bootstrap.FilePermissionUtils.addDirectoryPath(FilePermissionUtils.java:64) ~[elasticsearch-8.13.4.jar:?]
… 5 more
ERROR: Elasticsearch did not exit normally - check the logs at /opt/sonarqube/logs/sonarqube.log

ERROR: Elasticsearch died while starting up, with exit code 1
2024.07.08 07:25:10 WARN app[o.s.a.p.AbstractManagedProcess] Process exited with exit value [ElasticSearch]: 1
2024.07.08 07:25:10 INFO app[o.s.a.SchedulerImpl] Process[ElasticSearch] is stopped
2024.07.08 07:25:10 INFO app[o.s.a.SchedulerImpl] SonarQube is stopped

还请老师看下是什么原因,多谢老师了

你的启动命令是啥,看样子是你挂载的目录权限问题。