How can I extract .class files from .jar files?

人盡茶涼 提交于 2019-12-10 13:53:52

问题


A quick question: I like the look of this bit of Ant:

<fileset dir="${lib}">  
   <patternset refid="myPattern" />  
</fileset>

So I could use this e.g. to copy a few .jar files from ${lib} that match myPattern.

What if what I really want is to look into each .jar in ${lib} and select only .class files that match myPattern?


回答1:


The unjar task does pretty much what you want here out of the box. Use a fileset to specify the jar files you want to extract from, and a patternset to specify which files to extract.

<unjar dest="${dest.dir}">
  <patternset refid="myPattern" />
  <fileset dir="${lib}" includes="*.jar" />
</unjar>



回答2:


You can use the archives resource collection to extract several files from multiple archives:

<copy todir="somedir">
    <restrict>
        <!-- This is just an example, you can use any restriction -->
        <name name="somePattern"/> 
            <archives>
                <zips>
                    <fileset dir="${lib}" includes="**/*.jar" />
                </zips>
        </archives>
    </restrict>
</copy>



回答3:


I'm pretty sure you'd need to unjar the jars and then do the copy of class files.




回答4:


This should work with antcontrib's For task

<for param="file">
  <path>
    <fileset dir="${lib}" includes="*.jar"/>
  </path>
  <sequential>
    <unjar src="@{file}" dest="${dest}">
      <patternset refid="myPattern>
    </unjar>
  </sequential>
</for>


来源:https://stackoverflow.com/questions/5144496/how-can-i-extract-class-files-from-jar-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!