5

I'm using Intellij-IDEA 11. If there's a solution that works in 12, I'd be willing to upgrade for that.

I built a library that many projects depend on but the library changes often. This library is added to my project's pom.xml as a normal dependency:

    <dependency>
        <groupId>my.company</groupId>
        <artifactId>MyLibrary</artifactId>
    </dependency>

This is a typical series of steps I need to take when I add a feature to my project:

  1. Modify some code in my project
  2. Modify some code in the library I depend on
  3. Check to see if the feature works
  4. If not, repeat

Outside of my IDE (which happens to be Intellij-IDEA), this is a straight forward process: I add step 2.5 which is to cd into the dependency's project folder and then mvn install the library.

But I don't know a convenient way to do this in Intellij-IDEA. Ideally, I'd modify the source of the library inside Intellij, click the green play button and the IDE would figure out to use the modified sources of the library.

Right now, since the library is a dependency, it just uses what's already in my local repo. So modifying the source of the library doesn't have an affect until I manually mvn install it.

Note: I've figured out a trick that seems to work. If I modify the project's parent pom to include the library as a maven module and then include the same library as an intellij module, it uses that source instead of the jar in my local repo. But I don't like this solution because I'd never want to commit that modified pom (because the module has to point to a file system path that only exists on my local file system).

I've found a bug in the Intellij-IDEA bug tracker that seems to relate to this: http://youtrack.jetbrains.com/issue/IDEA-25146

4 Answers 4

2

I found out a way to do this. First, you add the library's pom.xml in the Maven Projects tool window. Then you Edit your configurations so that they run the install goal of your library. And image of how to do this is provided below.

Click here for a full sized link How to mvn install a library before you run

Sign up to request clarification or add additional context in comments.

2 Comments

ok... this solution just launch maven from your IDE. It don't use the code compiled by IDEA. If you debug and perform "update classes and resources" your code won't be updated.
That is true and it's a big downside.
1

Open module setting (F4 on a module), go to Modules, select the dependencies tab in module that depepends on your library.

If the library is a module add a module dependency If the library isn't a module, I assume it gets built and put somewhere, add that directory to the module dependencies.

The higher up the list the higher its priority

2 Comments

This didn't work for me when I tried it. Even though I put the Intellij module dependency at the top, it seems to be using the code from my local repo instead of the code from the module dependency. I tested this by modifying a class in the module depdency to log a message, started the app and saw that there was no output.
This is like a heisenbug, but this was temporarily working when I got in to work this morning. I don't know why it wasn't working when I originally tried. After that I tried follow your steps and it's not working again. One thing I notice is that every time I restart Intellij, it removes the module dependency which is really annoying. Either this is an Intellij bug, or it's doing it because it clears all the dependencies then gets that information from the poms
0

I've similar architecture and here is what I do (and it works very well).

  1. Create an IDEA maven project based on the pom.xml of my main project (the project using the frequently changing library).
  2. Add maven project to my main project (click on the "+" icon in the maven projects tab and choose the pom.xml of my frequently changing library)

EDIT

note: all the modules (from the library and from the main project) have a *-SNAPSHOT version

In more details, the pom.xml of my library project :

<groupId>com.company</groupId>
<artifactId>mylibrary</artifactId>
<version>2.0.1-SNAPSHOT</version>

And the pom.xml of my main project

<groupId>com.company</groupId>
<artifactId>myproject</artifactId>
<version>1.1-SNAPSHOT</version>
<dependencies>
    <dependency>
        <groupId>com.company</groupId>
        <artifactId>mylibrary</artifactId>
        <version>2.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>

I think that IDEA is smart enough to see that the dependency is a SNAPSHOT and that this library is in the current IDEA project (i.e. same IDEA window)... and so IDEA don't look in the local repo since the SNAPSHOT is in the current "workspace".

Hope it clarify the situation.

If I need to run something, I have different options either

  1. defining a new maven run configuration in IDEA to run any maven goal (from any pom.xml in my IDEA project) with any profile.
  2. defining a simple unit test configuration in IDEA (not directly linked with maven except that the classpath is derived from the classpath defined in pom.xml)
  3. defining a new Tomcat/JBoss configuration in IDEA and deploying some artifact in it. (IDEA detect deployable artifacts -like war or ejb- from the pom.xml)

6 Comments

I assume you have a step 3: "I manually install the library from IDEA". And then a step 4: "I run the main project". For me this isn't ideal, especially because the project has 5+ child modules and it's a little more work than I'd like to find the library's pom to run install.
I don't have the step 3 (no need to install manually the library). Step 4 : yes I run the main project and/or unit tests that are either part of the main project either part of the library. (note that all the modules (from the library and from the main project) have a *-SNAPSHOT version)
How does intellij know to run that library pom.xml? How does it know which goal of it to run? Unless you're running code in the library itself, I don't see why intellij would decide to use that pom for anything.
I've read your edit, but in my experience this doesn't work. In fact, I already had this setup before I asked the question. It still uses the local repo instead of the compiled classes. I find that intellij isn't "smart enough" to use the library until you use the trick I put in my "Note" at the bottom of my question.
Do you use SNAPSHOT version for the artifacts you are updating the code ?
|
0

I have the same scenario than you and I am using Intellij 2016.02.

Here is how I solved it:

1 - On Intellij open the maven panel going to View -> Tool Windows -> Maven Projects, then click at the plus green button (add maven project) and select the pom.xml file from the dependency project

2 - On Run/Debug configuration settings, add a before launch task as a maven goal, choosing the Working Directory of the dependency project and in command line just type "install".

3 - Now edit anything in your dependency project, and redeploy your main application, and the changes may affect.

EDITED:

I found a new solution, and it's solved the "update classes and resources" downside, here is what I did:

1 - Do the first step from the previous solution, but remove the install goal from before launch task if you add it on step 2.

2 - Edit the exploded artifact, and add the dependency project "compiled output" element from "Available elements" panel to output left panel.

Changes may work without need to install, and now my "Update classes and resources" works like a charm :)

3 Comments

Isn't that the answer I gave? What's the difference?
You're right, I did not understand your instructions neither your printscreen, but now, I see it is the same. I just tried to help you, sorry.
I update my answer with a new solution that works better for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.