SonarQube集成单元测试结果

对于Java来说,最简单的就是用maven进行构建

#surefire-report:report -Daggregate=true要求多个模块的报告合成一个
#可以用site作为替代,比这个更简单
mvn clean install javadoc:aggregate -Dadditionalparam=-Xdoclint:none surefire-report:report -Daggregate=true

#-Dsonar.host.url指定SonarQube地址
#-Dsonar.scm.disabled要求SonarQube不要自己去更新SVN或GIT
#-Dsonar.junit.reportPaths是单元测试报告的路径
mvn sonar:sonar -Dsonar.host.url=http://IP:9000 -Dsonar.scm.disabled=True -Dsonar.junit.reportPaths=Path_To_Surefire_Report

对于CSharp项目来说,用VS自带的CodeCoverage最方便了

#准备
#我一般只指定项目key就好了,多数情况下就是Solution名称
#d:sonar.cs.vscoveragexml.reportsPath一定要指定到正确位置
MSBuild.SonarQube.Runner.exe begin /k:"项目Key" /n:"项目名称" /v:"项目版本" /d:sonar.cs.vscoveragexml.reportsPaths="%CD%\MyProject.coverage.xml"

#构建Solution
msbuild 项目名称

#生成coverage报告
"%VSINSTALLDIR%\Team Tools\Dynamic Code Coverage Tools\CodeCoverage.exe" collect /output:"%CD%\MyProject.coverage" "PATH_TO_TEST_DLL1" "PATH_TO_TEST_DLL2" "PATH_TO_TEST_DLL3"

#coverage报告转为XML版本
"%VSINSTALLDIR%\Team Tools\Dynamic Code Coverage Tools\CodeCoverage.exe" analyze /output:"%CD%\MyProject.coverage.xml" "%CD%\MyProject.coverage"

#完成
MSBuild.SonarQube.Runner.exe end

MyEclipe中Java工程手动增加Maven支持

1修改文件.project

<buildSpec>
	<buildCommand>
		<name>org.maven.ide.eclipse.maven2Builder</name>
		<arguments>
		</arguments>
	</buildCommand>
</buildSpec>
<natures>
	<nature>org.maven.ide.eclipse.maven2Nature</nature>
</natures>

2.修改文件.classpath

<classpath>
	<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
</classpath>

3.新增文件pom.xml

<?xml version="1.0"?>
<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"&#93;
  <modelVersion>4.0.0</modelVersion>

  <groupId>groupId</groupId>
  <artifactId>artifactId</artifactId>
  <version>1.0</version>
  <name>name</name>
 
  <dependencies>
  </dependencies>

  <build>
      <plugins>
      </plugins>
  </build>  
</project>

Maven常用参数

1.常用命令参数
进行编译

mvn compile

进行打包

#测试并打包
mvn package
#直接打包,不进行测试
mvn package -DskipTests=true
#打Jar包
mvn jar:jar
#打War包
mvn war:war
#打War包但不执行最后的压缩
mvn war:exploded

进行测试

#编译单元测试
mvn test-compile
#编译并允许单元测试
mvn test

安装到本地repository

mvn install
mvn install -DskipTests=true
#安装jar包到本地仓库
mvn install:install-file -DgroupId=groupId -DartifactId=artifactId -Dversion=1.0.0 -Dpackaging=jar -Dfile=xxx.jar

安装到远程repository

mvn deploy

进行清理

mvn clean

生成网站

#生成网站
mvn site
#生成网站并发布
mvn site-deploy

打包源码包

#打包源码
mvn source:jar
#下载项目源码
mvn dependency:sources -DdownloadSources=true -DdownloadJavadocs=true

打包测试源码包

mvn source:test-jar

拷贝依赖到target目录

#拷贝依赖
mvn dependency:copy-dependencies 
#依赖分析
mvn dependency:analyze
mvn dependency:tree

生成ecplise项目

mvn eclipse:eclipse

生成ecplise项目

mvn idea:idea

生成文档

#单模块项目生成文档
mvn javadoc:javadoc
#多模块项目生成文档
mvn javadoc:aggregate

创建Jar项目

mvn archetype:create -DgroupId=gourpid -DartifactId=artifactId

创建War项目

mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=gourpid  -DartifactId=artifactId

2.settings.xml

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"]

	<localRepository>X:/Maven/repository</localRepository>	

	<servers>
		<server> 
			<id>neo-public</id> 
			<username>username</username> 
			<password>password</password> 
		</server>
		<server> 
			<id>neo-releases</id> 
			<username>username</username> 
			<password>password</password> 
		</server>
		<server> 
			<id>neo-snapshots</id> 
			<username>username</username> 
			<password>password</password> 
		</server>
	</servers>

	<mirrors>
		<mirror>
			<id>neo-public</id>
			<name>Neohope Nexux Public Mirror</name>
			<url>http://192.168.xxx.xxx:8081/nexus/content/groups/public</url>
			<mirrorOf>central</mirrorOf>
		</mirror>
	</mirrors>

	<profiles>
		<profile>
			<id>myprofile</id>
			<repositories>
				<repository> 
					<id>neo-public</id> 
					<name>Neohope Nexux Public Mirror</name> 
					<url>http://192.168.xxx.xxx:8081/nexus/content/groups/public</url> 
				</repository>
			</repositories>
		</profile>
	</profiles>
	
	<activeProfiles>
		<activeProfile>myprofile</activeProfile>
	</activeProfiles>
	
</settings>

3.pom.xml

<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.neohope.xxx</groupId>
	<artifactId>xxx-xxx</artifactId>
	<version>1.0.0</version>
	<packaging>jar</packaging>

	<name>xxx-xxx</name>
	<url>http://neohope.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<nexus.url>http://192.168.xxx.xxx:8081</nexus.url>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.5</source>
					<target>1.5</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<distributionManagement>
		<repository>
			<id>neo-releases</id>
			<name>Internal Releases</name>
			<url>${nexus.url}/nexus/content/repositories/releases</url>
		</repository>
		<snapshotRepository>
			<id>neo-snapshots</id>
			<name>Internal Snapshots</name>
			<url>${nexus.url}/nexus/content/repositories/snapshots</url>
		</snapshotRepository>
	</distributionManagement>
</project>

Maven Lifecycles

Maven有三大生命周期
default:默认生命周期,用于软件构建
clean:用于项目清理
site:用于生成项目网站

1、default

<phases>
  <phase>validate</phase>
  <phase>initialize</phase>
  <phase>generate-sources</phase>
  <phase>process-sources</phase>
  <phase>generate-resources</phase>
  <phase>process-resources</phase>
  <phase>compile</phase>
  <phase>process-classes</phase>
  <phase>generate-test-sources</phase>
  <phase>process-test-sources</phase>
  <phase>generate-test-resources</phase>
  <phase>process-test-resources</phase>
  <phase>test-compile</phase>
  <phase>process-test-classes</phase>
  <phase>test</phase>
  <phase>prepare-package</phase>
  <phase>package</phase>
  <phase>pre-integration-test</phase>
  <phase>integration-test</phase>
  <phase>post-integration-test</phase>
  <phase>verify</phase>
  <phase>install</phase>
  <phase>deploy</phase>
</phases>

2、clean

<phases>
  <phase>pre-clean</phase>
  <phase>clean</phase>
  <phase>post-clean</phase>
</phases>
<default-phases>
  <clean>
    org.apache.maven.plugins:maven-clean-plugin:2.5:clean
  </clean>
</default-phases>

3、site

<phases>
  <phase>pre-site</phase>
  <phase>site</phase>
  <phase>post-site</phase>
  <phase>site-deploy</phase>
</phases>
<default-phases>
  <site>
    org.apache.maven.plugins:maven-site-plugin:3.3:site
  </site>
  <site-deploy>
    org.apache.maven.plugins:maven-site-plugin:3.3:deploy
  </site-deploy>
</default-phases>