Ant including both jar files and .class files in d

2019-06-14 15:00发布

问题:

I have defined a jar task using ant build.xml. I need to bundle all the dependencies into my jar. I don't understant why ant extracts the jars and includes both .jars and .class files into my jar. It unnecessarily increases the size of my jar file. Here is my jar task.

<jar destfile="build/main/ERS2SupportingUtilities.jar">

            <fileset dir="target/classes">
                <exclude name="*.sh"/>
            </fileset>

            <restrict>
             <name name="**/*.class"/>
             <archives>
               <zips>
                 <fileset dir="./src/main/resources/lib" includes="**/*.jar"/> 
               </zips>
             </archives>
            </restrict>

            <manifest>
              <attribute name="Main-Class" value="com.cibc.ers2.invoker.JobTest"/>
                <attribute name="Class-Path" value="./lib/log4j-1.2.16.jar
                                                    ./lib/org.springframework.context-3.0.3RELEASE.jar
                                                    ./lib/org.springframework.asm-3.0.3.RELEASE.jar
                                                    ./lib/junit-4.7.jar
                                                    ./lib/org.springframework.orm-3.0.3.RELEASE.jar
                                                    ./lib/org.springframework.transaction-3.0.3.RELEASE.jar
                                                    ./lib/org.springframework.aspects-3.0.3.RELEASE.jar
                                                    ./lib/commons-pool-1.5.4.jar
                                                    ./lib/org.springframework.core-3.0.3.RELEASE.jar
                                                    ./lib/commons-logging-1.1.1.jar
                                                    ./lib/HashUtility.jar
                                                    ./lib/org.springframework.expression-3.0.3.RELEASE.jar
                                                    ./lib/commons-lang-2.6.jar
                                                    ./lib/org.springframework.instrument-3.0.3.RELEASE.jar
                                                    ./lib/mockito-all-1.9.5.jar
                                                    ./lib/com.springsource.org.aopalliance-1.0.0.jar
                                                    ./lib/ojdbc14.jar
                                                    ./lib/commons-io-2.4.jar
                                                    ./lib/commons-collections-3.1.jar
                                                    ./lib/org.springframework.jdbc-3.0.3.RELEASE.jar
                                                    ./lib/spring-batch-infrastructure-2.1.9.RELEASE.jar
                                                    ./lib/org.springframework.context.support-3.0.3.RELEASE.jar
                                                    ./lib/commons-dbcp-1.4.jar
                                                    ./lib/spring-batch-test-2.1.9.RELEASE.jar
                                                    ./lib/org.springframework.beans-3.0.3.RELEASE.jar
                                                    ./lib/org.springframework.oxm-3.0.3.RELEASE.jar
                                                    ./lib/org.springframework.aop-3.0.3.RELEASE.jar
                                                    ./lib/commons-beanutils.jar
                                                    ./lib/org.springframework.binding-2.1.1.RELEASE.jar
                                                    ./lib/spring-batch-core-2.1.9.RELEASE.jar
                                                    ./lib/org.springframework.test-3.0.3.RELEASE.jar
                                                    ./launch-context.xml
                                                    ./log4j.xml"
                />
            </manifest>
        </jar>

I have also tried zipgroupfileset but that also is giving the same problem.

EDIT

Apologies for not giving enough info about what I am trying to achieve. I have got an application which I am compilint @ target/classes. I need to package this application including my class files and dependecies into one jar.

回答1:

 <fileset dir="target/classes">
     <exclude name="*.sh"/>
 </fileset>

includes all files from target/classes which are not shell scripts.

<restrict>
    <name name="**/*.class"/>
    <archives>
         <zips>
             <fileset dir="./src/main/resources/lib" includes="**/*.jar"/> 
          </zips>
     </archives>
</restrict>

It seems like you again include classes and jars here.

If you want only jar change all above to :

<zipfileset dir="./src/main/resources/lib" includes="*.jar"/>