How to add log4j.xml path in Ant build.xml - java

I have a log4j2.xml file which is not being picked by Ant's build.xml. So, I keep getting the following error message in Eclipse console -
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
My build.xml file is very simple. (pasted below) I copied it from one of the examples online.
I have gone through ant documentation and am aware of the and I have used these to set the log4j2.xml in classpath and have failed.
<project name="Learning" default="hello" basedir=".">
<property name="src.dir" value="src"/>
<property name="lib.dir" value="${src.dir}/lib"/>
<property name="build.dir" value="build"/>
<target name="hello">
<!-- compile just the basic hello files -->
<echo> This will have all the basic builds </echo>
<javac srcdir="{src.dir}" destdir="{build.dir}"
includeantruntime="false"/>
</target>
Can you please give me a code snippet to add my Log4j2.xml file sitting in the basedir or the project to the classpath?

Unfortunately, javac does not copy the resources (the directory contains only the compiled classes). However, you can copy them. e.g.:
<project name="Learning" default="hello" basedir=".">
<property name="src.dir" value="src"/>
<property name="lib.dir" value="${src.dir}/lib"/>
<property name="build.dir" value="build"/>
<target name="hello">
<echo>Copy non-java resources</echo>
<copy todir="{build.dir}" overwrite="true">
<fileset dir="{src.dir}" excludes="**/*.java"/>
</copy>
<!-- compile just the basic hello files -->
<echo> This will have all the basic builds </echo>
<javac srcdir="{src.dir}" destdir="{build.dir}"
includeantruntime="false"/>
</target>
See more in Copy Task.

Related

Issue while creating war using ant with Jenkins

I have a ant build script which creates a war file. The file content are as follows.
<?xml version="1.0" encoding="UTF-8"?>
<project name="TestProj" default="war" basedir=".">
<property name="project-name" value="${ant.project.name}" />
<property name="builder" value="IaasTeam" />
<property name="war-file-name" value="${project-name}.war" />
<property name="source-directory" value="src" />
<property name="classes-directory" value="build/classes" />
<property name="web-directory" value="WebContent" />
<property name="web-xml-file" value="WebContent/WEB-INF/web.xml" />
<property name="lib.dir" value="WebContent/WEB-INF/lib" />
<property name="catalina.home" value="../../outside/project/lib"/>
<tstamp prefix="build-info">
<format property="current-date" pattern="d-MMMM-yyyy" locale="en" />
<format property="current-time" pattern="hh:mm:ss a z" locale="en" />
</tstamp>
<property name="build-directory" value="build" />
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
<fileset dir="${catalina.home}" includes="**/*.jar"/>
</path>
<target name="clean">
<delete dir="build"/>
</target>
<target name="compile">
<mkdir dir="build/classes"/>
<javac includeantruntime="false" srcdir="src" destdir="build/classes" classpathref="classpath" />
</target>
<target name="war" depends="clean,compile">
<mkdir dir="${build-directory}" />
<delete file="${build-directory}/${war-file-name}" />
<war warfile="${build-directory}/${war-file-name}" webxml="${web-xml-file}">
<classes dir="${classes-directory}" />
<fileset dir="${web-directory}">
<!-- Need to exclude it since webxml is an attribute of the war tag above -->
<exclude name="WEB-INF/web.xml" />
</fileset>
<manifest>
<attribute name="Built-By" value="${builder}" />
<attribute name="Built-On" value="${build-info.current-date}" />
<attribute name="Built-At" value="${build-info.current-time}" />
</manifest>
</war>
</target>
I am using Jenkins as a build server (this is hosted on different machine kind of DEV environment).
I also use Gitlab as a repository and after pushing the latest code I have a hook for Jenkins job which gets triggered automatically and calls this build.xml.
Now the issues here is that when I run this script on my local machine everything works well but when Jenkins execute this it fails during the compilation phase giving me below error.
compile:
[mkdir] Created dir: /app/infra/jenkins/workspace/TestProj/build/classes
[javac] Compiling 49 source files to /app/infra/jenkins/workspace/TestProj/build/classes
BUILD FAILED
/app/infra/jenkins/workspace/TestProj/build.xml:27: /app/infra/jenkins/outside/project/lib does not exist.
The reason for this issue is the build server does not have any directoy called outside/project/lib.
The only reason of adding this directory in my build.xml is to have the container specific jar files ready for compiling.
How can I fix this issue?
Do I need to copy container specific jars on my build server? Or is there any way to tell Jenkins that not to copy this external jars but just use them for compilation.
Where would Jenkins find the jars? They need to be accessible otherwise your build will fail. If you don't want to have the files checked in (which is very sensible), you could use Apache Ivy to download them for you.
This is the most common way of handling the situation you're having. Using a dependency management framework like Ivy (or Maven, or similar) will save you a lot of headaches down the line. I recommend you have a look at their tutorial. After you set it up, your ant build will take care of downloading the files you need.

ANT JAR file for JavaFX Application

I have successfully complied the JavaFX code using Build Script with the previous help. Now I can not able to create JAR file uisng ANT for my application. I am adding sample script in build.xml. My requirement is to create a simple JAR file of my JavaFx XYZ application.
<project name="XYZ" basedir=".">
<property name="WorkingFolder" location="XYZSourceData"/>
<property name="ClassPath" location="C:\Program Files\Oracle\JavaFX 2.2 Runtime\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_09\lib\ant-javafx.jar;"/>
<target name="init">
<echo message="Java installation directory: ${java.home}"/>
<!-- Create the time stamp -->
<tstamp/>
<delete dir="${WorkingFolder}/build"/>
<delete dir="${dist}"/>
<mkdir dir="${WorkingFolder}/CustomJars"/>
</target>
<target name="Compilingxyz" depends="init">
<mkdir dir="${WorkingFolder}/build"/>
<taskdef resource="com/sun/javafx/tools/ant/antlib.xml"
uri="javafx:com.sun.javafx.tools.ant" classpath=".;C:\Program Files\Oracle\JavaFX 2.2 Runtime\lib\jfxrt.jar"/>
<javac classpath="${ClassPath};${WorkingFolder}/CustomJars/*.jar;" srcdir="${WorkingFolder}/src/com/xyz" destdir="${WorkingFolder}/build"/>
</target>
<target name="CreatingxyzJars" depends="Compilingxyz" description="generate the distribution" >
<taskdef resource="com/sun/javafx/tools/ant/antlib.xml"
uri="javafx:com.sun.javafx.tools.ant" classpath="C:\Program Files\Java\jdk1.7.0_09\lib\ant-javafx.jar"/>
<fx:jar destfile="${WorkingFolder}/CustomJars/XYZ.jar">
<fx:application name="XYZ"
mainClass="com.xyz.main.XYZEntryFX"/>
<fx:resources>
<fx:fileset dir="${WorkingFolder}/build" includes="${WorkingFolder}/libs/*.jar"/>
</fx:resources>
<fileset dir="${WorkingFolder}/resources"/>
</fx:jar>
</target>
I am getting following error -
BUILD FAILED
C:\Users\JavaUser4\Desktop\2012.12FX\build.xml:83: The prefix "fx" for element "
fx:jar" is not bound.
Total time: 0 seconds
What is the missing part? I have Java Desktop application. How can I create a ANT JAR for Java Fx Application. Please Help.
I am taking reference of following example -
Example
You're missing the fx: namespace declaration in your project. Rather than
<project name="XYZ" basedir=".">
you need something like:
<project name="XYZ" basedir="."
xmlns:fx="javafx:com.sun.javafx.tools.ant">
(This is from the documentation you linked to, prior page, ยง12.3 Using JavaFX Ant Tasks.)

using xjc with ant

I am trying to use xjc compiler from ant. Builds successfully but nothing gets generated.
My ant script is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<project name="AutomateWithAnt" basedir=".">
<property file="build.properties"/>
<path id="compile.classpath">
<fileset dir="${lib.includes}" includes="*.jar"></fileset>
</path>
<target name="init" description="create java class">
</target>
<taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask" classpathref="compile.classpath"/>
<!-- Generates the source code from the test.xsd schema using jaxb -->
<target name="option-generate" description="Generates the source code" depends="init">
<xjc schema="test.xsd" destdir="${generated-src.dir}" package="${generated-src.dir}">
<arg value="-Xcommons-lang" />
<arg value="-Xcommons-lang:ToStringStyle=SHORT_PREFIX_STYLE" />
<produces dir="${generated-src.dir}" includes="**/*.java" />
</xjc>
</target>
</project>
my build.properties is:
lib.includes=lib/
generated-src.dir=/
I am using java 1.6 and I have used jaxb-sjc.jar.
You've defined 2 Ant targets (init and option-generate), but neither of them will be invoked unless you specify which one to run.
You either need to specify it on the command line, e.g.
ant option-generate
or add a default target to the <project> element, e.g.
<project name="AutomateWithAnt" basedir="." default="option-generate">
Incidentally, your init target is empty, and therefore pointless.

How do you transition from using Eclipse for your builds to using Ant

I have a spring web application in Eclipse Helios. I use Ant for my builds on other projects but am not familiar with initially setting up Ant build files. Is there a painless way to make the transition from using Eclipse to do my builds, to creating an Ant build file and any supporting files? I tried copying a basic Ant build file into my application but I'm not sure if it's even close to what I need. I've included it below. It's giving me this error when I run it.
BUILD FAILED
C:\..\..\workspace\..\build.xml:21: Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK.
My system JAVA_HOME path is set to 'C:\Program Files\Java\jdk1.7.0\bin'
<?xml version="1.0" encoding="UTF-8"?>
<project name="projectName" default="dist" basedir=".">
<description>
build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init"
description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}"/>
</target>
<target name="dist" depends="compile"
description="generate the distribution" >
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the News-${DSTAMP}.jar file -->
<jar jarfile="${dist}/lib/ProjectName-${DSTAMP}.jar" basedir="${build}"/>
</target>
<target name="clean"
description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>
Don't include the bin folder in your JAVA_HOME
Try this:
C:\Program Files\Java\jdk1.7.0

Is there an ant task to add a .war to an existing exploded ear

Have a build process that can't be edited and need to pack another war in the the ear that is generated. The ear is exploded so it's just a matter of copying the war file into it but the application.xml needs to be updated so I'd like to find an ant task that will do this. Anyone know of one that will work?
ended up just doing :
<replace file="${j2ee.build.dir}/${j2ee.app..name}/META-INF/application.xml" token="</application>" value="<module><web><web-uri>admin.war</web-uri><context-root>/admin</context-root></web></module></application>"/>
Rather hackish but couldn't come up with another way to edit the file easily
The idea is to create the war and put the file into the ear directory, see the following build.xml code
<?xml version="1.0"?>
<project name="Add war to ear example" default="all" basedir=".">
<target name="init">
<property name="root.directory" value="${basedir}"/>
<property name="classdir" value="${root.directory}/build/src"/>
<property name="src" value="${root.directory}/src"/>
<property name="web" value="${root.directory}/web"/>
<property name="deploymentdescription" value="${root.directory}/build/deploymentdescriptors"/>
<property name="war.file" value="test.war"/>
<property name="ear.file" value="test.ear"/>
<property name="ear.directory" value="${root.directory}/build/ear"/>
<property name="war.directory" value="${root.directory}/build/war"/>
<!-- Create Web-inf and classes directories -->
<mkdir dir="${war.directory}/WEB-INF"/>
<mkdir dir="${war.directory}/WEB-INF/classes"/>
<!-- Create Meta-inf and classes directories -->
<mkdir dir="${ear.directory}/META-INF"/>
</target>
<!-- Main target -->
<target name="all" depends="init,build,buildWar,buildEar"/>
<!-- Compile Java Files and store in /build/src directory -->
<target name="build" >
<javac srcdir="${src}" destdir="${classdir}" debug="true" includes="**/*.java" />
</target>
<!-- Create the War File -->
<target name="buildWar" depends="init">
<copy todir="${war.directory}/WEB-INF/classes">
<fileset dir="${classdir}" includes="**/*.class" />
</copy>
<copy todir="${war.directory}/WEB-INF">
<fileset dir="${deploymentdescription}" includes="web.xml" />
</copy>
<copy todir="${war.directory}">
<fileset dir="${web}" includes="**/*.*" />
</copy>
<!-- Create war file and place in ear directory -->
<jar jarfile="${ear.directory}/${war.file}" basedir="${war.directory}" />
</target>
<!-- Create the War File -->
<target name="buildEar" depends="init">
<copy todir="${ear.directory}/META-INF">
<fileset dir="${deploymentdescription}" includes="application.xml" />
</copy>
<!-- Create ear file and place in ear directory -->
<jar jarfile="${root.directory}/${ear.file}" basedir="${ear.directory}" />
</target>
</project>
Won't <copy> do the job?
What about using <xslt> to modify application.xml then?

Categories