Adding eclipse project dependencies to a Maven project - java

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.

Related

Broken maven dependency while closing the project

I am facing an issue with maven.
I created a project A, build it and did maven install. I see the jar file copied in local repo.
I included the project A's POM dependency in Project B. Project B is now able to reference the project A. The concern here is in eclipse when I expand Maven dependencies in Project explorer it shows me reference to project A instead of jar.
Now when I close project A in workspace of eclipse, the project B could not reference the project A.
Could you please help me understanding and fixing this issue.
Regards,
Amandeep
The M2E plugin in Eclipse is able to see whether a workspace project already satisfies a dependency in another project. You observed it already. If the project is not in the workspace, the dependency will be served by the local and/or the remote repository.
If you close a dependency project, you first have to make a Maven Update. Simply rightclick your project, select "Maven", then "Update". This will recalculate the project settings and its dependencies.

how to configure pom.xml to working with ear project as dependency

I`m new here and i trying to configure maven to working with ear project as dependency of other war project: To imaginate my idea i include the following image
I wonder how to configure poms.xml of this two war projects
(doggle and expert) to get possiblity calling enterprise beans of core.ear project. Simply adding dependency to pom.xml not working correctly. I think I need use some maven plugin or do something addtiotional.
Could somebody help me or give some instructions what I should do to include in pom.xml of each war project ear project and than call EJB bean? I Will grateful for your help.
That's not possible. Convert your war project into jar projects and create two new war projects that depend on the jar project. Now your ear project can depend on the two jar projects.

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.

Can Intellij IDEA automatically replace Maven dependencies with module dependencies?

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.

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.

Categories