简体   繁体   English

属性中的自动完成依赖项版本

[英]Auto-complete dependency version in properties

In my pom, I have extracted the version of a dependency as a property like this: 在我的pom中,我将依赖项的版本提取为如下属性:

<properties>
  <slf4j.version>1.7.21</slf4j.version>
</properties>

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>${slf4j.version}</version>
</dependency>

However, by doing that, I lose the auto-completion that I would get if I entered the version directly in the dependency group. 但是,这样做会丢失我直接在依赖项组中输入版本时获得的自动完成功能。

Is there a trick to get auto-completion of the version when using a property like above? 使用上面的属性时,是否有技巧来自动完成版本?

Note: I'm equally interested in answers for Netbeans and Intellij. 注意:我同样对Netbeans和Intellij的答案感兴趣。

A quick way in IntelliJ, is to click inside the <dependencies></dependencies> , and, just type dep like this: 在IntelliJ中,一种快速的方法是在<dependencies></dependencies>单击,然后像这样键入dep

<dependencies>
    dep
</dependencies>

then hit tab . 然后点击tab You will then see something like this: 然后,您将看到以下内容:

<dependency>
        <groupId></groupId>
        <artifactId></artifactId>
        <version></version>
</dependency>

with the cursor inside of the artifactId tab. 将光标放在artifactId标签内。 Start typing your artifact name, eg slf , and the list of choices will narrow down until you see the one you want. 开始输入工件名称,例如slf ,选项列表将缩小,直到您看到所需的名称为止。 Select that, and it will fill in the artifactId, and move the cursor to groupId . 选择该选项,它将填充artifactId,并将光标移动到groupId It will probably be the one you want, so just hit enter . 可能是您想要的那个,所以只需按Enter即可 Then the cursor will move to the version tag, and show a list of available version numbers. 然后,光标将移至version标签,并显示可用版本号的列表。 Select the version number and it will end up looking something like this: 选择版本号,它将最终看起来像这样:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.18</version>
</dependency>

I want the version number to be specified in dependencyManagement,dependencies , not build,dependencies so I tell IntelliJ to extract what it can into dependencyManagement, by hitting ctrl alt M or opt cmd M , which leaves only the group and artifact in build dependencies, and puts all 3 values in dependencyManagement dependencies. 我希望版本号在dependencyManagement,dependencies指定dependencyManagement,dependencies不是build,dependencies因此我告诉IntelliJ通过按ctrl alt Mopt cmd M将其可以提取的内容提取到dependencyManagement中,这仅将组和工件保留在生成依赖项中,并将所有3个值放入dependencyManagement依赖项中。

Then, in dependencyManagement dependencies, I click on the version number and use ctrl alt V or opt cmd V to replace the version number with a property. 然后,在dependencyManagement依赖关系中,单击版本号,然后使用ctrl alt Vopt cmd V将其替换为属性。

Important - use the name it suggests for the property name. 重要 -使用建议的名称作为属性名称。 If you do, then you will be able to use 如果您这样做,则可以使用

mvn versions:display-property-updates

which will look in your repositories for newer versions and list them. 它将在您的存储库中查找较新的版本并列出它们。

The IDE can't auto complete a property because it doesn't know the context in which the property is used. IDE无法自动完成属性,因为它不知道使用该属性的上下文。 A property value could be used in various locations in the POM file for different dependencies. 可以在POM文件中的不同位置使用属性值,以实现不同的依存关系。

If you want to keep auto-complete you can't have a property 如果您想保持自动完成状态,就不能拥有房产

EDIT: Tried in NetBeans and IntelliJ and you can't autocomplete a property in a POM file. 编辑:在NetBeans和IntelliJ中尝试过,您不能自动完成POM文件中的属性。

On IntelliJ, first create your dependency with the version for auto completion 在IntelliJ上,首先使用版本自动创建依赖项

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.21</version>
</dependency>

Then underline the version and use the shortcut Ctrl+Alt+v 然后在版本下划线并使用快捷键Ctrl+Alt+v

Or with your mouse Right Click -> Refactor -> Extract -> Property 或用鼠标Right Click -> Refactor -> Extract -> Property

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

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