Skip to content

Commit

Permalink
[playframework#778] Provide a build.xml file to new applications to r…
Browse files Browse the repository at this point in the history
…un the project without Python
  • Loading branch information
huljas authored and erwan committed Apr 27, 2011
1 parent 20e0d14 commit 8cfd80f
Show file tree
Hide file tree
Showing 5 changed files with 375 additions and 2 deletions.
193 changes: 193 additions & 0 deletions application-build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
General ant build file for play applications.
Author: Heikki Uljas
Usage example:
ant -f <play path>/application-build.xml -Dplay.path=<play path> -Dbasedir=<application directory> run
Or with PLAY_PATH environment variable:
ant -f $PLAY_PATH/application-build.xml -Dbasedir=<application directory> run
You could also define the project file in the application directory
build.xml:
<project basedir=".">
<property environment="env"/>
<property name="play.path" value="${env.PLAY_PATH}"/>
<import file="${play.path}/application-build.xml"/>
</project>
And then run play just by:
ant run
-->
<project>

<property environment="env"/>
<property name="play.path" value="${env.PLAY_PATH}"/>
<property name="application.path" value="${basedir}"/>
<property name="play.id" value=""/>
<property name="precompiled" value="false"/>
<loadfile property="version" srcFile="${play.path}/framework/src/play/version"/>

<!-- classpath including play classes and dependencies -->
<path id="play.classpath">
<fileset dir="${play.path}/framework/lib">
<include name="*.jar"/>
</fileset>
<fileset dir="${play.path}/framework">
<include name="*.jar"/>
</fileset>
</path>

<!-- classpath including application dependencies -->
<path id="application.classpath">
<fileset dir="${basedir}/lib" erroronmissingdir="false">
<include name="*.jar"/>
</fileset>
</path>

<!-- additional classpath for running play in test mode -->
<path id="test.classpath">
<path refid="play.classpath"/>
<fileset dir="${play.path}/modules/testrunner/lib">
<include name="*.jar"/>
</fileset>
</path>

<!-- classpath for firephoque test runner -->
<path id="testrunner.classpath">
<fileset dir="${play.path}/modules/testrunner/lib">
<include name="*.jar"/>
</fileset>
<fileset dir="${play.path}/modules/testrunner/firephoque">
<include name="*.jar"/>
</fileset>
</path>

<!-- macro for the play python script in case you need it
usage example:
<target name="help">
<play-python command="help"/>
</target>
-->
<macrodef name="play-python">
<attribute name="command"/>
<sequential>
<exec executable="cmd.exe" osfamily="winnt">
<arg line="/c ${play.path}/play.bat @{command}"/>
</exec>
<exec executable="python" osfamily="unix">
<arg line="${play.path}/play @{command}"/>
</exec>
</sequential>
</macrodef>

<taskdef classname="play.ant.PlayConfigurationLoadTask" name="playconfload">
<classpath>
<pathelement location="${play.path}/framework/play-${version}.jar"/>
</classpath>
</taskdef>

<target name="run" description="Runs the application">
<playconfload applicationDir="${basedir}" playId="${play.id}"/>
<java classname="play.server.Server" fork="yes" failonerror="yes">
<classpath>
<path refid="play.classpath"/>
<path refid="modules.classpath"/>
<path refid="application.classpath"/>
</classpath>
<jvmarg line="-javaagent:${play.path}/framework/play-${version}.jar -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"/>
<sysproperty key="play.id" value="${play.id}"/>
<sysproperty key="play.debug" value="true"/>
<sysproperty key="precompiled" value="${precompiled}"/>
<sysproperty key="application.path" value="${basedir}"/>
</java>
</target>

<target name="test" description="Run the application in test mode">
<playconfload applicationDir="${basedir}" playId="test"/>
<java classname="play.server.Server" fork="yes" failonerror="yes">
<classpath>
<path refid="test.classpath"/>
<path refid="modules.classpath"/>
<path refid="application.classpath"/>
</classpath>
<jvmarg line="-javaagent:${play.path}/framework/play-${version}.jar -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"/>
<sysproperty key="play.id" value="test"/>
<sysproperty key="play.debug" value="true"/>
<sysproperty key="application.path" value="${basedir}"/>
</java>
</target>

<target name="precompile" description="Compile all java sources and templates">
<playconfload applicationDir="${basedir}" playId="${play.id}"/>
<java classname="play.server.Server" fork="yes" failonerror="yes">
<classpath>
<path refid="play.classpath"/>
<path refid="modules.classpath"/>
<path refid="application.classpath"/>
</classpath>
<jvmarg line="-javaagent:${play.path}/framework/play-${version}.jar"/>
<sysproperty key="application.path" value="${basedir}"/>
<sysproperty key="play.id" value=""/>
<sysproperty key="precompile" value="true"/>
</java>
</target>

<target name="auto-test" description="Automatically run all application tests">

<playconfload applicationDir="${basedir}" playId="test"/>
<property name="application.conf.http.port" value="9000"/>
<property name="application.url" value="http://localhost:${application.conf.http.port}"/>
<get verbose="false" ignoreerrors="true" src="${application.url}/@kill" dest="${basedir}/test-result/[email protected]"/>
<delete dir="${basedir}/tmp"/>
<delete dir="${basedir}/test-result"/>

<property name="server.log" value="${basedir}/logs/auto-test.out"/>
<mkdir dir="${basedir}/logs"/>
<echo message="Starting server and redirecting output to ${server.log}"/>

<parallel>
<java classname="play.server.Server" fork="yes" output="${server.log}" failonerror="true">
<classpath>
<path refid="test.classpath"/>
<path refid="modules.classpath"/>
<path refid="application.classpath"/>
</classpath>
<jvmarg line="-javaagent:${play.path}/framework/play-${version}.jar"/>
<sysproperty key="play.id" value="test"/>
<sysproperty key="application.path" value="${basedir}"/>
</java>

<sequential>
<echo message="Waiting for the server to start up at ${application.url}/@tests"/>
<get verbose="false" ignoreerrors="false" src="${application.url}/@tests"
dest="${basedir}/test-result/[email protected]"/>

<echo message="Testing server at: ${application.url}"/>
<java classname="play.modules.testrunner.FirePhoque" fork="yes" failonerror="yes">
<classpath refid="testrunner.classpath"/>
<sysproperty key="application.url" value="${application.url}"/>
</java>

<echo message="Shutting down server"/>
<get verbose="false" ignoreerrors="true" src="${application.url}/@kill"
dest="${basedir}/test-result/[email protected]"/>
</sequential>
</parallel>

<fail message="There were test failures.">
<condition>
<not>
<resourcecount count="1">
<fileset dir="${basedir}/test-result" includes="result.passed"/>
</resourcecount>
</not>
</condition>
</fail>

</target>

</project>
2 changes: 1 addition & 1 deletion framework/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
</delete>
<antcall target="clean-unittest"/>
</target>

<target name="version" unless="version">
<exec executable="git" outputproperty="gitversion" errorproperty="giterror" failonerror="false" failifexecutionfails="false">
<arg value="describe" />
Expand Down
152 changes: 152 additions & 0 deletions framework/src/play/ant/PlayConfigurationLoadTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package play.ant;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.selectors.FilenameSelector;
import org.apache.tools.ant.util.FileUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
* Ant task which loads settings needed by the ant from the ant configuration file.
*
* These include:
* - Resolving the settings for the given play id and setting them to the ant project properties
* - Creating classpath element for the module libraries
*
*/
public class PlayConfigurationLoadTask {

private Project project;
/** Play id */
private String playId = "";
/** Prefix to use for the properties loaded from the configuration file */
private String prefix = "application.conf.";
/** Id for the classpath element */
private String modulesClasspath = "modules.classpath";
/** Source file to read */
private File applicationDir;

public void setProject(Project project) {
this.project = project;
}

public void setPlayId(String playId) {
this.playId = playId;
}

public void setPrefix(String prefix) {
this.prefix = prefix;
}

public void setApplicationDir(File applicationDir) {
this.applicationDir = applicationDir;
}

public void execute() {
if (applicationDir == null) {
throw new BuildException("No applicationDir set!");
}
File srcFile = new File(applicationDir, "conf/application.conf");
if (!srcFile.exists()) {
throw new BuildException("No application configuration found! " + srcFile.getAbsolutePath());
}
Map<String,String> map = loadAndResolve(srcFile, playId);
for (Map.Entry<String,String> entry: map.entrySet()) {
String key = entry.getKey();
String value = project.replaceProperties(entry.getValue());
project.setProperty(prefix + key, value);
project.log("Loaded property '" + prefix + key + "'='" + value + "'", Project.MSG_VERBOSE);
}

File applicationDir = srcFile.getParentFile().getParentFile();
Path path = new Path(project);

FilenameSelector endsToJar = new FilenameSelector();
endsToJar.setName("*.jar");

for (Map.Entry<String,String> entry : map.entrySet()) {
if (entry.getKey().startsWith("module.")) {
String s = entry.getValue();
s = project.replaceProperties(s);
File moduleDir;
if (!FileUtils.isAbsolutePath(s)) {
moduleDir = new File(new File(applicationDir, "conf"), s);
} else {
moduleDir = new File(s);
}
if (!moduleDir.exists()) {
project.log("Failed add non existing module to classpath! " + moduleDir.getAbsolutePath(), Project.MSG_WARN);
continue;
}
File moduleLib = new File(moduleDir, "lib");
if (moduleLib.exists()) {
FileSet fileSet = new FileSet();
fileSet.setDir(moduleLib);
fileSet.addFilename(endsToJar);
path.addFileset(fileSet);
project.log("Added fileSet to path: " + fileSet, Project.MSG_VERBOSE);
} else {
project.log("Ignoring non existing lib dir: " + moduleLib.getAbsolutePath(), Project.MSG_VERBOSE);
}
}
}
project.addReference(modulesClasspath, path);
project.log("Generated classpath '" + modulesClasspath + "':" + project.getReference(modulesClasspath), Project.MSG_VERBOSE);
}

public static Map<String, String> loadAndResolve(File srcFile, String playId) {
try {
Map<String,String> defaults = new HashMap<String,String>();
Map<String,String> idSpecific = new HashMap<String,String>();
BufferedReader reader = new BufferedReader(new FileReader(srcFile));
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith("#")) {
continue;
}
if (line.startsWith("%")) {
if (playId.length() > 0 && line.startsWith(playId + ".")) {
line = line.substring((playId + ".").length());
String[] sa = splitLine(line);
if (sa != null) {
idSpecific.put(sa[0], sa[1]);
}
} else {
continue;
}
} else {
String[] sa = splitLine(line);
if (sa != null) {
defaults.put(sa[0], sa[1]);
}
}
}
defaults.putAll(idSpecific);
return defaults;
} catch (IOException e) {
throw new BuildException("Failed to load configuration file: " + srcFile.getAbsolutePath(), e);
}
}

private static String[] splitLine(String line) {
int i = line.indexOf("=");
if (i > 0) {
String key = line.substring(0, i);
String value = "";
if (i < line.length()) {
value = line.substring(i+1);
}
return new String[]{key.trim(), value.trim()};
}
return null;
}
}
28 changes: 28 additions & 0 deletions resources/application-skel/build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Usage:
Example 1:
export PLAY_PATH=/home/user/play
ant run
Sets play path to the environment variable PLAY_PATH and then runs play with target run.
Example 2:
ant run -Dplay.path=/home/user/play
Gives the play path to the ant as command line property.
Example 3:
build.xml:
...
<property name="play.path" value="/home/user/play"/>
...
ant run
Sets the play path to the build.xml directly.
-->
<project basedir=".">

<property environment="env"/>
<property name="play.path" value="${env.PLAY_PATH}"/>
<import file="${play.path}/application-build.xml"/>

</project>
2 changes: 1 addition & 1 deletion resources/module-skel/build.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>

<project name="%MODULE%" default="build" basedir=".">

<target name="check" unless="play.path">
<fail message="Please specify Play framework path using -Dplay.path=/path/to/framework/home" />
</target>
Expand Down

0 comments on commit 8cfd80f

Please sign in to comment.