Solved

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

Posted on 2013-12-11
3
719 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
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 about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

759 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

21 Experts available now in Live!

Get 1:1 Help Now