• Enter Slide 1 Title Here

    This is slide 1 description. Go to Edit HTML of your blogger blog. Find these sentences. You can replace these sentences with your own words.

  • Enter Slide 2 Title Here

    This is slide 2 description. Go to Edit HTML of your blogger blog. Find these sentences. You can replace these sentences with your own words.

  • Enter Slide 3 Title Here

    This is slide 3 description. Go to Edit HTML of your blogger blog. Find these sentences. You can replace these sentences with your own words.

Tuesday, July 28, 2009

[Ant] Build .WAR files in Eclipse for Web Applications

Eclipse JEE versions support Java Web Application projects, but other Eclipse versions do not. Java developers need to build WAR (web archive) files for deployments (yes, Exploded deployments are also possible). However Eclipse does not provide a direct way to create war files; developers write ant build files for this. So we thought of sharing a generic ant build file for Web Applications.

Our general Web Application's folder structure is shown in the image. In most cases, this structure will exactly match t your project structure; however the folder named "WebRoot" may be different to yours. (If your folder structure is different, let us know in comments section).

Ant Build file (build.xml)

Following is the general ant build file (build.xml).

<project name="MyWebApplication" basedir="." default="archive">

<property name="WEB-INF" value="${basedir}/WebRoot/WEB-INF" />
<property name="OUT" value="${basedir}/out" />
<property name="WAR_FILE_NAME" value="mywebapplication.war" />
<property name="TEMP" value="${basedir}/temp" />

<target name="help">
<echo>
--------------------------------------------------
compile - Compile
archive - Generate WAR file
--------------------------------------------------
</echo>
</target>

<target name="init">
<delete dir="${WEB-INF}/classes" />
<mkdir dir="${WEB-INF}/classes" />
</target>

<target name="compile" depends="init">
<javac srcdir="${basedir}/src"
destdir="${WEB-INF}/classes"
classpathref="libs">
</javac>
</target>

<target name="archive" depends="compile">
<delete dir="${OUT}" />
<mkdir dir="${OUT}" />
<delete dir="${TEMP}" />
<mkdir dir="${TEMP}" />
<copy todir="${TEMP}" >
<fileset dir="${basedir}/WebRoot">
</fileset>
</copy>
<move file="${TEMP}/log4j.properties"
todir="${TEMP}/WEB-INF/classes" />
<war destfile="${OUT}/${WAR_FILE_NAME}"
basedir="${TEMP}"
compress="true"
webxml="${TEMP}/WEB-INF/web.xml" />
<delete dir="${TEMP}" />
</target>

<path id="libs">
<fileset includes="*.jar" dir="${WEB-INF}/lib" />
</path>

</project>

You can go through the above xml file and see the process; we have created an attribute for WAR file's name.

<property name="WAR_FILE_NAME" value="mywebapplication.war" />

You should change the value "mywebproject.war" to match your project name. Save the above build.xml file inside Web applications project folder as shown in the folder structure image.

Ant build file has separate tasks for compiling the project and to build the war file. In Eclipse you just have to right click on this build file and select "Ant Build" to execute it. The war file will be generated and stored inside <web-project>/out folder.

Related Articles

How to open a .war (web archive) or .jar (java archive) files
Open and read any file in a .war file of a web application with Java

Thursday, July 9, 2009

[Tomcat] validateJarFile(servlet-api.jar) - jar not loaded. Offending class: javax/servlet/Servlet.class

validateJarFile jar not loadedorg.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(<APP_PATH>\WEB-INF\lib\servlet-api.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
.

We are using Apache Tomcat to deploy web applications, and getting the above message when Apache Tomcat is started. All web applications inside Tomcat are working fine, however no developer would want to see message (at least we do not want to see).

The above message is only a warning message due to the existence of multiples of the same javax.servler.Servlet.class for Tomcat runtime to pick; and this extra instance has come from a jar file named <APP_PATH>\WEB-INF\lib\servlet-api.jar. Tomcat has its own servlet-api.jar file; look at the following folders to locate the file (depending on the Tomcat version; location may be different).
  • <TOMCAT_HOME>\common\lib
  • <TOMCAT_HOME>\lib
Your web applications have the same servlet-api.jar file inside following folder.
  • <TOMCAT_HOME>\webapps\<PROJECT>\WEB-INF\lib
As the Tomcat runtime has the required javax/servlet/Servlet.class file loaded from its own lib\servlet-api.jar file, you do not need to place it inside each and every web application. After removing the jar file from your web application, you will not receive the message again.

Related Articles

Wednesday, July 8, 2009

Attempted to lock an already-locked dir: Subversion issue

"Attempted to lock an already-locked dir" - svn: Working copy locked; this message is frequently faced by users of SubVersion (SVN) source repository. The message is self explanatory; simply some source files are locked and no commit or update operations allowed on those files/folders. In some occasions, Software developers in a team receive this message in a SVN operation even though none of them have locked the files or folders; causing confusion. So how would you try and resolve?

We use Subversion to create and store our projects in repositories; Subclipe (an Eclipse plugin) as the client tool to connect to repository.

We tried following solutions

1. Commit Changes - change and commit changes into repository
2. File Delete - delete the files and commit the delete into repository
3. Override & update - override the local changes and update from repository

None of the above options resolved the issue, but got the same type of error messages. Followings are the error messages shown inside Eclipse while trying the above mentioned options.

commit -m C:/workspace/MyProject/config.properties
Attempted to lock an already-locked dir
svn: Working copy 'C:\workspace\MyProject' locked

update -r 13 C:/workspace/MyProject/src
Attempted to lock an already-locked dir
svn: Working copy 'C:\workspace\MyProject' locked

delete --force C:/workspace/MyProject/config.properties
Attempted to lock an already-locked dir
svn: Working copy 'C:\workspace\MyProject' locked

Use Cleanup command

Subversion:Team-CleanupSubversion supports a command named "Cleanup"; it is used to release the locks on a project.

If you have faced with the above issue while no one has locked the project files, you can run this command and resolve the issue.
You do this in Eclipse by right clicking the project; then Team->Cleanup as shown in the image.