Issue
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.
Solution
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, e.g. 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 ctrlaltM or optcmdM, 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 ctrlaltV or optcmdV 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.
Answered By - Hank D