October 19, 2006
XML and the Ugly Duckling
I love XML, but XML-based ANT scripts can be difficult to read and write. Enter project Gosling.
Project Gosling (https://gosling.dev.java.net/), named after the subect of the book, The Ugly Duckling, not the author of Java, allows you to write build scripts for Java projects in, what else? Java. Justin Lee created Gosling to work just like Ant, but with Java build scripts that use the Java SE 1.6 compiler API instead of XML.
In fact, Gosling's creator Justin Lee goes so far as to say that Gosling can be considered a fork of the Ant project, with some key differences:
- By removing the XML processing within Ant, the code has been cleaned up and restructured in a way that is more natural to most Java developers.
- Java 1.6, with the Compiler API, is a minimum requirement
- Gosling build scripts are written in Java, not XML
You can compile and execute your Java-based build scripts using javac and java, respectively. The benefits of Java build scripts over XML are:
- Java is a programming language; XML is not
- Gosling build scripts, being built in Java, are more comfortable for Java developers to deal with. There is no need to learn XML-structured Ant syntax.
- Build-file inheritance is supported
- Ant issues such as those around antcall go away as all logic is built in Java
To get an idea of what a Gosling build script looks like, here is the build script for Gosling itself:
import java.io.File;
import com.antwerkz.gosling.Default;
import com.antwerkz.gosling.Description;
import com.antwerkz.gosling.Project;
import com.antwerkz.gosling.tasks.Copy;
import com.antwerkz.gosling.tasks.Jar;
import com.antwerkz.gosling.tasks.Javac;
import com.antwerkz.gosling.tasks.Javadoc;
import com.antwerkz.gosling.tasks.Zip;
import com.antwerkz.gosling.types.DirSet;
import com.antwerkz.gosling.types.FileSet;
import com.antwerkz.gosling.types.Path;
public class Gosling extends Project {
public static final String VERSION = "0.1";
private static final String BUILD_DIR = "build/classes";
private static final String DIST_DIR = "build/dist";
private static final String DIST_FILE = "build/gosling-" + VERSION + ".zip";
private static final String BOOTSTRAP_BIN_DIR = "bootstrap/bin";
private static final String BOOTSTRAP_LIB_DIR = "bootstrap/lib";
private static final String JAVA_SRC_DIR = "src/java";
private static final String BIN_SRC_DIR = "src/bin";
private static final String JAVADOC_DIR = "api";
public Gosling() {
loadProperties("gosling/gosling.properties");
}
@Default
@Description("Builds the Gosling project")
public void build() {
new Javac(this, BUILD_DIR)
.addSources(new FileSet("src/java")
.addInclude("**/*.java"))
.addOption("-Xlint:unchecked")
.addOption("-Xlint:deprecated")
.addOption("-g")
.execute();
new Copy(this)
.setDestDir(new File(BUILD_DIR))
.addFileSet(new FileSet(JAVA_SRC_DIR)
.addExclude("**/*.java"))
.execute();
}
@Description("Removes the build directory")
public void clean() {
delete("build");
}
@Description("Builds the distribution bundles")
public void dist() {
clean();
jar();
mkdir(DIST_DIR);
new Copy(this)
.setDestDir(new File(DIST_DIR + "/bin"))
.addFileSet(new FileSet(BIN_SRC_DIR))
.execute();
new Copy(this)
.setDestDir(new File(DIST_DIR + "/lib"))
.addFileSet(new FileSet(BUILD_DIR)
.addInclude("*.jar"))
.execute();
new Zip(this, new File(DIST_FILE))
.addFileSet(new FileSet(".")
.addInclude("build.*")
.addInclude("src/**")
.addInclude("lib/*.jar"))
.execute();
}
@Description("Builds a jar of the compiled resources")
public void jar() {
build();
new Jar(this, new File("build/gosling.jar"))
.addFileSet(new FileSet(BUILD_DIR)
.addInclude("**"))
.execute();
}
@Description("Builds the javadoc")
public void javadoc() {
delete(JAVADOC_DIR);
new Javadoc(this, new File(JAVADOC_DIR))
.setSourcepath(new Path(this)
.setPath(JAVA_SRC_DIR))
.addPackageset(new DirSet(JAVA_SRC_DIR))
.execute();
}
}
Posted by Eric Bruno at 10:43 PM Permalink
|