Skip to content

Commit

Permalink
feat(scaffold): support sonar integration
Browse files Browse the repository at this point in the history
  • Loading branch information
taccisum committed Oct 25, 2019
1 parent b1d3ffc commit 63de138
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 60 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ $ yo deepexi-spring-cloud --help
|开发框架|✅springfox(swagger)|✅lombok|✅guava|✅common-lang3|✅joda-time|
| |✅hutool|✅payload|
|测试框架|✅junit|✅mockito|✅assertj|✅jacoco|☑️mockserver|
| |✅️jfairy|☑️mockneat|
| |✅️jfairy|☑️mockneat|✅sonar|
|开发相关|✅devtools|
|部署相关|✅docker|✅filebeat|
|其它|✅actuator|
Expand Down
37 changes: 37 additions & 0 deletions generators/app/templates/1.docs/guides/dependencies/jacoco.tmpl.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Jacoco

Jacoco可以为你统计代码覆盖率,默认作用在maven生命周期的`verify`阶段。

## 如何使用

执行maven的`verify`生命周期

```shell
$ mvn clean verify
```

如果你使用IDE,也可以直接在IDE上执行`root -> Lifecycle -> verify`

然后可查看生成的测试报告

```shell
$ open cov-report/target/jacoco-ut/index.html
```

## 配置Jacoco

可以通过 `pom.xml``cov` profile对jacoco进行详细的配置,详细请参考[官方文档](https://www.jacoco.org/jacoco/trunk/doc/maven.html)

### 常用配置

- `configuration -> rules -> rule -> limits -> limit`
- COVEREDRATIO: 最低覆盖率百分比限制
- MISSEDCOUNT: 最低无测试的类数量限制
- `configuration -> excludes -> exclude`: 需要排除统计的类

## 其它

### 关于cov-report模块

`cov-report`是为了聚合多模块工程环境下的多份报告而存在的模块,如果工程中添加了新的模块,需要在cov-report模块的pom.xml中加入相关的依赖,否则新模块的报告无法被聚合显示。

26 changes: 26 additions & 0 deletions generators/app/templates/1.docs/guides/dependencies/sonar.tmpl.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Sonar

> Sonar属于外部服务,在接入之前请先准备好可用的基础设施
## 如何接入

通过 `pom.xml``sonar` profile 配置 `sonar.host.url` 为你的sonar服务地址

```xml
<profile>
<id>sonar</id>
<properties>
<sonar.host.url>http://{sonar-url}</sonar.host.url>
</properties>
</profile>
```

然后执行

```shell
$ mvn clean verify sonar:sonar
```

如果你使用IDE,也可以直接在IDE先执行 `root -> Lifecycle -> verify`,再执行 `root -> Plugins -> sonar -> sonar:sonar`

等待任务完成后,即可在sonar ui界面查看到你的代码分析结果。
5 changes: 5 additions & 0 deletions generators/app/templates/1.docs/guides/dev_reference.tmpl.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ dependencies.map(dep => {
});
%>

## Code Quality

- [测试报告](./dependencies/jacoco.md)
- [质量检测](./dependencies/sonar.md)

## Node.JS相关

以下功能均基于Node.JS,使用前请先确保安装了[Node.JS](https://nodejs.org/zh-cn/download/)并在项目根目录下执行`npm install`
Expand Down
52 changes: 52 additions & 0 deletions generators/app/templates/cov-report/pom.tmpl.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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">
<parent>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>cov-report</artifactId>

<description>this module is only for coverage report aggregation.</description>

<dependencies>
<dependency>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}-api</artifactId>
</dependency>
<dependency>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}-provider</artifactId>
</dependency>
</dependencies>

<profiles>
<profile>
<id>cov</id>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>report-aggregate</id>
<phase>verify</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
</project>
174 changes: 115 additions & 59 deletions generators/app/templates/pom.tmpl.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<modules>
<module>${artifactId}-api</module>
<module>${artifactId}-provider</module>
<module>cov-report</module>
</modules>

<properties>
Expand All @@ -37,6 +38,17 @@
<type>pom</type>
<scope>import</scope>
</dependency>

<dependency>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}-api</artifactId>
<%='<version>${project.version}</version>'%>
</dependency>
<dependency>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}-provider</artifactId>
<%='<version>${project.version}</version>'%>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -70,65 +82,109 @@
<artifactId>versions-maven-plugin</artifactId>
<version>2.3</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.4</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>cov report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>check cov rules</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>BUNDLE</element>
<limits>
<limit>
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>0.30</minimum>
</limit>
<limit>
<counter>CLASS</counter>
<value>MISSEDCOUNT</value>
<maximum>0</maximum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
<configuration>
<dataFile>target/jacoco.exec</dataFile>
<outputDirectory>target/jacoco-ut</outputDirectory>
<excludes>
<!-- add excluded classes here -->
<exclude>${basePath}/config/**</exclude>
<exclude>${basePath}/constant/**</exclude>
<exclude>${basePath}/converter/**</exclude>
<exclude>${basePath}/domain/**</exclude>
<exclude>${basePath}/enums/**</exclude>
<exclude>${basePath}/exception/**</exclude>
<exclude>${basePath}/util/ValidationUtils.class</exclude>
<exclude>${basePath}/StartupApplication.class</exclude>
<exclude>**/*Demo*</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>cov</id>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.4</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>prepare jacoco.exec</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>cov report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>check cov rules</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>BUNDLE</element>
<limits>
<limit>
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>0.30</minimum>
</limit>
<limit>
<counter>CLASS</counter>
<value>MISSEDCOUNT</value>
<maximum>0</maximum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
<configuration>
<%='<dataFile>${project.build.directory}/jacoco.exec</dataFile>'%>
<%='<outputDirectory>${project.build.directory}/jacoco-ut</outputDirectory>'%>
<excludes>
<!-- add excluded classes here -->
<exclude>${basePath}/config/**</exclude>
<exclude>${basePath}/constant/**</exclude>
<exclude>${basePath}/converter/**</exclude>
<exclude>${basePath}/domain/**</exclude>
<exclude>${basePath}/enums/**</exclude>
<exclude>${basePath}/exception/**</exclude>
<exclude>${basePath}/util/ValidationUtils.class</exclude>
<exclude>${basePath}/StartupApplication.class</exclude>
<exclude>**/*Demo*</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>sonar</id>
<properties>
<sonar.host.url>http://127.0.0.1:9000</sonar.host.url>
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<%='<sonar.coverage.jacoco.xmlReportPaths>${project.build.directory}/jacoco-ut/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>'%>
<sonar.language>java</sonar.language>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.7.0.1746</version>
</plugin>
</plugins>
</build>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
</project>
2 changes: 2 additions & 0 deletions generators/test/app/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ required.addProjectFiles([
'1.docs/guides/dependencies/swagger.md',
'1.docs/guides/dependencies/payload.md',
'1.docs/guides/dependencies/converter.md',
'1.docs/guides/dependencies/jacoco.md',
'1.docs/guides/dependencies/sonar.md',
'1.docs/guides/dependencies/others.md',
'1.docs/sql/v1.0.0/schema.sql',
'1.docs/sql/v1.0.0/data.sql',
Expand Down

0 comments on commit 63de138

Please sign in to comment.