Gradle Study (-sonar)

Source: Internet
Author: User
Tags stack trace
Preface


Sonar related information is divided into 2 chapters, part of Sonar, part of Sonar runner, this study is sonar.

The sonar plugin relies on the Sonar runner plug-in, and the sonar version is 2.1.1 or more. If you are performing sonar tasks, tap Gradle sonaranalyze at the command line. The Sonaranalyze task is independent and is the task that parses the code, which does not depend on other tasks. This task does not rely on source files, but on class files and build files, so try to make full build before use. Under normal circumstances, a report will be generated one day.


Knowledge points


1.sonar plug-in


Apply plugin: ' Sonar '

2. Configuring Sonar's 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 your browser.

The database (Sonardatabase in the API) represents the information of the databases, including the access address, driver, user name and password.

This is to be filled in according to your actual attributes.


3. Configuring Sonar's project Properties


Project in Sonar (Sonarproject in the API) is used to set how code is parsed.

As with the above, it is also added in 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 multi-project build, you can configure sonar in the root project project to analyze individual sub-projects at a faster rate than you would for a separate analysis. Sonar can show the tree structure of each item on the page. The server and database in sonar only need to be configured in the root project, without having to configure one in each sub-project individually. This effect can be achieved by subprojects tasks. However, some of the custom settings for project need to be configured separately. For example, the following code


set the encoding for each sub-item


Build.gradle

subprojects {
	Sonar {
		Project {
			sourceencoding = ' UTF-8 '
		}
	}
}

If you want to set up a specific subproject, you can set it in the project task in Gradle, in the form project (' Subproject name '), such as the following code (also build.gradle in root project)

Build.gradle:


configuration of specific sub-projects


Project (":p Roject1") {
	sonar {
		Project {
			skip = True
		}
	}
}

The function of the above code is not to analyze the Project1 project when Sonar analysis is performed. Naturally, the item is not displayed in a tree structure in a Web page.
There is also a language item that is used to configure the programming language of the subproject. However, this property can only have one language for a subproject and cannot be configured in more than one language. The following example sets the language for Project2 to groovy.


Build.gradle

Project (":p Roject2") {
	sonar {
		Project {
			language = "Groovy"
		}
	}
}

The following syntax is more concise when the configured item has only one item:


Project (":p Roject2"). Sonar.project.language = "Groovy"

5. Analyze the custom source Set


By default, sonar only analyzes the two sourceset of main and test in the project. You can also add your own custom sourcesets to the range of sonar analysis, so sonar will analyze it 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 customized sourcesets.



6. Analyzing non-Java language projects


The 4th has already been said, you can set a different language for each sub-project.


7. Set Sonar Custom properties


The property settings in Sonar are set in key-value format, and the following knowledge is over, and for beginners, these things are too far away. Just remember that there is a 2 definition method, one that is globally defined, including the root project and subproject, 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 is 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 following properties can be set:


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: Forcing analysis



8. Execution Mode


You can perform sonar tasks by tapping the following statement at the command line


Gradle Sonaranalyze


actual Combat


Why there is no actual combat example, because I am in the company, you understand. And then add it at night:


Failure:build failed with an 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 with--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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.