Ant creating WAR JAVA - java

Good day!
I have been trying to write ant file for compilation and making the WAR file for a long time.
But still it doesnt work. At this time I have no build errors, but there is a Tomcat "Resource not found" error, where should be my servlet.
Here is my build.xml:
<?xml version="1.0" ?>
<property environment="env"/>
<property name="dir.src" value="src"/>
<property name="dir.dist" value="ant-dist"/>
<property name="dir.lib" value="WebContent/WEB-INF/lib"/>
<property name="dir.build" value="ant-build"/>
<property name="dir.classes" value="${dir.build}/classes"/>
<property name="tomcat-home" value="${env.TOMCAT_HOME}"/>
<property name="java-home" value="${env.JAVA_HOME}"/>
<path id="compile.classpath">
<fileset dir="${dir.lib}" includes="*.jar"/>
<fileset dir="${tomcat-home}" includes="**/*.jar" />
<fileset dir="${java-home}" includes="**/*.jar" />
</path>
<target name="init">
<mkdir dir="${dir.classes}"/>
<mkdir dir="${dir.dist}" />
</target>
<target name="compile" depends="init" >
<javac destdir="${dir.build}" debug="true" srcdir="${dir.src}" includeantruntime="false">
<classpath refid="compile.classpath"/>
</javac>
</target>
<target name="war" depends="compile">
<war destfile="${dir.dist}/SoulVoxServer.war" needxmlfile="false">
<fileset dir="WebContent"/>
<lib dir="${dir.lib}"/>
<classes dir="${dir.classes}"/>
</war>
</target>
<target name="clean">
<delete dir="${dir.dist}" />
<delete dir="${dir.build}" />
</target>
I thought that the problem can depend on web.xml file missing, but i use annotation so it should work anyway, isn't it?
Thank you!
EDIT
I found that my war file doesnt contain any classes. Its structure:
+META-INF
--MANIFEST.MF
+WEB-INF
--classes (It is empty)
--lib (here goes my jar files. Its ok)
-Playlist.jsp
EDIT
I solved that problem and now i see the next:
I have 2 war files:
1) Generated by ant. 13.3 Mb. Doesnt work.
2) Generated by eclipse and has the same files inside (after jar -xvf), but it is only 6.6 Mb size.. Thi one works.
What is that?

Related

Ant - javac task - classpath is not taking the jars

I have my ant build.xml file like this:
<!-- Properties -->
<property name="src" value="${basedir}" />
<property name="jars" value="${src}/jars" />
<property name="dest" value="${src}/dest" />
<property name="reports" value="${src}/reports" />
<path id="claspath">
<fileset dir="${jars}">
<include name="*.jar" />
</fileset>
</path>
<target name="clean">
<echo> removing the directories "dest" and "reports" </echo>
<delete dir="${dest}" />
<delete dir="${reports}" />
</target>
<target name="makedir" depends="clean">
<echo> creating directories "dest" and "reports" </echo>
<mkdir dir="dest" />
<mkdir dir="reports" />
</target>
<target name="complie" depends="makedir">
<javac srcdir="${src}" destdir="${dest}" />
<classpath refid="classpath"/>
</target>
When I type ant command in cmd prompt the compiling happens but the jar files are not loading so I am getting compiling errors. The jar folder that i have mentioned in the above code has only one jar file which is "testng-6.8.5.jar". Please let me know what is wrong in the above code.
You're closing the <javac> tag on the same line, it should be:
<javac srcdir="${src}" destdir="${dest}">
<classpath refid="classpath"/>
</javac>

runtime classpath using manifest.mf classpath

i built an ear using ant and the its compiling fine. however when i deploy i get the noclassdeffound error in the weblogic server logs. so i added the libraries(jars) in server startup script i.e the server java classpath it works fine.
Please help me how to resolve this runtime classpath issue using ant build. i assume adding classpath in manifest.mf file will help. so far my build.xml is:
Please advice
<?xml version="1.0"?>
<project name="xxx APP Check" default="all" basedir=".">
<target name="init">
<property name="software.version" value="1.0"/>
<property name="user.name" value="usrnme"/>
<property name="dirs.base" value="${basedir}"/>
<property name="classdir" value="${dirs.base}/build/src"/>
<property name="src" value="${dirs.base}/src"/>
<property name="mf" value="${dirs.base}/src/META-INF"/>
<property name="jar" value="${dirs.base}/build/jar"/>
<property name="web" value="${dirs.base}/web"/>
<property name="deploymentdescription" value="${dirs.base}/build/deploymentdescriptors"/>
<property name="warFile" value="xxxappchk.war"/>
<property name="earFile" value="xxxxappchk.ear"/>
<property name="earDir" value="${dirs.base}/build/ear"/>
<property name="warDir" value="${dirs.base}/build/war"/>
<property name="srcDir" value="${dirs.base}/build/src"/>
<!-- Create Web-inf and classes directories -->
<mkdir dir="${warDir}/WEB-INF"/>
<mkdir dir="${warDir}/WEB-INF/classes"/>
<mkdir dir="${warDir}/WEB-INF"/>
<mkdir dir="${warDir}/WEB-INF/lib"/>
<mkdir dir="${warDir}/WEB-INF/classes"/>
<!-- Create Meta-inf and classes directories -->
<mkdir dir="${warDir}/META-INF"/>
<mkdir dir="${earDir}/META-INF"/>
</target>
<!-- Main target -->
<target name="all" depends="init,build,buildWar,buildEar,clean"/>
<!-- Compile Java Files and store in /build/src directory -->
<target name="build" >
<path id="3rdparty.jar.path">
<fileset dir="${jar}">
<include name="*.jar" />
</fileset>
</path>
<javac srcdir="${src}" includeantruntime="false" destdir="${classdir}" debug="true" includes="**/*.java" >
<classpath>
<path refid="3rdparty.jar.path"/>
</classpath>
</javac>
</target>
<!-- Create the War File -->
<target name="buildWar" depends="init">
<copy todir="${warDir}/WEB-INF/classes">
<fileset dir="${classdir}" includes="**/*.class" />
</copy>
<jar destfile="${warDir}/WEB-INF/lib/xxxx-appcheck.jar"
basedir="${warDir}/WEB-INF/classes"
/>
<echo message="Hyperion appcheck jar created."/>
<copy todir="${warDir}/WEB-INF/classes/com/ca/xxxx_appcheck">
<fileset dir="${src}" includes="**/*.java" />
</copy>
<copy todir="${warDir}/WEB-INF">
<fileset dir="${deploymentdescription}" includes="web.xml" />
</copy>
<copy todir="${warDir}/WEB-INF/lib">
<fileset dir="${jar}" includes="*.jar" />
</copy>
<copy todir="${warDir}">
<fileset dir="${web}" includes="**/*.*" />
</copy>
<!-- Create war file and place in ear directory -->
<jar jarfile="${earDir}/${warFile}" basedir="${warDir}" />
</target>
<!-- Create the War File -->
<target name="buildEar" depends="init">
<copy todir="${earDir}/META-INF">
<fileset dir="${deploymentdescription}" includes="application.xml" />
</copy>
<!-- Create ear file and place in ear directory -->
<jar jarfile="${dirs.base}/${earFile}" basedir="${earDir}" />
</target>
<target name="clean" description="Delete all generated files">
<delete dir="${srcDir}" failonerror="false"/>
<delete dir="${earDir}" failonerror="false"/>
<delete dir="${warDir}" failonerror="false"/>
<echo message="Deleted the temp directories src, war, ear"/>
<mkdir dir="${earDir}"/>
<mkdir dir="${warDir}"/>
<mkdir dir="${srcDir}"/>
<echo message="created the temp directories src, war, ear"/>
</target>
</project>
Usually you don't need to add anything to MANIFEST.MF to make your jars added to application classpath.
Your web application (WAR) files should be directly under EAR.
Overall dependencies (JAR) should be under EAR's APP-INF/lib directory.
Web application own dependencies under WAR files WEB-INF/lib directory. Looking at your build.xml, they should be there.
Before changing you Ant build script, please check these prerequisites and please provide the stacktrace from Weblogic error log.

ANT/JAVA: Config to include library files in the war file

Pretty new to ANT and building using it. I have a Java-Jersey rest project and i have included the Jersey libraries under WEB-INF/lib.
I have a build.xml for building/compiling the project.
<?xml version="1.0"?>
<project name="Ant-Test" 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="lib.dir" location="" />
<property name="build.dir" location="bin" />
<!--
Create a classpath container which can be later used in the ant task
-->
<path id="build.classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</path>
<!-- Deletes the existing build directory-->
<target name="clean">
<delete dir="${build.dir}" />
</target>
<!-- Creates the build directory-->
<target name="makedir">
<mkdir dir="${build.dir}" />
</target>
<!-- Compiles the java code -->
<target name="compile" depends="clean, makedir">
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="build.classpath" />
<jar destfile="${build.dir}/CrunchifyRESTJerseyExample.jar" basedir="${build.dir}"/>
<war destfile="${build.dir}/CrunchifyRESTJerseyExample.war" webxml="WebContent/WEB-INF/web.xml">
<classes dir="${build.dir}"/>
</war>
</target>
<target name="Main" depends="compile">
<description>Main target</description>
</target>
</project>
With this, i am not getting the library files in the war file. What should i add to get it in the war?.
If you take a look on the Ant war task you can specify a <lib> element with the jars that are going to be put under WEB-INF/lib folder. So try this:
<war destfile="${build.dir}/CrunchifyRESTJerseyExample.war" webxml="WebContent/WEB-INF/web.xml">
<classes dir="${build.dir}"/>
<lib dir="${lib.dir}">
<exclude name="jdbc1.jar"/> <!-- Exclude here jars you don't want -->
</lib>
</war>
Note: You should set your property at the begining of your script for the above task to work properly:
<property name="lib.dir" location="lib" /> <!-- Or whatever you call your project folder with the jars-->
try including fileset element as child of war element
<fileset dir="${home.dir}/WEB-INF/libDirectory/*">
<include name="**/*"/>
</fileset>

How to include local dependencies in my ant build

At present I have the following build.xml:
<project name="Bccn" default="help" basedir=".">
<!-- Define the properties used by the build -->
<property name="app.name" value="bccn" />
<property name="app.version" value="0.1-dev" />
<property name="tcserver.home" value="/home/abhishek/tomcat" />
<property name="work.home" value="${basedir}/work" />
<property name="dist.home" value="${basedir}/dist" />
<property name="src.home" value="${basedir}/src" />
<property name="web.home" value="${basedir}/web" />
<property name="lib.dir" value="${basedir}/lib" />
<target name="help">
<echo>You can use the following targets:</echo>
<echo>
</echo>
<echo> help : (default) Prints this message </echo>
<echo> all : Cleans, compiles, and packages application</echo>
<echo> clean : Deletes work directories</echo>
<echo> compile : Compiles servlets into class files</echo>
<echo> dist : Packages artifacts into a deployable WAR</echo>
<echo>
</echo>
<echo>For example, to clean, compile, and package all at once, run:</echo>
<echo>prompt> ant all </echo>
</target>
<!-- Define the CLASSPATH -->
<path id="compile.classpath">
<fileset dir="${tcserver.home}/bin">
<include name="*.jar" />
</fileset>
<pathelement location="${tcserver.home}/lib" />
<fileset dir="${tcserver.home}/lib">
<include name="*.jar" />
</fileset>
</path>
<target name="all" depends="clean,compile,dist" description="Clean work dirs, then compile and create a WAR" />
<target name="clean" description="Delete old work and dist directories">
<delete dir="${work.home}" />
<delete dir="${dist.home}" />
</target>
<target name="prepare" depends="clean" description="Create working dirs and copy static files to work dir">
<mkdir dir="${dist.home}" />
<mkdir dir="${work.home}/WEB-INF/classes" />
<!-- Copy static HTML and JSP files to work dir -->
<copy todir="${work.home}">
<fileset dir="${web.home}" />
</copy>
</target>
<target name="compile" depends="prepare" description="Compile Java sources and copy to WEB-INF/classes dir">
<javac srcdir="${src.home}" destdir="${work.home}/WEB-INF/classes">
<classpath refid="compile.classpath" />
</javac>
<copy todir="${work.home}/WEB-INF/classes">
<fileset dir="${src.home}" excludes="**/*.java" />
</copy>
</target>
<target name="dist" depends="compile" description="Create WAR file for binary distribution">
<jar jarfile="${dist.home}/${app.name}-${app.version}.war" basedir="${work.home}" />
</target>
Now I included log4j as a local dependency and want to include it when I create my .war file. However, ANT is not able to find the dependency. Is there a way to get it working? Sorry for the basic question, I am a noob at it.
Update (and thanks for the help I got already):
I didn't want to add the "war" thing so I modified my build.xml as follows:
``
<target name="help">
<echo>You can use the following targets:</echo>
<echo>
</echo>
<echo> help : (default) Prints this message </echo>
<echo> all : Cleans, compiles, and packages application</echo>
<echo> clean : Deletes work directories</echo>
<echo> compile : Compiles servlets into class files</echo>
<echo> dist : Packages artifacts into a deployable WAR</echo>
<echo>
</echo>
<echo>For example, to clean, compile, and package all at once, run:</echo>
<echo>prompt> ant all </echo>
</target>
<!-- Define the CLASSPATH -->
<path id="compile.classpath">
<fileset dir="${tcserver.home}/bin">
<include name="*.jar" />
</fileset>
<pathelement location="${tcserver.home}/lib" />
<fileset dir="${tcserver.home}/lib">
<include name="*.jar" />
</fileset>
<fileset dir="${lib.dir}">
<include name="*.jar" />
</fileset>
</path>
<target name="all" depends="clean,compile,dist" description="Clean work dirs, then compile and create a WAR" />
<target name="clean" description="Delete old work and dist directories">
<delete dir="${work.home}" />
<delete dir="${dist.home}" />
</target>
<target name="prepare" depends="clean" description="Create working dirs and copy static files to work dir">
<mkdir dir="${dist.home}" />
<mkdir dir="${work.home}/WEB-INF/classes" />
<!-- Copy static HTML and JSP files to work dir -->
<copy todir="${work.home}">
<fileset dir="${web.home}" />
</copy>
</target>
<target name="compile" depends="prepare" description="Compile Java sources and copy to WEB-INF/classes dir">
<javac srcdir="${src.home}" destdir="${work.home}/WEB-INF/classes">
<classpath refid="compile.classpath" />
</javac>
<copy todir="${work.home}/WEB-INF/classes">
<fileset dir="${src.home}" excludes="**/*.java" />
<fileset dir="${lib.dir}" includes="*.jar" />
</copy>
</target>
<target name="dist" depends="compile" description="Create WAR file for binary distribution">
<jar jarfile="${dist.home}/${app.name}-${app.version}.war" basedir="${work.home}" />
</target>
Now ANT can find the dependencies and compile it. However when I deploy it to a Tomcat server, it fails to file the dependencies. Can you please provide some ideas as to how I can package the dependencies so its visible to Tomcat as well?
You should use the war task instead:
<war destfile="${warname}" webxml="war/WEB-INF/web.xml">
<fileset dir="${work.home}"/>
<lib dir="${lib.dir}/" includes="log4j.jar"/>
<classes dir = "${CLASSES}" />
</war>
You can configure it to find the dependency jars to be included in your war file and to specify the path to the web.xml folder. You shouldn't be coping the .class files to the destination in the war folder, let the war task do it for you.

Ant built does not generate class files

I'm using build.xml to build my src. However it failed to generate class files without any error message. The full script is
<?xml version="1.0"?>
<project name="auxiliary" basedir="." default="dist">
<property name="src.dir" value="../auxiliary-src/com/nextbio/drugbank"/>
<property name="dist.dir" value="dist"/>
<property name="lib.dir" value="../jboss_config/common_app_jars"/>
<property name="temp.dir" value="temp"/>
<property name="foo_dist.dir" value="../foo/dist"/>
<path id="libs-classpath">
<fileset dir="${foo_dist.dir}">
<include name="foo.jar"/>
</fileset>
</path>
<target name="dist" depends="auxiliary-dist" />
<target name="auxiliary-cleanup">
<delete dir="${temp.dir}"/>
<delete dir="${dist.dir}"/>
<echo message="cleaned up. ${temp.dir}, and ${dist.dir} have been deleted."/>
</target>
<target name ="auxiliary-dist">
<delete dir="${temp.dir}"/>
<echo message="delete ${temp.dir}" />
<mkdir dir="${temp.dir}"/>
<javac destdir="${temp.dir}" source="1.6" target="1.6" debug="on" fork="true" memorymaximumsize="1024m">
<src path="${src.dir}"/>
<classpath>
<path refid="libs-classpath"/>
</classpath>
<include name="com/car/**"/> <!-- troubled line -->
</javac>
<!--<copy overwrite="true" todir="${temp.dir}">
<fileset dir="${src.dir}">
<exclude name="**/*.java"/>
<exclude name="**/*.sql"/>
<exclude name="**/*.txt"/>
</fileset>
</copy>
<delete dir="${dist.dir}"/>
<mkdir dir="${dist.dir}"/>
<jar destfile="${dist.dir}/auxiliary.jar" basedir="${temp.dir}"/> -->
</target>
There is no class file in ${temp.dir} after this step, and no error message. I double checked it, and found it is because of the "troubled line". I tried to add some files to the classpath. I don't know why it is wrong.
The source path should point to the root of the package tree. You make it point to a specific package inside the sources : ../auxiliary-src/com/nextbio/drugbank.
And in the javac task, you ask it to compile all the files matching the pattern com/car/**. That means that it will compile the Java source files in ../auxiliary-src/com/nextbio/drugbank/com/car or in a subdirectory. If that's the case, you have very unconventional package names.
I had the same problem.
My project complilated well but the classes there weren't in nowhere and It didn't have any error message.
My problem was the classpath. The eclipse wizard added EclipseLink 2.5.1 jars.
I removed it and the problem is gone.
I suggest make a simple HelloWord and remove all jars
reference from the classpath and try again.
I encountered this "ant, javac, compile" problem related with the classpath to.
No debug or verbose message shown.
This behavior appear because in classpath exists not compatible (superior) version jar packages and that cause no output classes.

Categories