Solved

Including XML files in a jar file when building a project using Ant

Posted on 2013-12-11
3
740 Views
Last Modified: 2013-12-19
I have an Ant script that I use to build a jar file that contains our company's library.  Included in the library are a bunch of Hibernate models and services so I need to include hibernate xml files as part of the build:   Below is the complete script.   My problem is that I can't seem to get Ant to include the XML files as part of the build.   I have to manually open the jar each time and insert the XML files, then save the file.   Any way to do this better?

<?xml version="1.0" encoding="UTF-8"?>
<project name="companyx.makejar" default="makejar" basedir=".">
<target name ="makejar" description="Create a jar for the companyx project">      
<jar jarfile="companyx.jar" includes="**\*.class,*.properties,*.xml" basedir="bin\.">
      <manifest>
                       <attribute name="Main-Class" value="com.companyx.util.DummyMain" />
                         <attribute name="Class-Path" value="dom4j-1.6.1.jar  
                         log4j-1.2.15.jar       
                         ejb3-persistence-1.0.2.GA.jar
                         commons-collections-3.1.jar
                         commons-io-1.3.2.jar
                         javassist-3.4.GA.jar
                         jta-1.1.jar
                         hibernate-emscribedx.cfg.xml      
                         hibernate-emscribedxcode.cfg.xml      
                         commons-lang-2.4.jar
                         commons-logging-1.1.1.jar
                         mysql-connector-java-5.1.24-bin.jar
                         slf4j-api-1.5.2.jar
                         slf4j-log4j12-1.5.2.jar
                         sqljdbc4.jar
                         hibernate-annotations.jar
                         hibernate3.jar
                         cryptoutils.jar
                         log4j-1.2.15.jar
                         httpcore-4.2.1.jar
                         json_simple-1.1.jar
                         httpclient-4.2.1.jar      
                                "/>
             </manifest>
      </jar>
</target>
</project>
0
Comment
Question by:efamilant
  • 2
3 Comments
 
LVL 16

Expert Comment

by:Valeri
ID: 39711228
Try this:

<fileset id="resources" dir="${rez}">
        <include name="**/*.properties"/>
        <include name="**/*.gif"/>
        <include name="**/*.jpg"/>
        <include name="**/*.png"/>
        <include name="**/*.xml"/>
    </fileset>
 <target name="jar" depends="compile,revupdate" description="Build jar">
 <jar jarfile="${jarfile}" basedir="${bin}" manifest="Manifest">
 <fileset refid="resources"/>
 </jar>
 </target>
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 39711613
A better solution is to have a dedicated resource copying task. That way, the resources can be put in the right place (in the classpath) at runtime, as well as when packaged in an archive. Of course, leaving them permanently in the classpath interferes with clean tasks, so it's better to do it on-demand. e.g.

    <target name="copy-resources">
	<copy todir="${bin.dir}/resources">
	    <fileset dir="${source.dir}">
		    <include name="**/*.properties" />
		    <include name="**/*.xml" />
	    </fileset>
	</copy>
    </target>

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39729766
:)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now