简体   繁体   中英

Auto-complete dependency version in properties

In my pom, I have extracted the version of a dependency as a property like this:

<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.

A quick way in IntelliJ, is to click inside the <dependencies></dependencies> , and, just type dep like this:

<dependencies>
    dep
</dependencies>

then hit tab . You will then see something like this:

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

with the cursor inside of the artifactId tab. Start typing your artifact name, eg slf , and the list of choices will narrow down until you see the one you want. Select that, and it will fill in the artifactId, and move the cursor to groupId . It will probably be the one you want, so just hit enter . Then the cursor will move to the version tag, and show a list of available version numbers. 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.

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.

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. A property value could be used in various locations in the POM file for different dependencies.

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.

On IntelliJ, first create your dependency with the version for auto completion

<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

Or with your mouse Right Click -> Refactor -> Extract -> Property

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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