Use JUnit to check the encoding method during compilation

Source: Internet
Author: User
Tags echo message checkstyle

We all know that the most common functions in daily development are: compile, package, run JUnit testcase, and check the encoding specifications. Here is an ant template file for these most commonly used features, which will be shared with you.
<? XML version = "1.0" encoding = "gb2312"?>
<Project name = "test project" default = "all" basedir = ".">

<Property name = "src. Code" value = "src"/>
<Property name = "src. JUnit" value = "JUnit"/>
<Property name = "Lib. dir" value = "lib"/>
<Property name = "Lib. Jar" value = "testclass. Jar"/>

<! -- Checkstyle configuration -->
<Property name = "checkstyle. config" value = "checkstyle. xml"/>
<Taskdef resource = "checkstyletask. properties"
Classpath = "$ {Lib. dir}/checkstyle-all-3.1.jar"/>


<! -- Output document -->
<Property name = "Doc. dir" value = "Doc"/>
<Property name = "Doc. API" value = "$ {Doc. dir}/API"/>
<Property name = "javadoc. Package" value = "com. *"/>

<! -- Output binary file -->
<Property name = "Dist. Root" value = "Dist"/>
<Property name = "Dist. proj" value = "$ {Dist. Root}/proj"/>
<Property name = "Dist. Classes" value = "$ {Dist. proj}/classes"/>
<Property name = "Dist. lib" value = "$ {Dist. proj}/lib"/>
<Property name = "Dist. JUnit" value = "$ {Dist. Root}/JUnit"/>

<! -- Classpath -->
<Path id = "classpath">
<Fileset dir = "$ {Lib. dir}">
<Include name = "**/*. Jar"/>
</Fileset>
</Path>

<Path id = "proj. Libs">
<Fileset dir = "$ {Dist. Lib}">
<Include name = "**/*. Jar"/>
</Fileset>
</Path>

<Target name = "init">
<Mkdir dir = "$ {Doc. dir}"/>
<Mkdir dir = "$ {Dist. Root}"/>
<Mkdir dir = "$ {Dist. proj}"/>
<Mkdir dir = "$ {Dist. Lib}"/>
<Tstamp/>
<Echo message = "$ {tstamp}"> </echo>
</Target>

<Target name = "all" depends = "compilesrc, javadoc, checkstyle"/>

<! -- Compile the source file -->
<Target name = "compilesrc" depends = "init">
<Mkdir dir = "$ {Dist. classes}"/>

<Javac destdir = "$ {Dist. classes}" deprecation = "on">
<SRC Path = "$ {SRC. Code}"/>
<Classpath refID = "classpath"/>
</Javac>

<Jar jarfile = "$ {Dist. Lib}/$ {Lib. Jar}" basedir = "$ {Dist. classes}">
<Include name = "**/*. Class"/>
</Jar>
</Target>

<! -- Generate javadoc -->
<Target name = "javadoc" depends = "init">
<Mkdir dir = "$ {Doc. API}"/>

<Javadoc packagenames = "$ {javadoc. Package}" sourcepath = "$ {SRC. Code }"
Private = "yes" defaultexcludes = "yes" destdir = "$ {Doc. dir}/API">
<Classpath refID = "classpath"/>
</Javadoc>
</Target>

<! -- Compile the JUnit file -->
<Target name = "compilejunit" depends = "compilesrc">
<Mkdir dir = "$ {Dist. JUnit}"/>

<Javac destdir = "$ {Dist. JUnit}" deprecation = "on">
<SRC Path = "$ {SRC. JUnit}"/>
<Classpath refID = "classpath"/>
<Classpath refID = "proj. Libs"/>
</Javac>
</Target>

<! -- Run checkstyle check Code Specifications -->
<Target name = "checkstyle" depends = "init">
<Checkstyle Config = "$ {checkstyle. config}">
<Fileset dir = "$ {SRC. Code}" includes = "**/*. Java"/>
<Formatter type = "plain"/>
<Formatter type = "XML" tofile = "$ {Dist. Root}/checkstyle_errors.xml"/>
</Checkstyle>
</Target>

<! -- Run JUnit -->
<Target name = "JUnit" depends = "compilejunit">
<JUnit printsummary = "yes" haltonfailure = "yes">
<Classpath>
<Path refID = "classpath"/>
<Pathelement location = "$ {Dist. JUnit}"/>
</Classpath>

<Formatter type = "plain"/>

<! -- Test name = "com. testclasstest" haltonfailure = "no" OUTFILE = "result"/-->

<Batchtest todir = "$ {Dist. JUnit}">
<Fileset dir = "$ {Dist. JUnit}" includes = "**/test *. Class"/>
</Batchtest>
</JUnit>
</Target>

<! -- Clear generated classes, JUnit-related classes, and documents -->
<Target name = "clean">
<Delete dir = "$ {Dist. classes}"/>
<Delete dir = "$ {Dist. JUnit}"/>
<Delete dir = "$ {Doc. API}"/>
</Target>

<! -- Clear all output results -->
<Target name = "cleanall" depends = "clean">
<Delete dir = "$ {Doc. dir}"/>
<Delete dir = "$ {Dist. Root}"/>
</Target>
</Project>

Related Recommendations:

Discard JUnit and greet Spock

Please do not misunderstand. I did not mean anything to slander JUnit ...... Cough ...... For better use ...... Even so, you cannot really lose it, because Spock also needs JUnit.

To put it simply, Spock is a groovy BDD testing framework. If this is the first time you use this term, you may first look at the Spock example and then visit the BDD link just given:

Def "subscribers receive published events at least once "(){
When: publisher. Send (Event)
Then: (1 .. _) * subscriber. Receive (Event)
Where: event <["started", "paused", "STOPPED"]
}

The above example shows the test example in Spock, but the advantages of Spock are far greater than that.

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.