Create Multiple JARs from Eclipse Project

2019-05-05 03:41发布

问题:

I have begun working on a project that, at the moment, produces a monolithic JAR file from an Eclipse project. Basically, there is an Ant script that runs anytime the Eclipse Java Builder runs and takes the output of the Eclipse Java Builder (the .class files) and packages that up into a JAR file. That JAR file is then placed in a common directory so other tools can grab it for the latest build.

This works well for the team so I don't want to change it too much, but, there are a lot of unnecessary files within that JAR making it much larger than it needs to be. I was wondering if it was possible, within the same sort of framework as I described above (Eclipse + Ant), to split up the big JAR file into smaller, more manageable JAR files. For example:

com.company.project.thing1 com.company.project.thing2 com.company.project.thing3

All three things are packaged into the JAR. Instead, I only want thing1 and thing2 in one JAR file and thing3 to be in a second JAR file.

I do realize I could have separate projects, but I wanted to see if it was possible to keep the project grouped together still as that does make sense. Any help would be much appreciated.

Thank you.

Per request, the Ant Build Script (I only included the jar task - the other is just a clean):

<target name="jar" description="Deploys the JAR file." depends="clean">
    <mkdir dir="${dir.dist}">

    <!-- Build the JAR file containing the classes. -->
    <jar destfile="${dir.dist}/${build.name}.jar">

        <!-- Generate the MANIFEST.MF file. -->
       <manifest>
           <attribute name="Built-By" value="${user.name}" />
           <attribute name="Release-Version" value=${version}" />
       </manifest>

       <zipfileset dir="${dir.build}" />
   </jar>
</target>

回答1:

if you use the includes facility of the zipfileset (and some macrodef magic), you could end up with something like the following:

<macrodef name="packaging">
    <attribute name="package"/>
    <sequential>
        <jar destfile="${dir.dist}/${build.name}-@{package}.jar">
            <manifest>
                <attribute name="Built-By" value="${user.name}"/>
                <attribute name="Release-Version" value=${version}"/>
            </manifest>

            <zipfileset dir="${dir.build}" includes="com/acme/@{package}/**/>
        </jar>
    </sequential>
</macrodef>

<packaging package="package1"/>
<packaging package="package2"/>
<packaging package="package3"/>


回答2:

Can you paste the content of your build.xml file (the one used by Ant) ?

It's very simple what you have to do, but show us the build file so we can help you.

OR.. read by yourself on Ant jar task



标签: eclipse ant jar