0

I have a Java web application. I am deploying this as a war using ant file onto Tomcat server. I could successfully do that.Please find below build.xml Now the challenge is i have 100000 images in my application under web folder which gets copied to war root folder.

If i create a war with 100000 images in the root folder of war file, it would be big headache.

Every time i change anything in JSP or java code, new war again would copy those 100000 images in war folder which takes more then 1 hour to build the war file.

How can i make sure that my images folder in war not getting copied again and again with every deployment ?

<!-- setting classpath -->


<path id="base.class.path">
    <pathelement location="lib/joda-time-1.6.1.jar" />
    <pathelement location="lib/fedExTrackingWebService.jar" />
    ....
    .....
</path>

 <property file="build.properties"/>

    <path id="classpath">
        <fileset dir="${lib.dir}"/>
    </path>

    <target name="clean">
        <echo>Cleaning the ${build.dir}</echo>
        <delete dir="${build.dir}"/>
        <delete dir="${dist.dir}"/>
    </target>

    <target name="init" depends="clean">
        <echo>Creating the build directory</echo>
        <mkdir dir="${build.dir}/WEB-INF/classes"/>
        <mkdir dir="${build.dir}/WEB-INF/lib"/>
        <mkdir dir="${dist.dir}"/>
    </target>

    <target name="compile" depends="init">
        <echo>Compile the source files</echo>
        <javac srcdir="${src.dir}" destdir="${build.dir}/WEB-INF/classes">
            <classpath refid="base.class.path"/>                
        </javac>
    </target>

    <target name="copy" depends="compile">
        <copy todir="${build.dir}/WEB-INF">
            <fileset dir="${web.dir.webinf}/WEB-INF"/>
        </copy>
        <copy todir="${build.dir}">
            <fileset dir="${web.dir}"/>
        </copy>
        <copy todir="${build.dir}/WEB-INF/lib">
            <fileset dir="${lib.dir}">
                            </fileset>
        </copy>
    </target>


    <target name="war">
        <echo>Building the war file</echo>
        <war destfile="${dist.dir}/${ant.project.name}.war" webxml="${build.dir}/WEB-INF/web.xml">
            <fileset dir="${build.dir}"/>
        </war>
    </target>

    <target name="deploy_local" depends="war">
        <echo>Deploying .war to local Tomcat</echo>
        <copy todir="${tomcat.dir}/webapps">
            <fileset dir="${dist.dir}">
                <include name="${ant.project.name}.war"/>
            </fileset>
        </copy>
    </target> 

Gaurav
  • 259
  • 4
  • 16
  • Could you please be more specific? How do you create war file? maven war plugin, ant war task, jar -cf, etc.? – lifus Aug 03 '13 at 23:53
  • Its ant war task - Please find above the build file. I am deploying the war using ant. – Gaurav Aug 04 '13 at 00:34
  • You may try to add `update="true"` to your `war` task. – lifus Aug 04 '13 at 00:44
  • update="true" -- Using this will the modified java and JSP files will be copied or not ? I want modified java and JSP files to be part of war every time but don't want to copy the images again and again. – Gaurav Aug 04 '13 at 01:01
  • Yes, it performs timestamp comparison. – lifus Aug 04 '13 at 01:07
  • When i build war with 100000 images then i get this error - java.util.zip.ZipException: invalid LOC header (bad signature). Can i do some thing like first of all i create the war without images folder and once the war is deployed in the webapps folder of the tomcat, i can copy the images manually. Can i do this ? if that is possible then what would happen if i build a war again with update =true and deploy to tomcat then will that remove the images folder which i added earlier manually ? – Gaurav Aug 04 '13 at 01:28
  • When i build war with 100000 images then i getjava.util.zip.ZipException: invalid LOC header (bad signature). Can i create the war without images folder and once the war is deployed in the webapps folder, can i copy the images folder manually. – Gaurav Aug 04 '13 at 01:34
  • Sure, it should work. You may freely exclude images in `copy` task. – lifus Aug 04 '13 at 01:35
  • Also, delete war file first. It may be corrupted. – lifus Aug 04 '13 at 01:49

1 Answers1

1

You may try to modify war file incrementally:

<target name="war" update="true">

You may preliminary include images into war file and then exclude them from fileset in copy task.

E.g:

<target name="copy" depends="compile">
    <copy todir="${build.dir}">
        <fileset dir="${web.dir}">
           <exclude name="images/**"/>
        </fileset>
    </copy>

You may also consider to add duplicate="preserve" to ant war task. It don't think that it's directly related to you issue, but still.

But as per manual:

Please note that the Zip format allows multiple files of the same fully-qualified name to exist within a single archive. This has been documented as causing various problems for unsuspecting users. If you wish to avoid this behavior you must set the duplicate attribute to a value other than its default, "add".

And also here's blog post.

lifus
  • 8,074
  • 1
  • 19
  • 24
  • In my index.jsp there is a statement – Gaurav Aug 04 '13 at 01:56
  • Don't worry. [That's how your war file should look like](http://documentation.progress.com/output/Iona/orbix/6.1/tutorials/fnb/dev_intro/images/j2ee_overviewa6.gif). – lifus Aug 04 '13 at 02:06
  • This is a very old project having thousands of JSPs. i cannot change the image path. it works like this -"images/frontpage/emoges/rbdi_revolution.jpg" but does not work with this - "/images/frontpage/emoges/rbdi_revolution.jpg". And currenlty all JSPs have link like this - "/images/frontpage/emoges/rbdi_revolution.jpg" So what should i do ? – Gaurav Aug 04 '13 at 02:18
  • [It might be useful](http://stackoverflow.com/questions/8375275/how-to-set-context-path-in-tomcat-so-one-could-enter-the-site-without-appending), especially [this](http://stackoverflow.com/a/7706950/2286990). – lifus Aug 04 '13 at 02:30
  • My context path is myApp. and my war name is also myApp. And currently the image folder is under this myApp. But image does not get picked up ? However if create image folder in webapps it works or if i change the path to "images/frontpage/emoges/rbdi_revolution.jpg". But both of these options does not seems to be practical at this time ? – Gaurav Aug 04 '13 at 02:35
  • Well, you may try several tricks mentioned in links above and use webapps folder as a fallback. – lifus Aug 04 '13 at 02:47
  • `images/frontpage/emoges/rbdi_revolution.jpg` is relative path. `/images/frontpage/emoges/rbdi_revolution.jpg` is absolute path. You should make your web application to be [the Tomcat default application](http://wiki.apache.org/tomcat/HowTo#How_do_I_make_my_web_application_be_the_Tomcat_default_application.3F) Thus your context path [will be empty string](http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Naming) and so /images/... will be accessible. – lifus Aug 04 '13 at 03:22
  • Thanks alot lifus....You really helped me alot that too on weekend. I was really struggling with this. Thanks to u again. – Gaurav Aug 04 '13 at 04:56
  • You are welcome. Congratulations, you reached the 20-point mark, so you will be able to move discussion to chat next time. There is really a lot of comments at this thread. – lifus Aug 04 '13 at 11:18