Preface
Sonar related information is divided into 2 chapters, part is Sonar, part is sonar runner, this study is sonar.
The sonar plug-in relies on the Sonar runner plug-in, and the sonar version is more than 2.1.1. If you want to perform a sonar task, knock Gradle Sonaranalyze at the command line. The Sonaranalyze task exists independently, that is, the task that analyzes the code, and the task does not depend on other tasks. This task does not depend on the source file, but on the class file and build file, so try to make a full build before using it. Normally, a report is generated one day.
Knowledge points
1.sonar Plugin
Apply plugin: ' Sonar '
2. Configure sonar servers and databases
Add a sonar (Sonarrootmodel in the API) task in Build.gradle to configure the information above
Build.gradle
sonar{
server{
url = "http://localhost:9002/"
}
database{
url = "jdbc:mysql://localhost:3306/ Sonar "
driverclassname =" Com.mysql.jdbc.Driver "
username =" Sonar "
password =" sonar "
}
}
The URL in the server (Sonarserver in the API) represents the address of the sonar server that you access in the browser.
The database (Sonardatabase in the API) represents information about the databases, including access addresses, drivers, user names, and passwords.
This is to be filled in according to your actual attributes.
3. Configure Sonar Project Properties
Project in Sonar (Sonarproject in the API) is used to set how the code is parsed.
As with the above, it is also added to the sonar task, as follows:
Build.gradle:
sonar{
server{
url = "http://localhost:9002/"
}
database{
url = "jdbc:mysql://localhost:3306/ Sonar "
driverclassname =" Com.mysql.jdbc.Driver "
username =" Sonar "
password =" sonar "
}
Project {
Coberturareportpath = file ("$buildDir/cobertura.xml")
}
}
4. Multi-Project Analysis
In a multiple-project build, you can configure sonar in the root project to analyze the subprojects so that it is faster than you would have a single analysis. Sonar can display the tree-shaped structure of each item on the page. The server and database in sonar only need to be configured on the root project, without having to configure one for each subproject separately. This effect can be achieved through the subprojects task. However, some custom settings for project need to be configured separately. For example, the following code
set encoding for each subproject
Build.gradle
subprojects {
Sonar {
Project {
sourceencoding = ' UTF-8 '
}
}
}
If you want to set up a specific subproject, you can set the project task in Gradle, in the form of Project (' Subproject name '), such as the following code (also build.gradle in root project)
Build.gradle:
configuration of a specific child project
Project (":p Roject1") {
sonar {
Project {
skip = True
}
}}
The function of the above code is not to analyze Project1 this project when conducting sonar analysis. Naturally, the item is not displayed in the tree structure of the Web page.
There is also a language item that is used to configure the programming language of the subproject. However, this property can have only one language for a subproject and cannot be configured with more than one language. The following example sets the language for Project2 to groovy.
Build.gradle
Project (":p Roject2") {
sonar {
Project {
language = ' Groovy '
}
}}
When only one item is configured, the following syntax is simpler:
Project (":p Roject2"). Sonar.project.language = "Groovy"
5. Analyze a custom source Set
By default, sonar only analyzes the two sourceset in the project, main and test. You can also add your own custom sourcesets to the scope of the sonar analysis so that sonar is analyzed together.
You can append your own custom sourcesets to the sonar analysis directory in the following ways:
Build.gradle:
Sonar.project {
Sourcedirs + = sourceSets.custom.allSource.srcDirs
Testdirs + = SourceSets.integTest.allSource.srcDirs
}
Or
sonar{
server{
url = "http://localhost:9002/"
}
database{
url = "jdbc:mysql://localhost:3306/ Sonar "
driverclassname =" Com.mysql.jdbc.Driver "
username =" Sonar "
password =" sonar "
}
Project {
Coberturareportpath = file ("$buildDir/cobertura.xml")
sourcedirs + = SourceSets.custom.allSource.srcDirs
Testdirs + = SourceSets.integTest.allSource.srcDirs
}
}
Where custom and Integtest are your custom sourcesets.
6. Analysis of non-Java language projects
4th has been said, you can set a different language for each subproject.
7. Set Sonar Custom properties
The property settings in Sonar are set in key-value format, and the following knowledge is over again, for beginners, these things are too far away. Just remember that there are 2 defined methods, one that is globally defined, including root projects and subprojects, and a small scope that applies only to this project. Defined by Withglobalproperties and withprojectproperties respectively.
Global
Build.gradle:
sonar.withglobalproperties {props->
props["some.global.property"] = "some value"
//Non-string values are Automatically converted to Strings
props["other.global.property"] = ["foo", "Bar", "Baz"]
}
Private
sonar.project.withProjectProperties {props->
props["some.project.property"] = "some value"
//Non-string Values are automatically converted to Strings
props["other.project.property"] = ["foo", "Bar", "Baz"]
}
7. Configure sonar in the command line mode
(or do not do so well, how tired AH)
The properties you can set are as follows:
Server.url: Server address
Database.url: Database Address
Database.driverclassname: Driver name
Database.username: Database user name
Database.password: Database Password
Showsql: Print SQL language
Showsqlresults: Printing Results
Verbose:log level
Forceanalysis: Mandatory Analysis
8. Mode of Implementation
You can perform sonar tasks by knocking the following statement at the command line
Gradle Sonaranalyze
actual Combat
Why there are no combat examples, because I'm in the company, you know. Wait for the evening to add:
Failure:build failed with a exception.
* What went wrong:
could not resolve all dependencies for configuration ': Testcompile '.
> could not resolve junit:junit:4.+.
Required by:
: testng_gradl:1.0
> Failed to list versions for Junit:junit.
> Unable to load Maven meta-data from Https://repo1.maven.org/maven2/juni
/junit/maven-metadata.xml.
> Could not get ' Https://repo1.maven.org/maven2/junit/junit/maven-meta
ata.xml '.
> Connection to https://repo1.maven.org refused
* Try:
Run and--stacktrace option to get the stack trace. R Un with--info or--debug option to get more
log output.
Build FAILED Total
time:28.194 secs