Apache Ant output using only log4j - ant

What I'm trying to achieve is to have log4j as the only output from my Ant build file.
Getting log4j works pretty much perfectly except that it extends Ants output. So I'm getting Ant's DefaultLogger interspersed with log4j output. Take a VERY simple Ant build file:
<project name="Maintenance_to_Delivery" default="main" basedir=".">
<target name="main">
<echo message="test">
</target>
</project>
And log4j.properties
log4j.rootLogger=ERROR, stdout
log4j.logger.org.apache.tools.ant.Project=INFO
log4j.logger.org.apache.tools.ant.Target=INFO
log4j.logger.org.apache.tools.ant.taskdefs=INFO
log4j.logger.org.apache.tools.ant.taskdefs.Echo=INFO
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss} %-5p %m%n
log4j.appender.stdout.Threshold=INFO
And running this plus the output:
# ant -lib /app/apache-sf/log4j/1.2.16/log4j-1.2.16.jar:. -listener org.apache.tools.ant.listener.Log4jListener
Buildfile: /home/sysswb/working/qa/sep/M2D-new/build.xml
16:36:59 INFO Build started.
main:
16:37:00 INFO Target "main" started.
[echo] TEST
16:37:00 WARN TEST
16:37:00 INFO Target "main" finished.
BUILD SUCCESSFUL
Total time: 1 second
16:37:00 INFO Build finished
so - is it possible to get log4j to completely replace the DefaultLogger output? ...without writing a custom logger? Since the -listener argument registers extra listeners, can whatever listener the DefaultLogger uses be removed/disabled?
I'm just Java 1.6.0_18 and Ant 1.8.0.
TIA.

You can pass log4j.properties in the class path while running the ant.
Check out this, it has much detailed explanation, follow comment by "Kevin", here is the excerpt:
Log4j loads its configuration by
looking for the system property
log4j.configuration. Failing that, it
looks for log4j.properties or
log4j.xml on the classpath.

try using the -e switch
ant -lib . -listener org.apache.tools.ant.listener.Log4jListener -e main
Buildfile: D:\install\apache-ant-1.8.0RC1-bin\apache-ant-1.8.0RC1\bin\build.xml
14:29:20 INFO Build started.
main:
14:29:20 INFO Target "main" started.
test
14:29:20 WARN test
14:29:20 INFO Target "main" finished.
BUILD SUCCESSFUL
Total time: 0 seconds
14:29:20 INFO Build finished.
[echo] test is gone

Related

Having issues creating a report.xml file for QUnit + PhantomJS + Jenkins

I've been trying to get Jenkins to display a JUnit report of a sample js project which i am testing with QUnit. I have literally scoured the internet for bits and pieces and so far, Running QUnit tests with Jenkins and Apache Ant? is the most helpful post that i have found.
I can confirm that:
The user has sufficient privileges to write to disk
PhantomJS works headless from the shell when i write something along the lines of:
[user#myserver PhantomJS]$ phantomjs phantomjs-runner/runner.js web/index.html
And shows:
Took 8ms to run 6 tests. 6 passed, 0 failed.
Qunit does work and provides test results when executed in a browser
Still i cannot get the report.xml to generate in order to feed it into Jenkins. Below is the target i have added to my build.xml file:
<target name="qunit" description="runs QUnit tests using PhantomJS">
<!-- QUnit Javascript Unit Tests -->
<echo message="Executing QUnit Javascript Unit Tests..."/>
<apply executable="/usr/local/CI/phantomjs/bin/phantomjs" >
<arg value="/usr/local/CI/phantomjs-runner/runner.js" />
<arg line="--qunit /usr/local/CI/jenkins/workspace/PhantomJS/web/js/qunit-1.17.1.js --tests /usr/local/CI/jenkins/workspace/PhantomJS/web/index.html --junit /usr/local/CI/jenkins/workspace/PhantomJS/test-results/report.xml" />
<fileset dir="${basedir}/web/" includes="/js/prettydate.js" />
<srcfile/>
</apply>
<echo message="Tests complete..."/>
</target>
Compiling the project in Jenkins gives me the following output:
? PhantomJS/result.xml
? PhantomJS/test-results
Using locally configured password for connection to :pserver:user#server:/cvsroot
cvs rlog -S -d18 Feb 2015 15:49:54 +0000<18 Feb 2015 15:51:40 +0000 QUnit_Jenkins
[PhantomJS] $ /usr/local/CI/ant/bin/ant qunit
Buildfile: /usr/local/CI/jenkins/workspace/PhantomJS/build.xml
qunit:
[echo] Executing QUnit Javascript Unit Tests...
[echo] Tests complete...
BUILD SUCCESSFUL Total time: 0 seconds Recording test results Test
reports were found but none of them are new. Did tests run? For
example,
/usr/local/CI/jenkins/workspace/PhantomJS/test-results/report.xml is 4
hr 18 min old
Build step 'Publish JUnit test result report' changed build result to
FAILURE Finished: FAILURE
As you may notice, jenkins can't find an updated report.xml file because there simply isn't one getting generated.
Can you observe any mistakes in my build.xml? If not, any ideas, hints that would assist me in getting the result.xml file generated?
I have found a solution to my answer by taking the following steps:
1) added <script src="js/qunit-reporter-junit.js"></script> as it is required to generate the report. Ensure you also have the qunit.js library included also. I used qunit-1.17.1.js
2) I placed the following code in the html file that tests my js code:
<script>
QUnit.jUnitReport = function(report) {
console.log(report.xml)
};
</script>
3) I added the Ant code in my build.xml file:
<target name="build" description="runs QUnit tests using PhantomJS">
<!-- Clean up output directory -->
<delete dir="./build/qunit"/>
<mkdir dir="./build/qunit"/>
<!-- QUnit Javascript Unit Tests -->
<echo message="Executing QUnit Javascript Unit Tests..."/>
<exec executable="/usr/local/CI/phantomjs/bin/phantomjs" output="./build/qunit/qunit-results.xml">
<arg value="/usr/local/CI/phantomjs-runner/runner-muted.js"/>
<arg value="./web/index.html"/>
</exec>
</target>
You will observe that i changed the name of runner.js to runner-muted.js This is so, because i have made changes to runner.js to not include its output to the xml file, as this makes it unreadable by jenkins. To do so:
cp /usr/local/CI/phantomjs-runner/runner.js /usr/local/CI/phantomjs-runner/runner-muted.js
Find and comment out console.log occurrences found under QUnit.done and QUnit.testDone to mute the runner from displaying its own test run results.
Ensure that in Jenkins you have selected the correct path to the generated xml file.
I hope this helps any of you trying to get it to work

TeamCity deploy to Tomcat with Ant fails

I'm running TeamCity 7 on Ubuntu 12, and I'm trying to deploy a war file to a tomcat server on machine with IP x using Ant.
The thing is - this worked on a different machine with TeamCity 7, and the only thing I've changed is the machine (moved to Ubuntu running on KVM), and the TC version (upgrade).
I've set ANT_HOME to the correct location, and I see the TC is using it in the build log:
... -Dant.home=/usr/share/ant ...
I've added the following jars to my ANT_HOME/lib:
catalina-ant, tomcat-coyote, tomcat-juli, tomcat-util
The build is running on an agent which has the default Ant settings
My ant file looks like this:
<target name="deploy.to.server">
<property name="port" value="${tomcat.port}"/>
<property name="tomcat.manager" value="manager/text"/>
<property name="url" value="http://${tomcat.server}:${port}/${tomcat.manager}"/>
<property name="path" value="/${server.name}"/>
<echo message="Deploying application to ${url}"/>
<antcall target="undeploy.from.tomcat"/>
<sleep seconds="3"/>
<antcall target="deploy.to.tomcat"/>
</target>
<taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask"/>
<taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask"/>
<target name="deploy.to.tomcat" description="Install web application">
<deploy url="${url}" username="${username}" password="${password}"
path="${path}" war="${work.dir}/${path}.war"/>
</target>
<target name="undeploy.from.tomcat" description="Remove web application">
<undeploy url="${url}" username="${username}" password="${password}"
path="${path}"/>
</target>
I've got this in the TC log:
[Step 1/2] Starting: /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java -Dagent.home.dir=/home/system/dev/TeamCity/buildDeployAgent -Dagent.name=Deploy Agent -Dagent.ownPort=9091 -Dagent.work.dir=/home/system/dev/TeamCity/buildDeployAgent/work -Dant.home=/usr/share/ant -Dbuild.number=131 -Dbuild.vcs.number.Nutrino_Monitor_sources=588 -Dbuild.vcs.number.Nutrino_build_scripts=590 -Dfile.encoding=ANSI_X3.4-1968 -Dfile.separator=/ -Djava.io.tmpdir=/home/system/dev/TeamCity/buildDeployAgent/temp/buildTmp -Dos.arch=amd64 -Dos.name=Linux -Dos.version=3.5.0-19-generic -Dpath.separator=: -Dteamcity.agent.cpuBenchmark=684 -Dteamcity.agent.dotnet.agent_url=http://localhost:9091/RPC2 -Dteamcity.agent.dotnet.build_id=45574 -Dteamcity.auth.password=mlTjdmhOxwfxuM6vGfcQPsKg81q29rFU -Dteamcity.auth.userId=TeamCityBuildId=45574 -Dteamcity.build.changedFiles.file=/home/system/dev/TeamCity/buildDeployAgent/temp/buildTmp/changedFiles7524737972530602224.txt -Dteamcity.build.checkoutDir=/home/system/dev/TeamCity/Builds/DeployNutritionServer -Dteamcity.build.id=45574 -Dteamcity.build.properties.file=/home/system/dev/TeamCity/buildDeployAgent/temp/buildTmp/teamcity.build3049879068391711216.properties -Dteamcity.build.tempDir=/home/system/dev/TeamCity/buildDeployAgent/temp/buildTmp -Dteamcity.build.workingDir=/home/system/dev/TeamCity/Builds/DeployNutritionServer -Dteamcity.buildConfName=Deploy to Integration -Dteamcity.buildType.id=bt38 -Dteamcity.configuration.properties.file=/home/system/dev/TeamCity/buildDeployAgent/temp/buildTmp/teamcity.config301718488736388101.properties -Dteamcity.projectName=Nutrition Builds -Dteamcity.runner.properties.file=/home/system/dev/TeamCity/buildDeployAgent/temp/buildTmp/teamcity.runner1078243400245029410.properties -Dteamcity.runtime.props.file=/home/system/dev/TeamCity/buildDeployAgent/temp/agentTmp/ant7992360137092769527runtime -Dteamcity.tests.recentlyFailedTests.file=/home/system/dev/TeamCity/buildDeployAgent/temp/buildTmp/testsToRunFirst5600043147441131768.txt -Dteamcity.version=7.1.2 (build 24170) -Dtomcat.server=integration -Duser.country=US -Duser.home=/home/system -Duser.language=en -Duser.name=system -Duser.timezone=Asia/Jerusalem -Dwork.dir=/home/system/dev/TeamCity/Builds/DeployNutritionServer -classpath /usr/share/java/ant-launcher-1.8.2.jar org.apache.tools.ant.launch.Launcher -lib /home/system/dev/TeamCity/buildDeployAgent/plugins/antPlugin/ant-runtime.jar:/home/system/dev/TeamCity/buildDeployAgent/lib/runtime-util.jar -listener jetbrains.buildServer.agent.ant.AgentBuildListener -buildfile /home/system/dev/TeamCity/Builds/DeployNutritionServer/build/deploy.xml deploy.nutrino.server
[15:06:21][Step 1/2] in directory: /home/system/dev/TeamCity/Builds/DeployNutritionServer
[15:06:21][Step 1/2] taskdef
[15:06:21]
[taskdef] taskdef class org.apache.catalina.ant.DeployTask cannot be found
using the classloader AntClassLoader[]
[15:06:21]
[Step 1/2] The following error occurred while executing this line:
/home/system/dev/TeamCity/Builds/DeployNutritionServer/build/tomcat.tasks.xml:9: taskdef class org.apache.catalina.ant.DeployTask cannot be found
using the classloader AntClassLoader[]
[15:06:21][Step 1/2] Process exited with code 1
[15:06:21][Step 1/2] Ant output
[15:06:21][Ant output] Buildfile: /home/system/dev/TeamCity/Builds/DeployNutritionServer/build/deploy.xml
[15:06:21][Ant output]
[15:06:21][Ant output] BUILD FAILED
[15:06:21][Ant output] /home/system/dev/TeamCity/Builds/DeployNutritionServer/build/deploy.xml:8: The following error occurred while executing this line:
[15:06:21][Ant output] /home/system/dev/TeamCity/Builds/DeployNutritionServer/build/tomcat.tasks.xml:9: taskdef class org.apache.catalina.ant.DeployTask cannot be found
[15:06:21][Ant output] using the classloader AntClassLoader[]
[15:06:21][Ant output]
[15:06:21][Ant output] Total time: 0 seconds
[15:06:22][Step 1/2] Step Deploy (Ant) failed
Now, this is extremely weird, as when I run this from the command line it works perfectly:
ant -buildfile deploy.xml -Dtomcat.server= ...
There is no apparent difference between the two - running it from the CL and through the TC agent should both be running Ant from ANT_HOME (usr/share/ant), and loading the libraries in $ANT_HOME/lib.
Any help would be appreciated.
:)
Thanks!
Just answering the question,
Thanks to #timomeinen - adding the catalina-ant.jar to $ANT_HOME/lib did the job.
That means it is not seeing the lib catalina-ant.jar
You can add:
classname="org.apache.catalina.ant.DeployTask"
classpath="C:\Programming\Tomcat\Instances\8080\lib\catalina-ant.jar"
It will solve this reference problem, but I guess you will face similar problems in the future with other libraries, so in the long term you will still need solve the reference problem.

How to determine Jenkins build directory from Ant?

I am trying to migrate an Ant script I wrote to build and deploy projects from within the Jenkins framework (instead of triggered from an SVN post-commit hook, which was the expedient way we initially approached things). Everything is great, except I need to stage files for the deploy step and I want to stuff them into the 'build' directory Jenkins creates for the job (and since my build.xml lives in a non-project-specific location, ${basedir} and ${user.dir} do not point to the desired location).
within the Jenkins configuration, I've setup the following:
[Jenkins]
Build Record Root Directory: E:/builds/${ITEM_FULLNAME}
[Job-Specific]
Build File: C:\vc-tools\shadow\build.xml
when running a build, the script is appropriately launched and a job-specific build directory is created, e.g.
E:\builds\Test\2012-08-07_12-51-21
I want to get at this directory from within the build script, but cannot figure out how. some of the things I've tried:
[echo] ${basedir}: C:\vc-tools\shadow
[echo] ${user.dir}: C:\vc-tools
[echo] ${env.workspace}: C:\Program Files (x86)\Jenkins\workspace\Test
[echo] ${env.build_id}: 2012-08-07_12-51-21
[echo] ${jenkins_home}: C:\Program Files (x86)\Jenkins
[echo] ${BuildDir}: E:/builds/${ITEM_FULLNAME}
note: for that last one, I tried passing in:
BuildDir=E:/builds/${ITEM_FULLNAME}
as a property configured from the job within Jenkins (clearly ${} expansion doesn't take place in this context).
according to the documentation, there are no specific environment variables that are set to the full build directory path -- I can fudge it by hardcoding the E:\builds root and tacking on ${env.build_id}, but was hoping there would be an easier way to access the complete path from something Jenkins exposes (either an Ant property and an environment variable) in order to make the script more flexible.
I am using Jenkins version 1.476.
thanks
It's always a good idea for your project to have a copy of it's build logic included alongside the source code. It makes your build more portable across machines.
Having said that it's also quite common to setup build files containing common shared build logic. ANT defines the following tasks to support such activity:
include
import
So a possible solution is to store a simple build.xml file, in the root of your project directory:
<project name="my project" default="build">
<include file="C:\vc-tools\shadow\common-build-1.0.xml" as="common"/>
<target name="build" depends="common.build"/>
</project>
Notes:
It's a good idea to use a revision number in the common build file name. This assists in preserving backward compatibility with other builds using the older logic.
Update
When Jenkins runs a job is sets a number of environment variables.
The following ANT logic will print the location of the Jenkins workspace directory:
<property environment="env"/>
<target name="run">
<echo message="Jenkins workspace: ${env.WORKSPACE}"/>
<echo message="Job directory: ${env.WORKSPACE}../../jobs/${env.JOB_NAME}"/>
<echo message="Build data: ${env.WORKSPACE}../../jobs/${env.JOB_NAME}/build/${env.BUILD_ID}"/>
</target>
These days (Jenkins v. 1.484) 'run' target from answer above should look like this:
<target name="run">
<echo message="Jenkins workspace: ${env.WORKSPACE}"/>
<echo message="Job directory: ${env.WORKSPACE}/../../${env.JOB_NAME}"/>
<echo message="Build data: ${env.WORKSPACE}/../../${env.JOB_NAME}/builds/${env.BUILD_ID}"/>
</target>

Could not create task or type: getProjectData from Ant

I am trying to run an Ant task from within IBM RSA IDE using Ant build ...
I get the following error message:
BUILD FAILED
build.xml:21: Could
not create task or type of type: getProjectData.
Ant could not find the task or a class this task relies upon.
This is common and has a number of causes; the usual
solutions are to read the manual pages then download and
install needed JAR files, or fix the build file:
- You have misspelt 'getProjectData'.
Fix: check your spelling.
- The task needs an external JAR file to execute
and this is not found at the right place in the classpath.
Fix: check the documentation for dependencies.
Fix: declare the task.
- The task is an Ant optional task and the JAR file and/or libraries
implementing the functionality were not found at the time you
yourself built your installation of Ant from the Ant sources.
Fix: Look in the ANT_HOME/lib for the 'ant-' JAR corresponding to the
task and make sure it contains more than merely a META-INF/MANIFEST.MF.
If all it contains is the manifest, then rebuild Ant with the needed
libraries present in ${ant.home}/lib/optional/ , or alternatively,
download a pre-built release version from apache.org
- The build file was written for a later version of Ant
Fix: upgrade to at least the latest release version of Ant
- The task is not an Ant core or optional task
and needs to be declared using <taskdef>.
- You are attempting to use a task defined using
<presetdef> or <macrodef> but have spelt wrong or not
defined it at the point of use
Remember that for JAR files to be visible to Ant tasks implemented
in ANT_HOME/lib, the files must be in the same directory or on the
classpath
Please neither file bug reports on this problem, nor email the
Ant mailing lists, until all of these causes have been explored,
as this is not an Ant bug.
Here's the Ant buildfile:
<!-- Get property locationName. -->
<target name="config">
<echo message="${ear.project.name}" />
<getProjectData projectName="${ear.project.name}" />
</target>
I am not quite sure what the problem is here because the error message seems not helpful. Any suggestions?
I believe getProjectData is an IBM extension to ant. Like you, I had a similar error, but I was able to get it working after ensuring the Run in the same JRE as the workspace option was enabled (which you can find by right-clicking the build file, run-as, Ant Build..., and selecting the option on the JRE tab).
I discovered the solution on the IBM info center:
The Run in the same JRE as the workspace option enables the classpath
of the workbench to access the additional Ant tasks that perform
operations that are specific to the workbench, such as projectImport,
projectBuild, workspaceBuild, ejbDeploy, or earExport. If your Ant
build script uses any Ant tasks that perform workbench operations,
verify that you selected the Run in the same JRE as the workspace
option; otherwise you might get the following error message in the
Console view:
Problem: failed to create task or type <Ant task> Cause:
The name is undefined.
The build file I used looked like this:
<?xml version="1.0"?>
<project name="Test" default="config" basedir=".">
<target name="config">
<getProjectData Basedir="${basedir}" />
<echo message="getProjectData: projectName=${projectName}
nature=${natureName}
workspace=${workspaceName}
basedir=${basedir}" />
</target>
</project>
And output:
Buildfile: C:\DATA\java\workspace\test-java\build.xml
config:
[getProjectData] Setting projectName=test-java
[getProjectData] Retrieved following Project Data :
[getProjectData] workspaceName=C:\DATA\java\workspace
[getProjectData] natureName=Java
[echo] getProjectData: projectName=test-java
nature=Java
workspace=C:\DATA\java\workspace
basedir=C:\DATA\java\workspace\test-java
BUILD SUCCESSFUL
Total time: 78 milliseconds

ant script taskdef and classname not found

i am not very familar with taskdef and classname... i am having trouble running an ant script from another ant script:
2011-05-25 15:50:26,040 [Thread-23] WARN ScriptRunner - BUILD FAILED
2011-05-25 15:50:26,040 [Thread-23] WARN ScriptRunner - C:\Work\6.70_Extensions\NightlyBuild\nightly_build.xml:31: The following error occurred while executing this line:
2011-05-25 15:50:26,040 [Thread-23] WARN ScriptRunner - C:\Work\6.70_Extensions\NightlyBuild\Master Build.xml:6: taskdef class com.ANT_Tasks.CFileEdit cannot be found
where line 6 is:
<taskdef mame="CFileEdit" classname="com.ANT_Tasks.CFileEdit" />
my root directory is C:\Work\6.70_Extensions\NightlyBuild and i have a folder C:\Work\6.70_Extensions\NightlyBuild\com\ANT_Tasks and i have a CFileEdit.java in that folder. Anybody knows what is wrong?
Edit:
I do not wish to change the structure of my ANT script, therefore what is the next step here?
i Complied my CFileEdit.java file into CFileEdit.class
I converted CFileEdit.class into CFileEdit.jar
i put this CFileEdit.jar into C:\apache-ant-1.8.2\lib where this is my ant directory?
please tell me if i am doing it right or wrong, i am having issues with this currently
You must compile the .java into a .class and use the class for classpath for the taskdef
Compile CFileEdit.java
Define taskdef as follows -
<taskdef name="CFileEdit" classname="com.ANT_Tasks.CFileEdit">
<classpath>
<!-- refer to the compiled class here along with others as necessary to execute the task -->
</classpath>
</taskdef>

Resources