简体   繁体   English

Java中使用注释的依赖管理

[英]Dependency Management in java using annotations

I use maven quite extensively on all my java projects (and some non-java projects as well). 我在所有Java项目(以及一些非Java项目)中都广泛使用了Maven。 However, I am a big fan of moving configuration out of XML files and into the java source files using annotations. 但是,我非常喜欢使用注解将配置从XML文件移到Java源文件中。 I recently found the following on the Groovy Grape page: 我最近在Groovy Grape页面上找到了以下内容:

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. 尽管这是Groovy代码,但在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". 虽然似乎不太可能(或将要)将其内置到javac本身中,但可以将其内置到Maven或其他一些构建“包装器”中。

What are your thoughts? 你怎么看?

EDIT: It just occurred to me that annotations are not valid on import statements in java. 编辑:只是我想到注释在Java中的导入语句上无效。 This does present a problem... 这确实存在问题...

You can build an annotation preprocessor to analyze your source for these types of annotations. 您可以构建注释预处理器,以分析这些注释类型的来源。 Check out the Javadocs here . 此处查看Javadocs。

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. 但是,您可能会成为Gradle的忠实拥护者。 Though it's not exactly annotations, Gradle addresses most of your concerns. 尽管它不完全是注释,但Gradle可以解决您的大多数问题。

http://www.gradle.org/ http://www.gradle.org/

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). 从技术上讲,可以在任何基于注释和模块结构的语言(groovy,python,antlr)上编写* .java文件预处理器,该语言将处理文件并生成pom.xml(最好只是填充现有pom.xml中的依赖项)。 (注释应注释为不干扰Java编译器和IDE突出显示,例如旧的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? 如果要更改Spring版本会怎样? Should you scan all project files and replace it and after that recompile. 您应该扫描所有项目文件并替换它,然后重新编译。 Build and configuration should be separated from code. 构建和配置应与代码分开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM