Can Intellij IDEA automatically replace Maven dependencies with module dependencies? - java

I'm working with a Java project in Intellij IDEA where we have a lot of internal, standalone, Maven dependencies.
I have the source code for these dependencies imported into my IDEA project as modules, but when I select Go To -> Implementation(s) on a class that is part of an internal Maven dependency (and that I have also imported as a module in my project) I'm navigated to a decompiled view of the class in the Maven dependency (jar) and not the source code file of that class that I imported as a module.
To solve this I need to do the following:
Open Module Settings on the module using the internal Maven dependency
Find and remove the internal Maven module in the Dependencies list
Add a new Module Dependency and then select the imported module that corresponds to the internal Maven dependency
Is there any way to have IDEA figuring out this automatically or do I have to do this manual procedure over and over?

To add another maven project (that is not part of your main project) as a dependency:
open you main project
in Maven Projects Tool Window click on green + button and select pom.xml of a dependency
repeat 2 as required
Source:
https://www.jetbrains.com/idea/help/maven-projects-tool-window.html?search=maven%20project

Lets say you have a maven module A which has module B as a dependency.
If you import both modules into one IntelliJ project, IntelliJ will automatically use the imported module B instead of the maven artifact from repository - but you must import it as a maven module.
If you then run some test or application via IntelliJ from module A, it will use it's own compiled classes from module B and not the maven artifact from repository, navigation in the code and everything else will work as expected.
If this does not work, then you should report a bug.

I'd suggest that you are using intellij incorrectly in this example. If you are referring to your maven dependencies as intellij modules, it will get confused.
You should either:
Change these maven dependencies to be maven modules.
Remove these intellij modules as intellij modules and set up your maven build so you can download the source from your maven dependencies.

Related

Building an artifact from a modified maven project with Intellij

i am trying to build an artifact from a modified maven project imported in intellij (v2016.2).
Have managed to:
(1) Import/build a multi module maven project in intellij
(2) Successfully built/run an artifact as a single jar from the project
(3) Modified maven project by writing new classes with extra depedencies (via changing project's structure and adding new maven dependencies*)
(4) Successfully built & run modified project
however, when i try to run an artifact from the modified project (and even if it contains all the dependencies in the jar), it completely ignores all the extra functionality of the modified project (runs exactly as in (2))
What am I missing?
*note: havent touched any poms till now. All the dependencies added using File>Project Structure>Modules>Dependencies>add Library ...
If you don't see any change when running the project it's because you run the the previous build.
"Add Library" won't update your pom.xml. (Add maven dependency... will do)
To fix your maven project, you need to edit the pom.xml by hand to add the new libraries. (section dependencies)
Once it's done run mvn clean build from command line or from the maven project window (on the right edge of IntelliJ window).

Maven dependencies in p2 project

Let's consider the following projects:
Project1: uses Eclipse P2 Target Definition for dependencies
Project2: uses Maven repositories for dependencies
The problem: project1 requires a dependency which is present in project2 which comes from a Maven repository (and is not available in p2).
My workaround up to now is that I am exporting project2 as a jar with dependencies. Then, I add this jar to project1 and can access the dependency from there.
How could I do this in a better way?
What do you mean with a better way?, What do you think you are doing wrong?
The only way to use libraries or classes from external projects is by importing the package that contains them. In this case, i guess you made this package using (on your second project) mvn install, mvn package or similar, which is perfectly fine...
If you import to your first project the jar that you just created it is ok too!, there is no better way to do this unless you combine both projects and make them a single one.
You could use Package Drone, an open source tool I am currently working on. If your maven dependencies are OSGi bundles, you can drop them into Package Drone and let it create a P2 repository from it. So you can re-use these Maven dependencies using P2.

Idea IntelliJ. how creating maven project. what about pom.xml and .iml files

i just started with java, and created a project.
its maven project (i hadn't use maven yet)
so my project have two modules A and B.
and B depends on A, and A depends on some from remote maven repo. and B also depends on some remote repo.
its works fine in Idea IntelliJ and build jars fine.
but afaik .iml files are Idea IntelliJ specific. and pom.xml is maven specific.
and when i inspect files why all depedecy of project is written in .iml files and .idea dir instead of pom.xml(s)
if you want to see the real world source then here it is but its alpha project for learning java deeply.
and when i try to build project on travis-ci.org it unable to resolve dependencies of project
Meghraj,
I have forked your WebTrimmer repo here : https://github.com/ajorpheus/WebTrimmer and fixed a couple of issues which were preventing a successful build:
The travisci fails because you have three jars in the lib folder which are not available to the CI since it's doing a maven build. The fix was to remove those three jars and introduce corresponding maven dependencies as in this commit.
While adding the maven dependencies an exclusion was needed as noted here : The following artifacts could not be resolved: javax.jms:jms:jar:1.1
The WebTrimmerUI depends on the classes in it's sibling module WebTrimmerEngine, therefore a corresponding dependency is needed.
I have converted the project into a pure maven project which is IDE-agnostic. With the above changes, I can build the project from command line and expect that the travisci should be able to as well.
Regarding the question about why the dependencies are duplicated in .iml --- That's not the reason the CI job fails. The dependencies in that file are a snapshot of the dependencies in the pom.xml. This snapshot is updated when the maven project is re-imported manually by the user, or automatically if the maven project is set to 'Auto-Import'.
As Peter Lawrey mentioned in his comment above, if you add a jar to the project, maven does not know about it and it will be present only in the .iml file.
In general, to search and add a maven dependency, the following has always worked for me: https://stackoverflow.com/a/10178586/325742
Hope this helps !
You need to add dependencies to the pom yourself. The .iml files are for storing project specific settings for whatever project you are currently working on.
Having the pom files allows your maven builds to be IDE independent where as the .iml files require you to have IntelliJ.
You can exclude the .iml files from and version control you are using. You can also open an existing maven project directly via IntelliJ by opening its pom.xml and IntelliJ can auto import everything specified in the pom file and will generate new .iml files.

Adding eclipse project dependencies to a Maven project

I use Maven and I have two different scenarios , and in both of them I want to have the jar built from Maven pom.xml to include all the dependencies.
I have an eclipse buildpath dependency on Maven project B from Maven Project A , when I build Maven project A I want to include project B jar included in it , how can I do that ?
Maven project B also has a lib folder that I added to have a custom jar that is not available in any maven repository , I want this jar to also be included when project B is built by itself using its own maven pom.xml .
How can I achieve these two scenarios. Any guidance would be greatly appreciated.
Thanks.
I'm not sure about your question 1, but if I understand it correctly, you would like to build with Eclipse two Maven projects where A depends on B? Then you may use an Eclipse plugin like m2eclipse.
For your second question, I think the solution would be to use the system scope for your dependency that you can't find in any public repository. Of course, if you can deploy the dependency on an entreprise repository it would be better.

Trouble importing multi module maven project in to Eclipse (STS 2.5.2)

I've just spent the last hour looking at threads on Stackoverflow.com , trying to import a Maven project in to Spring ToolSuite 2.5.2.
The Maven project has multiple modules, when I use the Import... wizard in STS to import the project All the Modules are listed in the Package Explorer and some of the modules dependencies are not resolved.
Can any advise on the proper way to import a Multi-Module Maven project in to STS/Eclipse such that all the modules appear under a Parent directory?
And so all the modules dependencies are resolved?
The project is in a hierarchy on the file system,
I've tried the various Update Dependencies, Project Configuration options but all to no avail.
I have an embedded version of Maven installed in the IDE.
Thanks for your time.
M2Eclipse does a very good job at resolving inter-project dependencies. If it doesn't work, perhaps your project structure has errors. Check that:
every child project references the correct version of the parent project
every dependency has the version that corresponds to the current project version in the workspace
every child project is registered as a <module> in the parent pom (profile must be active if the module is defined in a profile).
If these versions don't match, the artifacts referenced will be pulled from the local repository, not the eclipse workspace.
About common root projects: I don't think it's a good idea to have a common Project, but the Eclipse concept that comes to mind (and that I use for multi-module projects) is a Working Set.
In the Package Explorer, switch the Top Level Elements to Working Sets:
Now choose Configure Working Sets ... from the same menu.
Here's a working set with the maven3 trunk and all submodules as separate projects:
Ugly trick - but sometimes can help:
mvn clean eclipse:clean eclipse:eclipse
Import projects as usual Eclipse project
enable Maven's Dependency Management
Perform Update Project Configuration for all modules

Categories