Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename maven pom artifactId using IntelliJ refactoring?

Tags:

I am using IntelliJ 13 and have a Java project setup with its modules based on maven projects.

There is a maven project that I must rename, so I will have to change it's artifactId property value in the pom file.

If I do this manually, I will then have to find all its occurrences and modify those manually as well (ex: where it is included as a module and where it is used as a dependency).

Since IntelliJ provides many refactoring techniques, I thought refactor->rename on the pom artifactId property would be supported, however it does not seem to be.

I could try and do a find/replace .. but that still makes me do all the work logic (error-prone).

IMO, IntelliJ should be able to detect where the pom is included as a module and which other modules use it as a dependency - anyone know of an automatic way to do this - or am I missing something here?

like image 917
Morgan Kobeissi Avatar asked May 20 '14 10:05

Morgan Kobeissi


1 Answers

Replace in Path (Ctrl+Shift+R by default) action with text to find <artifactId>OLD-ARTIFACT-ID</artifactId> and text to replace <artifactId>NEW-ARTIFACT-ID</artifactId> will do most of the work automatically with the minimum amount of false positives, especially if you additionally configure the proper file name filter (pom.xml, obviously).

In detail, here is the complete sequence of steps to rename the maven-based module (assuming that the name of the module matches the name of the directory where the module is located):

  1. Comment out the relevant <module>OLD-ARTIFACT-ID</module> lines in parent/aggregator poms, then allow IDEA to re-import the project and allow it to remove excluded modules from the project. It is better to remove leftover .iml files too.
  2. Perform search&replace from <artifactId>OLD-ARTIFACT-ID</artifactId> to <artifactId>NEW-ARTIFACT-ID</artifactId> to rename the module in question and to fix all dependency references to it.
  3. Perform search&replace from <module>OLD-ARTIFACT-ID</module> to <module>NEW-ARTIFACT-ID</module> to fix all aggregation references to it.
  4. Rename the module directory.
  5. Uncomment the relevant <module>NEW-ARTIFACT-ID</module> lines in parent/aggregator POM files, then allow IDEA to re-import the project.

If you feel like IDEA misses some useful functionality you should create a feature request at http://youtrack.jetbrains.com/. In this particular case, vote for IDEA-94223, IDEA-72181 and IDEA-104344.

like image 61
Oleg Estekhin Avatar answered Sep 20 '22 11:09

Oleg Estekhin