Prerequisites
-
The host server of the environment is 10.0.2.47;
-
Install vagrant on server 47;
-
Add vagrant box of precise x86_64:
vagrant box add ...
; -
Create a directory named "bvt-package" under ~/warez, which contains: apache-ant-1.9.3-bin.tar.bz2 cobertura.jar hamcrest-core-1.3.jar jdk-6u45-linux-x64.bin jenkins-1.544.war junit-4.8.1.jar sonar-3.6.1.zip svn_amd64_ubuntu12.04 libapr1_1.4.6-1_amd64.deb libaprutil1_1.3.12+dfsg-3_amd64.deb libdb4.8_4.8.30-11ubuntu1_amd64.deb libneon27-gnutls_0.29.6-1_amd64.deb libsvn1_1.6.17dfsg-3ubuntu3_amd64.deb subversion_1.6.17dfsg-3ubuntu3_amd64.deb unzip_6.0-4ubuntu1_amd64.deb zip_3.0-4_amd64.deb
-
The demo product codes in subversion repository is GNDP;
Build BVT Environment
$ cd /home/bvt/docs
$ take mybvt
$ vagrant box list
$ vagrant init precise64
// Add the following texts into Vagrantfile after the line 'config.vm.box = "precise64":'
config.vm.define "bs" do |bvtserver|
bvtserver.vm.hostname = "bvt-server"
bvtserver.vm.network "private_network", ip: "192.168.50.3",
virtualbox__intnet: "testnet"
bvtserver.vm.provider :virtualbox do |vbox|
vbox.customize ["modifyvm", :id, "--natnet1", "10.3/16"]
vbox.customize ["modifyvm", :id, "--memory", 1000]
end
bvtserver.vm.provision :shell, path: "provision.sh", privileged: false
bvtserver.vm.network "forwarded_port", guest: 9000, host: 9000
bvtserver.vm.network "forwarded_port", guest: 8088, host: 8088
end
$ scp -r bvt@10.0.2.47:/home/bvt/warez/bvt-package packages
$ vagrant up bs
Use This Environment
-
Open Sonarqube website, login with "admin/admin", in Settings -> Qualify Profiles, add rules "TongYong" with import "tyrules.xml" on local disk (the next section its content);;
-
Open Jenkins website;
-
Create Jenkins project: create a project named "GNDP" and add a "shell" build step: "/vagrant/bvt-gndp.sh";
-
Build this project;
-
You get the build log on jenkins website and code analysis results on sonarqube website;
Needed Scripts
Vagrant provision script
This is the environment building script executed during the vagrant provision period, so it's cited in Vagrantfile.
$ cat /home/bvt/docs/mybvt/provision.sh
#!/bin/sh
sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
sudo dpkg -i /vagrant/packages/svn_amd64_ubuntu12.04/*.deb
sudo dpkg -i /vagrant/packages/zip_3.0-4_amd64.deb
sudo dpkg -i /vagrant/packages/unzip_6.0-4ubuntu1_amd64.deb
APP_HOME=/home/vagrant/apps
mkdir $APP_HOME
cd $APP_HOME
chmod 755 /vagrant/packages/jdk-6u45-linux-x64.bin
sh /vagrant/packages/jdk-6u45-linux-x64.bin
tar jxf /vagrant/packages/apache-ant-1.9.3-bin.tar.bz2
unzip /vagrant/packages/sonar-3.6.1.zip
export PATH=$PATH:/home/vagrant/apps/jdk1.6.0_45/bin:/home/vagrant/apps/apache-ant-1.9.3/bin
export LC_ALL=en_US.UTF-8
export JENKINS_HOME=/home/vagrant/jenkinshome
mkdir $JENKINS_HOME
sonar-3.6.1/bin/linux-x86-64/sonar.sh start
nohup java -jar /vagrant/packages/jenkins-1.544.war --httpPort=8088 1>jenkins.log 2>jenkins-err.log &
BVT shell script
This script checkout the source code to be built, then invoke the Ant script. It's cited in Jenkins project definition.
$ cat /home/bvt/docs/mybvt/bvt-gndp.sh
#!/bin/sh
export PATH=$PATH:/home/vagrant/apps/jdk1.6.0_45/bin:/home/vagrant/apps/apache-ant-1.9.3/bin
export LC_ALL=en_US.UTF-8
svnprefix='svn co --non-interactive --no-auth-cache --username bvt_common --password password'
svnroot='http://10.0.2.59:8118/svn/gcp/Development-Area'
prod=GNDP
$svnprefix ${svnroot}/3-SCL/Trunk/$prod /vagrant/codes/$prod
rm -rf /vagrant/codes/*.zip
cd /vagrant/codes/$prod; ant
Ant build script
This script resides in the root folder of the source project GNDP in subversion repository.
<?xml version="1.0" encoding="UTF-8"?>
<project name="MyJavaProj" basedir="./" default="coverage_report">
<property name="build.dir" value="${basedir}/build" />
<target name="init">
<delete dir="${build.dir}"/>
<mkdir dir="${build.dir}"/>
</target>
<property name="src.dir" value="${basedir}/src" />
<property name="src.class.dir" value="${build.dir}/class" />
<target name="compile-src" depends="init">
<mkdir dir="${src.class.dir}"/>
<javac srcdir="${src.dir}" destdir="${src.class.dir}" encoding="UTF-8" debug="true"/>
</target>
<taskdef classpath="cobertura.jar" resource="tasks.properties" />
<property name="instrumented.class.dir" value="${build.dir}/instrumented-class" />
<property name="cobertura.data.file" value="${instrumented.class.dir}/cobertura.ser" />
<target name="instrument" depends="compile-src">
<cobertura-instrument datafile="${cobertura.data.file}" todir="${instrumented.class.dir}">
<fileset dir="${src.class.dir}">
<include name="**/*.class" />
</fileset>
</cobertura-instrument>
</target>
<property name="test.dir" value="${basedir}/test" />
<property name="test.class.dir" value="${build.dir}/test" />
<target name="compile-test" depends="instrument">
<mkdir dir="${test.class.dir}"/>
<javac srcdir="${test.dir}" destdir="${test.class.dir}">
<classpath>
<pathelement location="${instrumented.class.dir}"/>
<pathelement location="${src.class.dir}"/>
</classpath>
</javac>
</target>
<property name="unittest.report.dir" value="${build.dir}/unittest_report" />
<property name="lib.dir" value="${basedir}/lib" />
<target name="unittest" depends="compile-test">
<mkdir dir="${unittest.report.dir}"/>
<junit printsummary="yes" fork="yes">
<sysproperty key="net.sourceforge.cobertura.datafile" file="${cobertura.data.file}" />
<classpath>
<pathelement location="${instrumented.class.dir}"/>
<pathelement location="${src.class.dir}"/>
<pathelement location="${test.class.dir}"/>
<pathelement location="${src.dir}"/>
<path refid="lib.path"/>
</classpath>
<formatter type="xml"/>
<batchtest todir="${unittest.report.dir}" haltonerror="no">
<fileset dir="${test.dir}">
<include name="**/*.java" />
</fileset>
</batchtest>
</junit>
</target>
<property name="coverage.report.dir" value="${build.dir}/coverage_report" />
<target name="coverage-report" depends="unittest">
<cobertura-report datafile="${cobertura.data.file}" srcdir="${src.dir}" destdir="${coverage.report.dir}" format="xml" />
</target>
<property name="sonar.jdbc.url" value="jdbc:h2:tcp://localhost:9092/sonar" />
<property name="sonar.jdbc.username" value="sonar" />
<property name="sonar.jdbc.password" value="sonar" />
<property name="sonar.projectKey" value="com.doco.gif.MyModule" />
<property name="sonar.projectName" value="My Unit Test Project Demo" />
<property name="sonar.projectVersion" value="2.1" />
<property name="sonar.language" value="java" />
<property name="sonar.sources" value="src" />
<property name="sonar.tests" value="test" />
<property name="sonar.binaries" value="${src.class.dir},${test.class.dir},${build.instrument.dir}" />
<property name="sonar.dynamicAnalysis" value="reuseReports" />
<property name="sonar.surefire.reportsPath" value="${unittest.report.dir}" />
<property name="sonar.core.codeCoveragePlugin" value="cobertura" />
<property name="sonar.cobertura.reportPath" value="${coverage.report.dir}/coverage.xml" />
<target name="sonar" depends="coverage-report">
<taskdef classpath="sonar-ant-task-2.1.jar" uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml"/>
<sonar:sonar xmlns:sonar="antlib:org.sonar.ant" />
</target>
</project>
Code Static check rules
This file defines the static check rules used by Sonarqube. It's imported manually from file "tyrules.xml" on local hard disk.
$ cat ~/docs/tyrules.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.3//EN" "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
<property name="severity" value="warning"/>
<module name="TreeWalker">
<metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
<property name="fileExtensions" value="java"/>
<module name="MethodLength">
<property name="max" value="200"/>
</module>
<module name="NestedIfDepth">
<property name="max" value="5"/>
</module>
<module name="CyclomaticComplexity"/>
<module name="ParameterNumber">
<property name="max" value="6"/>
</module>
</module>
<module name="StrictDuplicateCode">
<property name="fileExtensions" value="java"/>
<property name="min" value="75"/>
</module>
<module name="FileLength">
<property name="fileExtensions" value="java"/>
<property name="max" value="1000"/>
</module>
</module>
For Unit Test
- copy junit-4.*.jar, cobertura.jar and hamcrest-core-1.3.jar to $ANT_HOME/lib;