14

i'm working on a project in which i have an android application project which has pom dependencies on other projects something like this:

<dependencies>
        <dependency>
            <groupId>some.project/groupId>
            <artifactId>some.artifact<artifactId>
            <version>1.0.0</version>
            <type>apklib</type>
        </dependency>
</dependencies>

while developing i always want Intellij to use the local source code from the some.project library i have in the project.
the problem is it will pull version 1.0.0 from the server and use that.
i tried using LATEST as version, this works fine as long as my some.project library version is updated to the latest version on the server.
since we have automated builds that will increment the version for some.project when changes are made i will have a scenario in which my source says 1.0.0 but the server has 1.0.1 and again intellij will pull the server apklib

Any suggestions how to achieve this? if i could use a dev profile to somehow define this for development only and a prod profile to use exact version numbers (which it should) that will be amazing.

Thanks!

talarari
  • 539
  • 3
  • 7
  • 19
  • 1
    I don't suppose you've figured this out in the four years since you've asked it? I tried all four answers and none of them worked. – micseydel May 16 '18 at 19:24

6 Answers6

14

You can add a module dependency in IntelliJ: File->Project Settings->Modules click on the module -> click Dependencies tab and then click the green '+' sign and choose '3. Module dependency'.. add the module on which you want to depend locally and click the blue arrows to bring the module above the Maven dependencies.

dmossakowski
  • 191
  • 1
  • 4
  • Unfortunately, that's the only I'm able to make it work with maven version range for now. :( – nicou50 Sep 08 '16 at 19:15
  • It's probably a problem with the project I'm working on, but when I try this I get > Source root "PATH" cannot be defined in module "root" because it belongs to content of nested module "common" – micseydel May 16 '18 at 18:26
  • Easier way to get to the same screen: click on project and press F4, or right click on project and select "Open Module Settings" – T Tse Jun 25 '21 at 18:01
10

There is a checkbox "resolve Workspace artifacts" under Maven Run/Debug Configuration, select it then it'll work as expected.

Jin Mengfei
  • 313
  • 3
  • 9
  • 14
    it's so annoying to get downvoted without a comment, at least tell me what's wrong in the answer, downvoter. – Jin Mengfei Mar 03 '16 at 09:27
  • 3
    Not the downvoter, but I think this is only part of the solution. I think you also have to ensure that you're using the right versions in the pom. – adam_0 Sep 07 '16 at 17:28
  • 3
    That worked for me. But I think the _resolve Workspace artifacts_ setting is only for the maven run configuration. The question was to achive this _while developing_ - not sure what task @talarari had in mind – ingo Nov 23 '17 at 15:05
2

We are dealing with the same problem...

"IDEA should resolve dependencies as a module dependency type (rather than a local jar Maven library) if this Maven project is opened in IDE and it's version matches the version, installed in a local Maven repository."

https://intellij-support.jetbrains.com/hc/en-us/community/posts/206836605-Always-prefer-local-module-source-over-Maven-dependencies

Our solution was to add local Maven profile in pom.xml and specify local dependency version. To make it work, you have to turn on local profile.

<profiles>
    <profile>
        <id>local</id>
        <properties>
            <dependency.version>x.x.x</dependency.version>
        </properties>
    </profile>
</profiles>

When local dependency is updated, you have to take care to update this version too, but currently this is the best solution we found and it's working for us.

frenky
  • 76
  • 1
  • 6
0

Assuming that you have the project organised as a multi-module project, and that you're talking about a dependency on a library that's one of the project's modules, then you need to use a snapshot dependency, and you need to use the full maven release process. As it is, you're depending on a pre-built, static version of the given library, but you need IntelliJ to look at the source and class you compile instead, and snapshot dependencies are used exactly for this purpose.

Software Engineer
  • 15,457
  • 7
  • 74
  • 102
  • 2
    Thanks for the reply, we are using snapshot dependencies, so the dependency is in fact on 1.0.0-snapshot. the thing is, intellij will download the latest snapshot libs instead of using my local code (all the modules are in my project) – talarari May 11 '14 at 09:00
0

This works for Maven projects in the following way:

  • Add the dependencies that you want to work with to the Maven Projects tab.
  • Navigate to the implementation of the class for which you are looking.
  • Once you are in the implementation, you can navigate back to the Super Method for the declaration.

That's either Navigate -> Implementation(s) or ALT+COMMAND+B (on the Mac map). Intellij will then present you with the option of decompiling the class from the JAR in the dependency list or the source code in the attached Maven project.

For example, I am in project bank of the Rooskie Bank, and I want to view the code for the Account class in bank-domain. I click on an Account object declaration and press ALT+COMMAND+B. From the popup menu, I choose the definition from the list of sources that does not have "(Maven: com.rooskiebank.bank-domain.jar)" in the choice.

IMO this is an awkward way of doing things. As much as I like it, I find Intellij is difficult to use in enterprise-sized systems with multiple in-house libraries, and I still use Eclipse a lot for them. Intellij works better with Git and is great for stand-alone microservices.

I imagine Gradle works in a similar way.

Steve Gelman
  • 874
  • 9
  • 16
0

Just a small hint how to do the same with Gradle. Supposing you have two workspaces with lib and app. The lib is deployed to some maven repo and needs to be described in the following way:

// build.gradle
group 'lib.group'
version 'lib.version'

// settings.gradle
rootProject.name = 'lib.name'

While app dependencies are the following:

// build.gradle
compile "lib.group:lib.name:lib.version"

So, the app module uses lib as a maven dependency. Now, you can import lib sources into app workspace using File | New | Module from existing sources. After refreshing the project from Gradle tool window, you will get two modules lib and app in the app workspace. However, even after reimporting Gradle the lib dependency will still point to the maven repository, what you can check in File | Project Structure.

The trick to be done here is to click with the right mouse button on app module in the Gradle tool window, and select Composite Build Configuration option. Then, on the popup window just select the local lib module to include it in the app module build. Now, after reimporting Gradle deps again for the whole workspace you will get the maven dependencies for lib replaced with the local dependency.

Lukasz Frankowski
  • 2,955
  • 1
  • 31
  • 32