Showing posts with label Ant. Show all posts
Showing posts with label Ant. Show all posts

Thursday, February 9, 2012

Ant Practice: Encoding Message Bundles by Ant

As we mentioned at JSTL Practice: I18N (Internationalization) practice, Locale, Bundle and Message.,
to create the message bundles for i18n, we have to run native2ascii in command line,
and may move the files as need or type very long path.

Now we know we can do these tasks by Ant (see Ant Introduction and First Try),
we will do this practice as below:

Write build file

For encoding message resource (from specific folder)
to message properties (and put to specific folder),
we have to write the build file as below:

<project name="genMessageBundles" default="encodeMessage" basedir=".">

    <!-- path property
        name denotes the property name
        location denotes the path
        the ${basedir} is the root of the project -->
    <property name="message.resource.path" location="${basedir}/src/test/jstl/i18n/resources"/>
    <property name="message.properties.path" location="${basedir}/src/test/jstl/i18n/properties"/>

    <!-- clear old files
        this will delete all *.properties
        under /src/test/jstl/i18n/properties -->
    <target name="clean">
        <delete dir="${message.properties.path}" includes="**/*.properties" />
    </target>

    <!-- encoding msgs
        this will encode all *.resource
        under /src/test/jstl/i18n/resources 
        rename the encoded files to *.properties
        and put them to /src/test/jstl/i18n/properties
        encoding is UTF-8 -->
    <target name="encodeMessage" depends="clean">
        <echo message="begin"/>
            <native2ascii src="${message.resource.path}" encoding="UTF-8"
                includes="**/*.resource" dest="${message.properties.path}" ext=".properties" />
        <echo message="end"/>
    </target>
</project>

Modify Resource Files

Then we modify the message resource files as below:



Run Ant Build

After we change the message resource files,
we can run Ant Build to encode them to message properties files.



Test the result

Then we can run the web project and see the new messages:



Download:
The full project is at github
https://github.com/benbai123/JSP_Servlet_Practice/tree/master/Practice/JSTLPractice

File of this practice:
ant.encodeMessages.xml

Tuesday, February 7, 2012

Ant Introduction and First Try

Introduction:

Ant is a Java-based build tool. In theory, it is kind of like Make, without Make's wrinkles and with the full portability of pure Java code.

To run ant build, several steps as below (with eclipse):

Step 1: Check the runtime environment:

Open Window -> Preferences


There should a tools.jar under Ant -> Runtime -> Global Entries



Step 2: Write a build file

Create a file named ant.firsttry.xml under root directory of a Project,
the code is as below:

<!-- The project entry, execute target firstTry at first -->
<project name="antTest" default="firstTry">
    <target name="preSet">
        <echo message="Preset of First Try"/>
    </target>
    <!-- Echo message, will run the target preSet before it self -->
    <target name="firstTry" depends="preSet">
        <echo message="Ant First Try"/>
    </target>
</project>

The default attribute of project tag denotes the default target to execute,
the depends attribute of target tag denotes the target that should be executed before.

In the case above, it will run target 'preSet' then 'firstTry'

Step 3: Run the ant build

Right click file ant.firsttry.xml, Run As -> Ant Build


The result will be



Download:
The full project is at github
https://github.com/benbai123/JSP_Servlet_Practice/tree/master/Practice/JSTLPractice

File of this practice:
ant.firsttry.xml

Reference:
http://ant.apache.org/