管理在运行时从类路径加载数据的 Java 类的数据依赖性

发布于 2024-09-02 06:40:31 字数 382 浏览 4 评论 0原文

管理 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

め七分饶幸 2024-09-09 06:40:31

这是 Maven 已经通过其构建、依赖和资源管理解决的众多问题之一。任何 Maven 项目都遵循标准目录布局,该布局规定了应将数据文件放置在何处:“资源”目录中。传统的 Maven 目录结构如下...

/
/src/
/src/main/java/
/src/main/java/App.java
/src/main/resources/
/src/main/resources/my.prod.data.or.cfg.or.whatever
/src/test/java/
/src/test/java/AppTest.java
/src/test/resources/
/src/test/resources/my.test.data.or.cfg.or.whatever
/pom.xml

这样做的好处是“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...

/
/src/
/src/main/java/
/src/main/java/App.java
/src/main/resources/
/src/main/resources/my.prod.data.or.cfg.or.whatever
/src/test/java/
/src/test/java/AppTest.java
/src/test/resources/
/src/test/resources/my.test.data.or.cfg.or.whatever
/pom.xml

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.

却一份温柔 2024-09-09 06:40:31

我认为您描述的系统不存在通用解决方案,但是,我只是尝试使用 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.

温柔戏命师 2024-09-09 06:40:31

难道您不能重构您的项目,以便拥有每个子模块都包含项目本身的相关文件吗? 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文