Alla
Alla

Reputation: 53

How to make a JAR file from a subset of project classes?

I have a Java project that has just about every class and package depending on everything else. I am trying to untangle it. My current idea is to extract core classes, that shouldn't depend on anything else and package them as a separate JAR that the rest of the application will use. I am using Ant and Netbeans. I am a complete Ant newbie, thought I started reading a book.

I have tried to make an Ant build file that will compile and package the core classes in a JAR. However, no matter what I do, the JAR ends up containing every application class. I am not sure if this is because some of my core classes depend on other classes, that get automatically pulled into the JAR, or because I can't get the Ant directives right. Here is my current build file:

<?xml version="1.0" encoding="UTF-8"?>
<project name="Core" default="jar">
<description>Compiles and packages Core packages</description>

<target name="init">
    <mkdir dir="build/classes"/>
    <mkdir dir="dist"/>    
</target>

<target name="compile" depends="init">
    <javac srcdir="src" destdir="build/classes"/>
    </target>

    <target name="jar" depends="compile">
        <jar destfile="dist/core.jar" basedir="build/classes"> 
            <zipfileset dir="build/classes" includes="core/**,xml/**,file/**,exceptions/**"/>
        </jar>
    </target>

</project>

Any advice?

Upvotes: 3

Views: 1024

Answers (1)

Jayan
Jayan

Reputation: 18468

What is the use of zipfileset entry? Following should be enough to filter in select folders to the jar.

  <target name="jar" depends="compile">
       <jar destfile="dist/core.jar" basedir="build/classes"  includes="core/**,xml/**,file/**,exceptions/**" />
   </target>

Upvotes: 1

Related Questions