管理在运行时从类路径加载数据的 Java 类的数据依赖性
管理 Java 类对类路径中存在的数据文件的依赖关系的最简单方法是什么?
更具体地说:
数据依赖关系应该如何标注?也许使用 Java 注释(例如,@Data)?或者更确切地说,构建脚本或属性文件中的一些构建条目?是否有构建工具可以集成和评估这些信息(Ant,Scons,...)?你有例子吗?
考虑以下场景:
Ant 的几行代码从我的源代码中创建了一个 Jar,其中包含在类路径上找到的所有内容。然后使用 jarjar 删除执行不需要的所有 .class 文件,例如 Foo 类。问题是 Bar 类所依赖的所有数据文件仍然存在于 Jar 中。然而,理想的部署脚本会认识到,可以删除仅类 Bar 所依赖的数据文件,而必须保留类 Foo 所依赖的数据文件。
有什么提示吗?
What is the simplest way to manage dependencies of Java classes to data files present in the classpath?
More specifically:
How should data dependencies be annotated? Perhaps using Java annotations (e.g., @Data)? Or rather some build entries in a build script or a properties file? Is there build tool that integrates and evaluates such information (Ant, Scons, ...)? Do you have examples?
Consider the following scenario:
A few lines of Ant create a Jar from my sources that includes everything found on the classpath. Then jarjar is used to remove all .class files that are not necessary to execute, say, class Foo. The problem is that all the data files that class Bar depends upon are still there in the Jar. The ideal deployment script, however, would recognize that the data files on which only class Bar depends can be removed while data files on which class Foo depends must be retained.
Any hints?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是 Maven 已经通过其构建、依赖和资源管理解决的众多问题之一。任何 Maven 项目都遵循标准目录布局,该布局规定了应将数据文件放置在何处:“资源”目录中。传统的 Maven 目录结构如下...
这样做的好处是“main”(prod)资源目录中包含的所有文件都可以在运行时从类路径供您的应用程序使用。所有“测试/资源”文件在构建和部署期间都可供您的代码使用。单元测试时间,但不包含在您的最终工件中。
This is one of the many problems Maven has already solved with it's build, dependency, and resource management. Any maven project follows a standard directory layout which dictates where you should put your Data files: in the 'resources' directories. The conventional Maven directory structure is as follows...
The benefit of this is that all files which are contained in the 'main' (prod) resources directories are available to your application at run-time from the Classpath. All of the 'test/resources' files are available to your code during build & unit test time but are NOT included in your final artifact.
我认为您描述的系统不存在通用解决方案,但是,我只是尝试使用 ASM,因为 jarjar 也使用它。通过这种方式读取注释数据并不难(将 ClassVisitor 传递给 ClassReader 上的accept() 方法,并在visitAnnotation 回调中执行一些有用的操作)。这意味着您可以尝试将您的预期行为包含到 jarjar 中,也可以将其作为自定义步骤添加到构建过程中。
I don't think a generic solution exists for the system you describe, however, I just had a stab at reading annotations on classes using ASM, since that is used by jarjar as well. It is not hard to read the annotation data that way (pass in a ClassVisitor to the accept() method on ClassReader, and do something useful on the visitAnnotation callback). This means you can either try and include your intended behavior to jarjar or you could add it as a custom step to your build process.
难道您不能重构您的项目,以便拥有每个子模块都包含项目本身的相关文件吗? Bar 类和 Bar 相关文件将打包在它们的包中,而 Foo 的文件将打包到另一个包中?
另一种可能性是使用一些包命名约定来过滤您想要在包中查看的文件。
Can't you refactor your project so that you have submodules that each contain the relevant files for the project itself ; Bar class and Bar related files will be packaged in their bundle while Foo ones will packed into another?
Another possibility would be to use some package naming convention to be able to filter the files you want to see i your bundles.