So basically the build file is pasted below in its entirety. The build file will:
- Download the maven-ant-tasks jar file (2.0.9)
- Create 2 files, one scala and one java
- Download the scala-library and scala-compiler from the maven-central repository
- Compile the scala and java code
- Create a jar file with the compiled code. This jar file will have its Main-Class and Class-Path attributes set to allow easy usage
- Copy the scala-library into a local path that the "project" jar file expects.
When finished, type: java -jar build/test-maven-in-ant.jar (or use \ in windows).
The fun part about this build file was having it bootstrap ant-tasks via the maven task. It wouldn't be hard to pull in the groovy task as well (which I plan to attempt given more free time). Thanks to the latest scala feature of parsing java files, scala-groovy-java projects are now more easily accomplished.
Anyway, without further ado... Here's the buildfile:
<project name="test-maven-in-ant" default="package.done" xmlns:artifact="urn:maven-artifact-ant">
<property name="root.dir" value="." />
<property name="sources.dir" value="${root.dir}${file.separator}src" />
<property name="build.dir" value="${root.dir}${file.separator}build" />
<property name="build.classes.dir" value="${build.dir}${file.separator}classes" />
<property name="lib.dir" value="${root.dir}${file.separator}lib" />
<property name="maven.ant.tasks.jar" value="${lib.dir}${file.separator}maven-ant-tasks-2.0.9.jar"/>
<property name="maven.ant.tasks.bootstrap.location" value="http://apache.inetbridge.net/maven/binaries/maven-ant-tasks-2.0.9.jar"/>
<available property="maven.ant.tasks.jar.exists" file="${maven.ant.tasks.jar}"/>
<property name="package.name" value="${ant.project.name}.jar"/>
<property name="package.location" value="${build.dir}${file.separator}${package.name}"/>
<!-- This will download the "latest version" of the maven-ant-tasks if needed -->
<target name="bootstrap.maven.tasks" unless="maven.ant.tasks.jar.exists">
<mkdir dir="${lib.dir}"/>
<get src="${maven.ant.tasks.bootstrap.location}" dest="${maven.ant.tasks.jar}"/>
</target>
<!-- This will initialize all the maven ant tasks -->
<target name="init.maven.tasks" depends="bootstrap.maven.tasks">
<path
id="maven.ant.tasks.classpath"
path="${maven.ant.tasks.jar}" />
<typedef
resource="org/apache/maven/artifact/ant/antlib.xml"
uri="urn:maven-artifact-ant"
classpathref="maven.ant.tasks.classpath" />
</target>
<!-- This bootstraps the scala ant tasks from the maven repository and initializes them -->
<target name="init.scala.tasks" depends="init.maven.tasks">
<artifact:dependencies pathId="scala.tasks.classpath">
<dependency groupId="org.scala-lang" artifactId="scala-compiler" version="2.7.2-rc2" scope="compile" />
<remoteRepository id="scala-tools.repository" url="http://scala-tools.org/repo-releases" />
</artifact:dependencies>
<taskdef resource="scala/tools/ant/antlib.xml">
<classpath refid="scala.tasks.classpath" />
</taskdef>
</target>
<!-- Initializes all the directories we need to build -->
<target name="init.build.dirs">
<mkdir dir="${build.classes.dir}" />
</target>
<!-- Initializes the classpath for the project dependencies-->
<target name="init.project.dependencies" depends="init.maven.tasks">
<artifact:dependencies pathId="project.classpath" filesetId="project.dependency.fileset">
<dependency groupId="org.scala-lang" artifactId="scala-library" version="2.7.2-rc2" scope="compile" />
<remoteRepository id="scala-tools.repository" url="http://scala-tools.org/repo-releases" />
</artifact:dependencies>
</target>
<!-- Initialize source code for compilation -->
<target name="init.sources">
<mkdir dir="${sources.dir}/test"/>
<echo file="${sources.dir}/test/IDoStuff.java">package test;
public interface IDoStuff {
public void doStuff();
}
</echo>
<echo file="${sources.dir}/test/ScalaDoStuff.scala">package test
class ScalaDoStuff extends IDoStuff {
override def doStuff() : Unit = {
Console println "HAI"
}
}
object ScalaMain {
def main(args : Array[String]) : Unit = {
val tmp : IDoStuff = new ScalaDoStuff();
tmp.doStuff();
}
}
</echo>
</target>
<!-- Dummy target so we can add arbitrary init tasks -->
<target name="init.done" depends="init.build.dirs,init.project.dependencies,init.scala.tasks,init.sources">
</target>
<!-- Initial step for all compilation. Marker -->
<target name="compile.init" depends="init.done" />
<!-- compiles Scala Code -->
<target name="compile.scala" depends="compile.init">
<scalac srcdir="${sources.dir}" destdir="${build.classes.dir}" classpathref="project.classpath" force="changed">
<include name="**/*.scala" />
<include name="**/*.java" />
</scalac>
</target>
<!-- Compiles java code, depends on scala -->
<target name="compile.java" depends="compile.scala">
<javac srcdir="${sources.dir}" destdir="${build.classes.dir}" source="1.5" target="1.5">
<include name="**/*.java"/>
<classpath refid="project.classpath"/>
<classpath>
<fileset dir="${build.classes.dir}">
<include name="**/*.class"/>
</fileset>
</classpath>
</javac>
</target>
<!-- Marker for finishing the compilation phase -->
<target name="compile.done" depends="compile.scala,compile.java" />
<!-- Marker for package phase -->
<target name="package.init" depends="compile.done"/>
<!-- copy our dependencies from maven's struture into our lib directory -->
<target name="package.copy.dependencies" depends="package.init">
<copy todir="${build.dir}/lib">
<fileset refid="project.dependency.fileset" />
<!-- This mapper strips off all leading directory information -->
<mapper type="flatten" />
</copy>
</target>
<!-- package our jar file -->
<target name="package.jar" depends="package.init">
<jar destfile="${package.location}" basedir="${build.classes.dir}">
<include name="**/*.class"/>
<manifest>
<!-- Hacked for now -->
<attribute name="Main-Class" value="test.ScalaMain"/>
<attribute name="Class-Path" value="lib/scala-library-2.7.2-rc2.jar"/>
</manifest>
</jar>
</target>
<!-- marks the end of the package phase -->
<target name="package.done" depends="package.jar,package.copy.dependencies"/>
</project>
0 comments:
Post a Comment