I have some legacy Java 6 project and I want to bring some updates on them like Java 8 time library. I found that is possible by using ThreeTen backport. But I don't know how to use it with Ant build tool. Any good references or examples please ?
Overview:
org.threeten.bp
with subpackages and use the imported classes in your code.On http://www.threeten.org/threetenbp/, at the top select Releases -> Download to get to the Maven repository. In the first search result (currently threetenbp 1.3.6 from 10-Jul-2017), in the Download column click jar. Download the file (in this case threetenbp-1.3.6.jar) to or move it to the lib folder of your Ant project. Or where you’ve got your external JARs. If you haven’t got such a place, create a folder called lib for it.
If you haven’t previously got any external dependencies in the form of external JAR files that your program uses, you may need to prepare your build.xml
file for these. In my build.xml
I added
<property name="lib.dir" value="lib"/>
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
This defines names for the lib folder and the classpath for use later. Note that I specify that all .jar
files in the lib
folder with subfolders are on the classpath, so in the future you can just drop JARs in to add them to your project. Then I added the classpath both to my compile target and to my run target:
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"/>
</target>
And
<target name="run" depends="jar">
<java fork="true" classname="${main-class}">
<classpath>
<path refid="classpath"/>
<path location="${jar.dir}/${ant.project.name}.jar"/>
</classpath>
</java>
</target>
We need to fix both targets because the external JAR(s) is/are needed both for compilation and for running.
Here’s my program. Note that the import
statements refer to org.threeten.bp
.
package ovv.ant.threetenbp;
import java.util.Date;
import org.threeten.bp.Instant;
import org.threeten.bp.DateTimeUtils;
public class AntAndThreeTenBackportDemo {
public static void main(String... commandLineArguments) {
Instant once = Instant.parse("1939-11-19T16:30:00Z");
Date oldfashionedDateObject = DateTimeUtils.toDate(once);
System.out.println("As Date: " + oldfashionedDateObject);
}
}
When I run from Ant (on my computer in Europe/Copenhagen time zone) I get:
run:
[java] As Date: Sun Nov 19 17:30:00 CET 1939
I used Ant 1.9.7, but I think it’s the same in other versions.
I used the Ant tutorial here, in particular the Using External Libraries section.