java中使用注解的依赖管理
我在所有 Java 项目(以及一些非 Java 项目)中广泛使用 Maven。然而,我非常喜欢使用注释将配置从 XML 文件移到 java 源文件中。我最近在 Groovy Grape 页面上发现了以下内容:
添加依赖项
@Grab(group='org.springframework', module='spring', version='2.5.6')
import org.springframework.jdbc.core.JdbcTemplate
尽管这是 Groovy 代码,但似乎上述内容在 Java 中并非完全不可能。虽然这似乎不太可能(或将)内置到 javac 本身中,但它可以内置到 Maven 或其他一些构建“包装器”中。
你有什么想法?
编辑:我刚刚想到注释在 java 中的 import 语句上无效。这确实存在一个问题...
I use maven quite extensively on all my java projects (and some non-java projects as well). However, I am a big fan of moving configuration out of XML files and into the java source files using annotations. I recently found the following on the Groovy Grape page:
Add a Dependency
@Grab(group='org.springframework', module='spring', version='2.5.6')
import org.springframework.jdbc.core.JdbcTemplate
Although this is Groovy code, it seems the above shouldn't be entirely impossible in Java. While it seems unlikely that this could (or would) be built into javac itself, it is something that could be built into Maven or some other build "wrapper".
What are your thoughts?
EDIT: It just occurred to me that annotations are not valid on import statements in java. This does present a problem...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以构建一个注释预处理器来分析这些类型注释的源。 此处查看 Javadocs 。
You can build an annotation preprocessor to analyze your source for these types of annotations. Check out the Javadocs here.
我认为您不喜欢用依赖注释来注释所有类的想法。这就是为什么将类分组为称为模块或项目的单元的原因,以便一个工件的导入或依赖项解析可在特定组或模块内的所有类中使用。
然而,您很可能会成为 Gradle 的忠实粉丝。虽然它不完全是注释,但 Gradle 解决了您的大部分问题。
http://www.gradle.org/
I don't think you would like the idea of annotating all your classes with dependency annotations. This is the reason why the classes are grouped into units called modules or projects so that the import or dependency resolution of one artifact is usable across all the classes within the particular group or module.
However, you are likely to become a big fan of Gradle. Though it's not exactly annotations, Gradle addresses most of your concerns.
http://www.gradle.org/
从技术上讲,可以在任何语言(groovy、python、antlr)上编写 *.java 文件预处理器,该语言将处理文件并基于注释和模块结构生成 pom.xml(最好只填充现有 pom.xml 中的依赖项部分) (应该对注释进行注释,以免干扰 java 编译器和 IDE 突出显示,就像旧的 XDoclet 一样)。我想这是你自己项目的想法)。
但从我的角度来看,这是个坏主意。如果你想更改 spring 版本会发生什么?您是否应该扫描所有项目文件并替换它,然后重新编译。构建和配置应该与代码分开。
Technically it is possible to write *.java files preprocesser on any language(groovy, python, antlr) that will process files and generate pom.xml(better just fill dependency section in existed pom.xml) on the base of annotations and module structure(annotations should be commented to do not disturb java compiler and IDE highlighting, like old XDoclet). I suppose this is idea for your own project).
However from my point of view it is bad idea. What happens if you want to change spring version? Should you scan all project files and replace it and after that recompile. Build and configuration should be separated from code.