如何自动构建 Java 类及其依赖的所有类?
我想这是 问题 1522329。
该问题讨论了通过 java -verbose:class 选项获取运行时使用的所有类的列表。
我感兴趣的是自动构建 JAR 文件,其中包含我的类以及它们依赖的所有其他类。通常,这就是我使用来自某些第三方开源产品的“客户端逻辑”的代码的地方,但他们没有提供一组干净的客户端 API 对象。他们的完整代码集位于服务器端,但我只需要必要的客户端位。
这似乎是一个常见问题,但我还没有看到任何对此有帮助的东西(例如在 Eclipse 中)。我错过了什么吗?
当然,我仍然可以通过以下方式手动完成:硬着头皮将所有第三方代码包含在一个巨大的 JAR 中(冒犯了我的纯粹主义情感)/源代码演练/试验和错误/-verbose:class输入内容(但后者在我的代码作为 J2EE servlet 的一部分运行的情况下不起作用,因此我只想针对给定的 Tomcat web 应用程序看到此内容,并且理想情况下,仅针对与 my 相关的类 其中的类)。
I guess this is kind of a follow-on to question 1522329.
That question talked about getting a list of all classes used at runtime via the java -verbose:class option.
What I'm interested in is automating the build of a JAR file which contains my class(es), and all other classes they rely on. Typically, this would be where I am using code from some third party open source product's "client logic" but they haven't provided a clean set of client API objects. Their complete set of code goes server-side, but I only need the necessary client bits.
This would seem a common issue but I haven't seen anything (e.g. in Eclipse) which helps with this. Am I missing something?
Of course I can still do it manually by: biting the bullet and including all the third-party code in a massive JAR (offending my purist sensibilities) / source walkthrough / trial and error / -verbose:class type stuff (but the latter wouldn't work where, say, my code runs as part of a J2EE servlet, and thus I only want to see this for a given Tomcat webapp and, ideally, only for classes related to my classes therein).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议使用 Ant 或 Maven 等构建系统。 Maven 是根据 Java 设计的,也是我专门使用的。您甚至可以让 Maven 将所有依赖类组装(使用组装插件)到一个大的 jar 文件中,这样您就不必担心依赖关系。
http://maven.apache.org/
编辑:
关于servlet,您还可以定义 您想要与 jar 一起打包的依赖项,如果您正在制作一个独立的应用程序,您可以让 jar 工具制作一个可执行的 jar。
注意:是的,我是 Maven 的拥护者,因为它使我从事的项目变得更加容易。不,我个人不参与该项目。 :)
I would recommend using a build system such as Ant or Maven. Maven is designed with Java in mind, and is what I use pretty much exclusively. You can even have Maven assemble (using the assembly plugin) all of the dependent classes into one large jar file, so you don't have to worry about dependencies.
http://maven.apache.org/
Edit:
Regarding the servlet, you can also define which dependencies you want packaged up with your jar, and if you are making a stand alone application you can have the jar tool make an executable jar.
note: yes, I am a bit of a Maven advocate, as it has made the project I work on much easier. No I do not work on the project personally. :)
看看 ProGuard。
Take a look at ProGuard.
你想要的不仅是你所依赖的类,还包括你所依赖的类,所依赖的类。等等,等等。
所以这实际上并不是一个构建问题,而更多的是一个依赖问题。要回答你的问题,你可以使用 Maven(显然)或 Ant + Ivy 来解决这个问题。
我与 Ivy 合作,有时使用 Ant Jar 任务的 zipgroupfileset 功能构建“ueber-jar”。可能会说不太优雅,但 10 秒内就完成了:-)
What you want is not only to include the classes you rely on but also the classes, the classes you rely on, rely on. And so on, and so forth.
So that's not really a build problem, but more a dependency one. To answer your question, you can either solve this with Maven (apparently) or Ant + Ivy.
I work with Ivy and I sometimes build "ueber-jar" using the zipgroupfileset functionality of the Ant Jar task. Not very elegant would say some, but it's done in 10 seconds :-)