Does grails have an `ant -s` analog? - grails

I'd like to be able to run grails from anywhere in the directory tree.

If there is no such option, you could create a bugreport to introduce such a parameter.
As a workaround, you could create a build.xml wrapping the grails commands in your projects and build your project using this wrapper. Griffon did auto-generate such a wrapper build.xml in early versions. It could look like
<project name="Foo" default="test">
<!-- =================================
target: clean
================================= -->
<target name="clean" description="--> Cleans a Grails application">
<grails>
<arg value="clean"/>
</grails>
</target>
<!-- ... -->
<!-- set up the grails macro -->
<property environment="env"/>
<property name="grails.home" value="${env.GRAILS_HOME}"/>
<property name="jdk.home" value="${env.JAVA_HOME}"/>
<condition property="grails" value="griffon.bat">
<os family="windows"/>
</condition>
<property name="grails" value="grails" />
<macrodef name="grails">
<element name="grails-args" implicit="yes"/>
<sequential>
<exec executable="${grails.home}/bin/${grails}" failonerror="true">
<env key="JAVA_HOME" value="${jdk.home}"/>
<env key="GRAILS_HOME" value="${grails.home}"/>
<grails-args/>
</exec>
</sequential>
</macrodef>
<!-- end set up the grails macro -->
</project>

Related

Build a Jenkins plugin with Ant

My company only uses Ant to build projects. However, Jenkins only suggests Maven as a build tool for plugin development.
How could I package my Jenkins plugin to a .hpi file using Ant and avoiding Maven at all costs?
Here is a way to build a Jenkins plugin using Ant. Let's make a script that builds a plugin skeleton which name is "awesome".
Default plugin arborescence
awesome-plugin/
-- awesome/
-- src/
-- pom.xml
Instructions
Add a lib/ folder which contains the following jars:
to be found in your Jenkins home directory Jenkins\war\WEB-INF\lib (note: you have to use the exact same versions that your current Jenkins use):
access-modifier-annotation-1.4.jar
bridge-method-annotation-1.4.jar
commons-io-1.4.jar
guava-11.0.1.jar
jenkins-core-1.513.jar
json-lib-2.4-jenkins-1.jar
remoting-2.23.jar
sezpoz-1.9.jar
stapler-1.207.jar
to be found on the web (you can choose the last version released):
servlet-api-2.4.jar
Replace the existing pom.xml with the following build.xml.
In the Ant script, you should adapt:
the project name, awesome here,
the plugin version,
the Jenkins version this plugin is made for,
the project group.id (main package), org.jenkinsci.plugins.awesome here.
New plugin arborescence
awesome-plugin/
-- awesome/
-- src/
-- lib/
-- you should have 10 jars here
-- build.xml
build.xml
<!-- Project dependent properties -->
<property name="project_name" value="awesome"/>
<property name="project_version" value="1.0"/>
<property name="jenkins_version" value="1.513"/> <!-- which version of Jenkins is this plugin built against? -->
<property name="project_groupid" value="org.jenkinsci.plugins.awesome"/>
<!-- Build properties -->
<property name="lib_dir" value="./lib"/>
<property name="bin_dir" value="./bin" />
<property name="target_dir" value="./target"/>
<property name="target_bin_dir" value="${target_dir}/${project_name}"/>
<property name="plugin_targetMetaInf_dir" value="${target_bin_dir}/META-INF"/>
<property name="plugin_targetWebInf_dir" value="${target_bin_dir}/WEB-INF"/>
<property name="plugin_targetWebInfBin_dir" value="${plugin_targetWebInf_dir}/classes"/>
<!-- Project paths -->
<path id="project.source.path">
<pathelement path="src/main/java" />
</path>
<path id="project.class.path">
<fileset dir="${lib_dir}" includes="*.jar"/>
</path>
<!-- Build flow -->
<target name="build">
<antcall target="clean" />
<antcall target="compile" />
<antcall target="createTreeDirectory" />
<antcall target="copyBin"/>
<condition property="has_file">
<and>
<available file="${target_dir}/${project_name}.hpi" type="file"/>
</and>
</condition>
<antcall target="createHpi"/>
<condition property="has_dir">
<and>
<available file="${target_bin_dir}" type="dir"/>
</and>
</condition>
<antcall target="cleanTargetDirectory" />
</target>
<!-- Cleans existing binaries -->
<target name="clean">
<delete includeEmptyDirs="true" quiet="true">
<fileset dir="${bin_dir}" />
</delete>
<mkdir dir="${bin_dir}"/>
</target>
<!-- Compiles JAVA code -->
<target name="compile">
<javac includeantruntime="false" destdir="${bin_dir}" debug="false" optimize="${optimize}" deprecation="${deprecation}" classpathref="project.class.path">
<src refid="project.source.path" />
</javac>
</target>
<!-- Creates necessary target folders -->
<target name="createTreeDirectory" >
<mkdir dir="${target_bin_dir}"/>
<mkdir dir="${plugin_targetMetaInf_dir}"/>
<mkdir dir="${plugin_targetWebInf_dir}"/>
<mkdir dir="${plugin_targetWebInfBin_dir}"/>
</target>
<!-- Moves new binaries to the plugin target -->
<target name="copyBin">
<copy todir="${plugin_targetWebInfBin_dir}" >
<fileset dir="${bin_dir}"/>
<fileset dir="src/main/resources"/>
</copy>
</target>
<!-- Cleans the target directory -->
<target name="cleanTargetDirectory" if="has_dir">
<delete dir="${target_bin_dir}"/>
</target>
<!-- Backup previous plugin -->
<target name="saveOldHpiFile" if="has_file">
<move file="${target_dir}/${project_name}.hpi" tofile="${target_dir}/${project_name}.save.hpi"/>
</target>
<!-- Archives the plugin -->
<target name="createHpi">
<antcall target="saveOldHpiFile"/>
<jar destfile="${target_dir}/${project_name}.hpi" basedir="${target_bin_dir}">
<manifest>
<attribute name="Manifest-Version" value="{project_version}"/>
<attribute name="Built-By" value="${user.name}"/>
<attribute name="Created-By" value="${user.name}"/>
<attribute name="Build-Jdk" value="${ant.java.version}"/>
<attribute name="Extension-Name" value="${project_name}"/>
<attribute name="Implementation-Title" value="${project_name}"/>
<attribute name="Implementation-Version" value="${version}"/>
<attribute name="Group-Id" value="${project_groupid}"/>
<attribute name="Short-Name" value="${project_name}"/>
<attribute name="Long-Name" value="${project_name}"/>
<attribute name="Plugin-Version" value="${project_version}"/>
<attribute name="Jenkins-Version" value="${jenkins_version}"/>
<attribute name="Hudson-Version" value="${jenkins_version}"/>
</manifest>
</jar>
</target>
To launch the build, cd towards the build.xml and type ant.
I know you stated "at all costs", but a compromise might be less effort, and still give super fast builds. A big reason for me to try to avoid maven is that the compile time is sloooowwwwww. That said, maven is quite good at creating the hpl file, and handling dependencies. The following targets are quite useful for helping to set up a super-fast non-maven build:
use 'mvn hpi:hpl' to generate the hpl file
use 'mvn dependency:copy-dependencies' to download all your dependencies, and put them into target/dependency, where it's easy to reference them from your ant script (you can add a symbolic link if necessary, from lib to target/dependency)

TestNG test-output folder getting generated on Desktop when expected to be in project folder

I have a setup of Selenium WebDriver + TestNG + Ant framework in my automation project. Running webdriver + TestNG tests from Ant using build.xml was working absolutely fine a few months ago. TestNG was generating the test-output folder in the project directory as expected. Now when I run my testng tests from ANT it's generating the default report folder test-output on my Desktop (home/user/Desktop). I don't know why it is happening.
This is my build.xml file:
<project name="InitialConfigProject" default="start" basedir=".">
<!-- ========== Initialize Properties =================================== -->
<property environment="env"/>
<property file="./app.properties"/>
<property name="ws.home" value="${basedir}"/>
<property name="test.dest" value="${ws.home}/build"/>
<property name="test.src" value="${ws.home}/src"/>
<property name="browser" value="/usr/bin/google-chrome"/>
<property name="mail_body_file" value="${basedir}/email_body.txt"/>
<property name="buildID" value="IND3.2.0"/>
<property name="sendmailscript_path" value="${basedir}/sendmail.sh"/>
<property name="mail_subject" value="Automated_test_execution_of_${buildID}"/>
<!-- ====== Set the classpath ==== -->
<target name="setClassPath" unless="test.classpath">
<path id="classpath_jars">
<fileset dir="${ws.home}/lib" includes="*.jar"/>
</path>
<pathconvert pathsep=":" property="test.classpath" refid="classpath_jars"/>
</target>
<!-- ============ Initializing other stuff =========== -->
<target name="init" depends="setClassPath">
<tstamp>
<format property="timestamp" pattern="dd/MM/yyyy hh:mm aa" />
</tstamp>
<!--
<condition property="ANT"
value="${env.ANT_HOME}/bin/ant.bat"
else="${env.ANT_HOME}/bin/ant">
<os family="windows" />
</condition> -->
<property name="build.log.dir" location="${basedir}/buildlogs" />
<mkdir dir="${build.log.dir}"/>
<property name="build.log.filename" value="build_${timestamp}.log"/>
<record name="${build.log.dir}/${build.log.filename}" loglevel="verbose" append="false"/>
<echo message="build logged to ${build.log.filename}"/>
<echo message="Loading TestNG.." />
<taskdef name="testng" classpath="${test.classpath}" classname="org.testng.TestNGAntTask" />
</target>
<!-- cleaning the destination folders -->
<target name="clean">
<delete dir="${test.dest}"/>
</target>
<!-- compiling files -->
<target name="compile" depends="init, clean" >
<delete includeemptydirs="true" quiet="true">
<fileset dir="${test.dest}" includes="**/*"/>
</delete>
<echo message="making directory..."/>
<mkdir dir="${test.dest}"/>
<copy file="${ws.home}/app.properties" todir="${ws.home}/build" />
<copy file="${ws.home}/resources/testdata/testDataSet1.properties" todir="${ws.home}/build" />
<echo message="compiling source files..."/>
<javac
debug="true"
destdir="${test.dest}"
srcdir="${test.src}"
target="1.6"
classpath="${test.classpath}"
includeantruntime="true"
>
</javac>
</target>
<!-- run -->
<target name="run" depends="compile">
<testng outputdir="${ws.home}/test-output" classpath="${test.classpath}:${test.dest}" suitename="Praxify Sanity Suite">
<xmlfileset dir="${ws.home}" includes="testng.xml"/>
</testng>
</target>
<!-- ========== Generating reports using XSLT utility ============== -->
<target name="testng-xslt-report">
<delete dir="${basedir}/testng-xslt">
</delete>
<mkdir dir="${basedir}/testng-xslt">
</mkdir>
<xslt in="${basedir}/test-output/testng-results.xml" style="${basedir}/testng-results.xsl" out="${basedir}/testng-xslt/index.html"
processor="SaxonLiaison">
<param expression="${basedir}/testng-xslt/" name="testNgXslt.outputDir" />
<param expression="true" name="testNgXslt.sortTestCaseLinks" />
<param expression="FAIL,SKIP,PASS,CONF,BY_CLASS" name="testNgXslt.testDetailsFilter" />
<param expression="true" name="testNgXslt.showRuntimeTotals" />
<classpath refid="classpath_jars"></classpath>
</xslt>
</target>
<!-- Starting point of the execution, should be dependent on target "run".
Target sequence will be:
start (not_execute) ==> run (not_execute) ==> compile (not_execute) ==> init (execute) ==> clean (execute)
start (execute) <== testng-xslt-report (execute) <== run (execute) <== compile (execute) <==
Suitable for ANT 1.7. Currently using this ====================== -->
<target name="start" depends="run, testng-xslt-report">
<tstamp prefix="getTime">
<format property="TODAY" pattern="MM-dd-yyyyhhmmaa"/>
</tstamp>
<echo message="sending report as mail...."/>
<property name="execution_time" value="${buildID}_${getTime.TODAY}"/>
<property name="dest_file" value="/home/xtremum/Reports/${execution_time}.zip"/>
<zip destfile="/home/xtremum/Reports/${execution_time}.zip" basedir="${basedir}/testng-xslt"/>
<property name="report_attachment_file" value="${dest_file}"/>
<exec executable="${sendmailscript_path}" newenvironment="false">
<arg value="${mail_subject}"/>
<arg value="${mail_recipient}"/>
<arg value="${report_attachment_file}"/>
<arg value="${mail_body_file}"/>
</exec>
</target>
Just for the record:
1. I am using Eclipse Juno.
2. I have installed TestNG plugin on Eclipse so that I can run tests directly from eclipse by right clicking on testng.xml and going for Run.
3. I have installed ANT 1.7 on my Ubuntu machine and have set my ANT_HOME pointing to /usr/share/ant. And I looked up in Windows -> Preferences -> Ant -> Runtime -> Ant Home Entries (Default) and they seem to have references to ant 1.8.3 libraries (JARS) which are inside the Eclipse package (eclipse/plugins/). Is there anything wrong here?
4. I am running the tests via ant from eclipse and not from command line.
I am not getting any build errors. Tests are getting executed but the test-output folder is getting created on Desktop. Any help?
If you are running through the testng plugin option, the output folder would be the one you specify in Project->Properties->TestNG->OutputDirectory

ant build script don't run on windows using command line

i have written an ant script, which runs ok and generate the .jar file when i use it with eclipse.
But when i use it on command prompt on windows xp, it's shows successfull, but nothing happens. ant is properly configured and also i can run other ant scripts.
here is my build.xml file
<?xml version="1.0"?>
<project name="TaskNodeBundle" basedir=".">
<!-- Sets variables which can later be used. -->
<!-- The value of a property is accessed via ${} -->
<property name="bundlename" value="task-node-bundle" />
<property name="src.dir" location="../src" />
<property name="lib.dir" location="../lib" />
<property name="build.dir" location="/buildoutput" />
<property name="build.dest" location="build/dest" />
<!--
Create a classpath container which can be later used in the ant task
-->
<path id="classpath">
<fileset dir="${lib.dir}/">
<include name="*.jar" />
</fileset>
</path>
<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${build.dest}" />
</target>
<!-- Deletes the existing build directory-->
<target name="mkdir" depends="clean">
<mkdir dir="${build.dest}"/>
</target>
<!-- Compiles the java code -->
<target name="compile" depends="mkdir">
<javac srcdir="${src.dir}" destdir="${build.dest}" classpathref="classpath" />
</target>
<target name="package-bundle" depends="compile" description="Generates the bundle">
<jar destfile="${dist.dir}/${bundlename}.jar">
<fileset dir="${src.dir}">
<include name="**/**.class" />
<include name="**/**.properties"/>
<include name="/META-INF/**.*" />
<include name="/META-INF/spring/**.*" />
</fileset>
</jar>
</target>
</project>
When you execute an ant script from the command line it will execute the first target defined in the build.xml file (in your case clean).
You can specify the target(s) to be executed on the command line
$ ant target1 target2
or define a default target in your build.xml file with the default attribute of the <project> tag:
<project name="TaskNodeBundle" basedir="." default="package-bundle">

Ant build.xml phpunit

I have a ant build.xml (below). I am able to run phpunit fine from the command line as follows:
D:> phpunit --verbose --testdox-html logs\today.html runtest
This runs all my phpunit tests within the folder d:\runtest.
My problem is when I run my build.xml as 'ant build' it tries to execute a file called runtest.php the output from ant is below:
D:\>ant build
Buildfile: D:\build.xml
check_os:
if_windows:
if_unix:
prepare:
phpunit:
[exec] PHPUnit 3.6.11 by Sebastian Bergmann.
[exec]
[exec] Cannot open file "runtest.php".
BUILD FAILED
D:\build.xml:48: exec returned: 1
Total time: 2 seconds
My Build.xml is as follows:
<!-- This project launches the test generator and execute all phpunit selenium tests -->
<project name="proj" default="build" basedir="">
<!--Get environment variables -->
<property environment="env" />
<property name="logFolder" value="${basedir}\logs"/>
<property name="testFolder" value="${basedir}\runtest"/>
<property name="test" value="**" />
<condition property="pattern" value="runtest/*.php">
<os family="windows" />
</condition>
<tstamp/>
<!-- Check Operating system to set phpunit path-->
<target name="check_os">
<condition property="isWindows">
<os family="windows" />
</condition>
<condition property="isLinux">
<os family="unix" />
</condition>
</target>
<target name="if_windows" depends="check_os" if="isWindows">
<property name="exe.phpunit" value="C:\\Program Files\\PHP\\phpunit.bat"/>
</target>
<target name="if_unix" depends="check_os" if="isLinux">
<property name="exe.phpunit" value="${env.PHP_HOME}/includes/PHPUnit-3.2.0/PHPUnit" />
</target>
<target name="prepare" depends="if_windows, if_unix">
<mkdir dir="${logFolder}"/>
</target>
<target name="phpunit">
<!-- Check if folder empty -->
<fileset id="fileset.test" dir="${testFolder}">
<include name="*.*"/>
</fileset>
<fail message="Files not found">
<condition>
<resourcecount refid="fileset.test" when="less" count="1"></resourcecount>
</condition>
</fail>
<!-- Execute phpunit tests -->
<exec executable="${exe.phpunit}" failonerror="true" dir="runtest">
<arg line="--verbose --testdox-html '${logFolder}\phpunit-report-${TODAY}.html' runtest" />
</exec>
</target>
<target name="build" depends="prepare,phpunit"/>
</project>
The problem was I specified dir="runtest" once I removed this from the Execute line it worked.
<target name="phpunit" unless="phpunit.done" depends="prepare" description="Run unit tests with PHPUnit">
<exec executable="${phpunit}" failonerror="true" dir="${basedir}/classes/tests/" resultproperty="result.phpunit">
${phpunit} - needs Path of phpunit installation, you can get it from where is phpunit command in linux
dir="${basedir}/classes/tests/" - Need only the folder path where your php application is present
<arg line="UserTest ${basedir}/classes/tests/userTest.php" />
line="UserTest ${basedir}/classes/tests/userTest.php" - Here UserTest is class name and ${basedir}/classes/tests/userTest.php it is path of test class file
<arg value="--configuration"/>
<arg path="${basedir}/classes/tests/UnitTest.xml"/>
path="${basedir}/classes/tests/UnitTest.xml" - Path of xml file
</exec>
<property name="phpunit.done" value="true"/>
</target>

Javadoc errors when building Ant project

I am trying to write a build.xml file for my project. When I run build.xml as an Ant project, I get the following error:
D:\workspace\LogAlerter\src\com\j32bit\alerter\launcher\LogAlerter.java:9:
error: package org.apache.log4j does not exist
[javadoc] import org.apache.log4j.Logger;
I have imported log4j in LogAlerter.Java. Here is my build.xml file:
<?xml version="1.0"?>
<project name="LogAlerter" default="main" basedir=".">
<!-- Sets variables which can later be used. -->
<!-- The value of a property is accessed via ${} -->
<property name="src.dir" location="src" />
<property name="build.dir" location="build" />
<property name="dist.dir" location="dist" />
<property name="docs.dir" location="docs" />
<property name="libs.dir" location="lib" />
<!--
Create a classpath container which can be later used in the ant task
-->
<path id="build.classpath">
<fileset dir="${libs.dir}">
<include name="**/*.jar" />
</fileset>
</path>
<!-- Deletes the existing build, docs and dist directory-->
<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${docs.dir}" />
<delete dir="${dist.dir}" />
</target>
<!-- Creates the build, docs and dist directory-->
<target name="makedir">
<mkdir dir="${build.dir}" />
<mkdir dir="${docs.dir}" />
<mkdir dir="${dist.dir}" />
</target>
<!-- Compiles the java code (including the usage of library for JUnit -->
<target name="compile" depends="clean, makedir" >
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="build.classpath" includeantruntime="false">
</javac>
</target>
<!-- Creates Javadoc -->
<target name="docs" depends="compile">
<javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
<!-- Define which files / directory should get included, we include all -->
<packageset dir="${src.dir}" defaultexcludes="yes">
<include name="**" />
</packageset>
</javadoc>
</target>
<!--Creates the deployable jar file -->
<target name="jar" depends="compile">
<jar destfile="${dist.dir}\LogAlerter.jar" basedir="${build.dir}">
<manifest>
<attribute name="Main-Class" value="LogAlerter.Main" />
</manifest>
</jar>
</target>
<target name="main" depends="compile, jar, docs">
<description>Main target</description>
</target>
</project>
Try adding a classpath ref to your javadoc task:
<javadoc packagenames="src"
sourcepath="${src.dir}"
destdir="${docs.dir}"
classpathref="build.classpath">
What the warning is telling you is that you've not provided the full classpath to the javadoc task. Try adding a similar classpath ref to that in your compile task and see where that leads.
Importing is fine but make sure it is available at run time for the JavaDoc tool. log4j.jar should be present in your build.classpath.
Make use of the classpathref inside the docs target like so:
<javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}" classpathref="build.classpath">

Resources