Hudson+Ant+SVN的Junit实例

来源:互联网 发布:视频广告过滤大师 mac 编辑:程序博客网 时间:2024/06/05 01:52

1.Install JDK
1) You can run jdk-6u31-windows-i586.exe and next is always
2) Add the path to the systtem variant
My Computer --> Property --> Environment Variant--> System Variant-->
Add-->name: JAVA_HOME value: C:\Program Files\Java\jdk1.6.0_31-->OK-->
choose Path-->edit-->add the last: ;%JAVA_HOME%-->OK-->OK-->OK

3) Test the java
cmd --> java -version

java version "1.6.0_31"
Java(TM) SE Runtime Environment (build 1.6.0_31-b05)
Java HotSpot(TM) Client VM (build 20.6-b01, mixed mode, sharing)

It shows it is ok.

2.Install TortoiseSVN and CollabNetSubversion Server
1) Run TortoiseSVN-1.7.5.22551-win32-svn-1.7.3.msi and always next or finish
2) Run CollabNetSubversionEdge-2.2.1_setup.exe -->Next-->Next-->Folder: e:\csvn -->Next-->Install-->
Finish-->
3) Use IE: http://localhost:3343/csvn
4) user: admin , password: admin
5) Repositories-->New Repository-->Name: myweb-->
choose Create standard trunk/branches/tags structure-->Create

It's ok.

3.Install Hudson(There are 2 methods to start Hudson)
A.Java Method
1) Create folder: C:\CI\hudson .Copy hudson.war to C:\CI\hudson .
2) The same path With hudson.war ,I write a file named StartCI.bat.
3) StartCI.bat is :
java -jar hudson.war

4) Create the quick link to the StartCI.bat .
5) Move the link to the topdesk.
6) Run the StartCI.bat link.
7) Use IE explorer, the address is : http://localhost:8080
You will see the Hudson page.

B.Tomcat Method
1) Run apache-tomcat-7.0.25.exe -->Next-->I Agree-->Next-->Next-->Install-->Finish-->Start Tomcat
2) Put the downloaded hudson.war into ${TOMCAT_HOME}/webapps .
3) Open http://localhost:8080/hudson and see the Hudson page.

4.Configure a Ant Project
1) Download Ant and Junit
apache-ant-1.8.2-bin.zip
http://mirror.bjtu.edu.cn/apache//ant/binaries/apache-ant-1.8.2-bin.zip

junit-4.10.jar
https://github.com/downloads/KentBeck/junit/junit-4.10.jar

2) Unzip apache-ant-1.8.2-bin.zip the file to C:\CI
the whole path is C:\CI\apache-ant-1.8.2

3) Add the path to the systtem variant
My Computer --> Property --> Environment Variant--> System Variant-->
Add-->name: ANT_HOME value: C:\CI\apache-ant-1.8.2-->OK-->
choose Path-->edit-->add the last: ;%ANT_HOME%\bin-->OK-->OK-->OK

4) Test the ant
cmd --> ant -version
Apache Ant(TM) version 1.8.2 compiled on December 20 2010

It shows that ant is installed.

5) Configure a Java project
a. create a folder named helloworld
b. creata folders src , test , lib under helloworld folder
c. copy junit-4.10.jar to the lib folder
d. create a file named HelloWorld.java under src folder ,and the contents are:

public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
public int add(int a,int b)
{
return a+b;
}
}

e. create a file named HelloWorldTest.java under test folder ,and the contents are:

import junit.framework.*;

public class HelloWorldTest extends TestCase{
public void testCase_Add(){
HelloWorld helloworld = new HelloWorld();
assertEquals( helloworld.add(1,2),3 );
}
}

e. create a file named build.xml under helloworld folder ,and the contents are:

<project name="HelloWorld" basedir="." default="main">
<property name="src.dir" value="src"/>
<property name="test.dir" value="test"/>
<property name="build.dir" value="build"/>
<property name="reports.dir" value="reports"/>
<property name="lib.dir" value="lib"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="test-classes.dir" value="${build.dir}/test-classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="main-class" value="HelloWorld"/>

<target name="clean">
<delete dir="${build.dir}"/>
</target>

<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="on"/>
</target>

<target name="test-compile">
<mkdir dir="${test-classes.dir}"/>
<javac srcdir="${test.dir}" destdir="${test-classes.dir}" includeantruntime="on">
<compilerarg value="-Xlint:unchecked"/>
<classpath>
<pathelement path="${classes.dir}"/>
<fileset dir="${lib.dir}" erroronmissingdir="false">
<include name="*.jar"/>
</fileset>
</classpath>
<include name="**/*.java"/>
</javac>
</target>

<target name="test">
<delete>
<fileset dir="${reports.dir}" includes="**/*" erroronmissingdir="false" />
</delete>
<mkdir dir="${reports.dir}"/>

<junit printsummary="on" failureProperty="test.failure">
<classpath>
<fileset dir="${lib.dir}" erroronmissingdir="false">
<include name="*.jar"/>
</fileset>
<pathelement path="${classes.dir}"/>
<pathelement path="${test-classes.dir}/"/>
</classpath>
<formatter type="xml"/>
<batchtest fork="yes" todir="${reports.dir}">
<fileset dir="${test.dir}">
<!-- only run test CLASS -->
<include name="**/*Test.java" />
</fileset>
</batchtest>
</junit>
<fail message="Test failed" if="test.failure" />
<junitreport todir="${reports.dir}">
<fileset dir="${reports.dir}">
<include name="TEST-*.xml"/>
</fileset>
</junitreport>
</target>

<target name="jar">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>

<target name="run">
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
</target>
<target name="main" depends="clean,compile,test-compile,test,jar,run"/>

</project>

e. cmd --> cd /d ... (The helloworld folder path) -->
There is approximate something.Show:

F:\helloworld>ant
Buildfile: F:\helloworld\build.xml

clean:
[delete] Deleting directory F:\helloworld\build

compile:
[mkdir] Created dir: F:\helloworld\build\classes
[javac] Compiling 1 source file to F:\helloworld\build\classes

test-compile:
[mkdir] Created dir: F:\helloworld\build\test-classes
[javac] Compiling 1 source file to F:\helloworld\build\test-classes

test:
[junit] Running HelloWorldTest
[junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.028 sec

jar:
[mkdir] Created dir: F:\helloworld\build\jar
[jar] Building jar: F:\helloworld\build\jar\HelloWorld.jar

run:
[java] Hello World

main:

BUILD SUCCESSFUL
Total time: 1 second

F:\helloworld>

f. delete build , reports folder
g. put helloworld folder in svn server-->Tortoise SVN-->Repo-browser-->
URL: http://localhost/svn/myweb/trunk-->OK-->Username: admin-->Password: admin-->
Save authentication-->OK-->Tow the helloworld folder to the trunk folder-->
Question: NO
Commit reason: Commit the helloworld project.

-->OK-->OK-->delete the local helloworld folder-->
SVN Checkout...-->URL of repository: http://localhost/svn/myweb/trunk/helloworld-->
OK-->
Now we create a repository on svn.

h. start hudson
i. firefox-->http://localhost:8080 --> Manage Hudson-->Configure System-->JDK-->
Add JDK-->Name: JDK1.6.0_31-->JAVA_HOME: C:\Program Files\Java\jdk1.6.0_31-->Ant-->
Name: apache-ant-1.8.2-->cancel Install automatically-->ANT_HOME: C:\CI\apache-ant-1.8.2-->Save

j. Hudson-->New Job-->Job name: myfirstjob-->choose Build a free-style software project-->OK-->
Source Code Management choose Subversion-->Repository URL:
http://localhost/svn/myweb/trunk/helloworld-->
enter credential-->User name/password authentication-->User name: admin-->Password: admin-->ok-->close-->
Build-->Add build step-->Invoke Ant-->Ant Version choose apache-ant-1.8.2-->Save-->Hudson-->choose myfirstjob Schedule a build-->
See Latest Console output

k. Create trigger -->Hudson-->myfirstjob-->Configure-->Choose Poll SCM-->Schedule: * * * * * (Every minute to check codes)-->
Post-build Actions-->Publish JUnit test result report-->Test report XMLs: reports/*.xml-->
choose Retain long standard output/error-->Save

l. install Email-ext plugin-->Hudson-->Manage Hudson-->Manage Plugins-->Available-->Choose Email-ext-->install-->restart Hudson-->

#########################
Here you maybe use internet,but I haven't it.So I setup a mail server and foxmail client.
Please See my hMailServer_Step by Step.txt file for the detail.
I configure 127.0.0.1 smtp server ip,User : Jenny@mymail.cn Password : Jenny
#########################

k. Hudson-->Manage Hudson-->Configure System-->E-mail Notification--> Advanced-->
SMTP server : 127.0.0.1
Default User E-mail Suffix: @mymail.cn
System Admin E-mail Address: Jenny@mymail.cn
Hudson URL: http://localhost:8080/

Use SMTP Autherntication (Choose)
User Name: Jenny@mymail.cn
Password: Jenny
User SSL (Disable)
SMTP Port: 25
Charset UTF-8
-->Test configuration by sending e-mail to System Admin Address-->Save-->Your Foxmail will receive a email from hudson.It's said that it is ok.

l. Hudson -->myfirstjob-->Configure-->E-mail Notification-->Recipients:Jenny@mymail.cn-->
Choose Send e-mail for every unstable build
Don't Choose Send separate e-mails to individuals who broke the build (because the svn user isn't a email user,so if you choose this ,the email
can't send above the correct email user.)
(Remember the Recipients users must use the blank ,if you use ',' ,the email cann't send always)
-->Save

m. Build Now-->You will receive a email.(If you build success.you will not receive a email.Please let you code error,you will receive a email).

n. Advanced Email Setting:
Hudson-->Manage Hudson-->Configure System-->Cancel Extended E-mail Notification-->
Hudson -->myfirstjob-->Configure-->Cancel E-mail Notification-->
Choose Editable Email Notification --> Global Recipient List: Jenny@mymail.cn -->
Default Content:
$PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS:

Check console output at $BUILD_URL to view the results.
${JELLY_SCRIPT,template="html"}

--> Content Type : choose HTML (text/html)
--> Advanced ...
--> Failure choose Send To Recipient List ,cancel Send To Committers ,Include Culprits
--> Add a Trigger : Success -->choose Send To Recipient List ,cancel Send To Committers ,Include Culprits
--> Save

o. Configure User-->Hudson-->Manage Hudson-->Configure System-->Enable security-->Hudson's own user database-->
choose Allow users to sign up
choose Enable captcha sign up
choose Notify user of Hudson account creation
-->Matrix-based security-->User/group to add: Jenny-->Add-->Set Jenny Administer role-->Save-->Create an account -->
Input Jenny information(Notice the picture different the capital)-->Sign up-->
Hudson-->Configure User-->Hudson-->Manage Hudson-->Configure System-->Enable security-->Hudson's own user database-->
cancel Allow users to sign up --> Save-->

5.Install HudsonTrayTracker
Run HudsonTrayTracker_v0.12.1.msi -->Next-->Next-->Install-->Finish-->
To Path C:\Program Files\Hudson Tray Tracker -->Run HudsonTrayTracker.exe-->Settings-->
Add server-->URL http://localhost:8080-->Authentication-->User name: Jenny-->Password: Jenny-->
-->OK-->Choose the myfirstjob project-->Close window-->OK


FAQ:
1.Change the language of the hudson(There are 2 methods to change)
A.Change the firefox language.(I use the firefox)
1) use https://addons.mozilla.org/en-US/firefox/addon/quick-locale-switcher/
2) Put the button of + Add to Firefox.
3) Install.
4) Restart Firefox.
5) Tools --> Quick locale Swticher --> en_US
It's ok.

B. Install plugin to hudson
1) Download locale.hpi file.
The local plugin page:
http://hudson-ci.org/downloads/plugins/locale/

locale.hpi
http://hudson-ci.org/downloads/plugins/locale/1.1/locale.hpi

2) Start hudson.
3) Manage Hudson-->Manage Plugins-->Advanced-->Upload Plugin-->浏览...-->point the locale.hpi file-->
Upload-->
4) Restart hudson
5) Manage Hudson-->Configure System-->Locale-->Default Language: en_US-->
if you want the language is always en_US,you can choose the Ignore browser preference and force this language to all users-->
save
It's ok.

2.CronTrigger Usage
A concrete Trigger that is used to fire a JobDetail at given moments in time, defined with Unix 'cron-like' definitions.

For those unfamiliar with "cron", this means being able to create a firing schedule such as: "At 8:00am every Monday through Friday" or "At 1:30am every last Friday of the month".

The format of a "Cron-Expression" string is documented on the CronExpression class.

MINUTE Minutes within the hour (0-59)
HOUR The hour of the day (0-23)
DOM Day-of-Month The day of the month (1-31)
MONTH The month (1-12)
DOW Day-of-Week The day of the week (0-7) where 0 and 7 are Sunday.


其中 * 号是代码 "每" 的意思,就是此属性的每一个可能值.
在这里表示every Wednesday.

?号用在 day-of-month 和 day-of-week 属性上.

L代表Last,W代表Weekend

#号指定每月的第几个星期几.如:FRI#3 意思是每月的第三个星期五.


Here are some full examples:
Expression Meaning
"0 12 * * ?" Fire at 12pm (noon) every day
"15 10 ? * *" Fire at 10:15am every day
"15 10 * * ?" Fire at 10:15am every day
"15 10 * * ? *" Fire at 10:15am every day
"15 10 * * ? 2005" Fire at 10:15am every day during the year 2005
"* 14 * * ?" Fire every minute starting at 2pm and ending at 2:59pm, every day
"0/5 14 * * ?" Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day
"0/5 14,18 * * ?" Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day
"0-5 14 * * ?" Fire every minute starting at 2pm and ending at 2:05pm, every day
"10,44 14 ? 3 WED" Fire at 2:10pm and at 2:44pm every Wednesday in the month of March.
"15 10 ? * MON-FRI" Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday
"15 10 15 * ?" Fire at 10:15am on the 15th day of every month
"15 10 L * ?" Fire at 10:15am on the last day of every month
"15 10 ? * 6L" Fire at 10:15am on the last Friday of every month
"15 10 ? * 6L" Fire at 10:15am on the last Friday of every month
"15 10 ? * 6L 2002-2005" Fire at 10:15am on every last friday of every month during the years 2002, 2003, 2004 and 2005
"15 10 ? * 6#3" Fire at 10:15am on the third Friday of every month

Pay attention to the effects of '?' and '*' in the day-of-week and day-of-month fields!

NOTES:

Support for specifying both a day-of-week and a day-of-month value is not complete (you'll need to use the '?' character in on of these fields).
Be careful when setting fire times between mid-night and 1:00 AM - "daylight savings" can cause a skip or a repeat depending on whether the time moves back or jumps forward.

原创粉丝点击