Exclude Jar from Ant Build using zipgroupfileset

2020-07-24 04:08发布

问题:

I have created a Jar Project which contains Filter. I need to use servlet-api.jar when I compile the Filter code.

But when I want to use the Jar in another application, I want to build the jar in such a way that it will exclude servlet-api.jar.

I am using Apache Ant with NetBeans 8:
I have added below code in build.xml.

<target name="-post-jar">
    <jar destfile="dist/MyJar.jar">
        <zipfileset src="dist/MyJarAsLibrary.jar"/>
        <zipgroupfileset dir="dist/lib/." excludes="javax/*"/>
    </jar>
</target>

I have used Apache Ant zipgroupfileset. But I searched on google I found the excludes for files. I tried with excluding the servlet packages as javax/*, but it did not work. No Idea how to exclude specific packages. I found few excludes with zipfileset, but I need to use zipgroupfileset and exclude the javax.servelt.* packages.

回答1:

The excludes attribute of zipgroupfileset is used to select the archives to pick entries from, not to filter the contents of the archives. zipgroupfileset always contains everything included inside the matched archives.

The simple solution if you know which archive contains the servlet classes is to exclude it from the zipgroupfileset and add an explicit zipfileset.

If you don't know the archive, you can mimic zipgroupfileset using the archives resource collection and wrap that into a restrict container. Something like

<restrict>
  <archives>
    <zips>
      <fileset dir="dist/lib/"/>
    </zips>
  </archives>
  <not>
    <name name="javax/servlet/*"/>
  </not>
</restrict>