Maven dependencies in p2 project - java

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.

Related

Export Maven project with subproject to jar-file

I'm writing a lot of plugins for minecraft bukkit server's and I've grown tired of copy+pasting the same utility classes in my projects all over again. I decided to just put them all in a separate project and add them to my plugins via maven. I'm using IntelliJ Ultimate.
I want to have a maven project that contains all my utitily classes called e.g. UtilityAPI. Then I want to be able to create another project, which will be my bukkit plugin. In this second project I want to be able to use all the code from the first one. Also, I'd like it very much, that if I choose to build a plugin jar, maven automatically takes into account the most recent code from my API-Project.
I started working on this and started reading about maven modules and how you can use them to forge different projects together. I initially thought, that this was just what I needed, and tried to just add
<modules>
<module>UtilityAPI</module>
</modules>
However this results in my bukkit plugin project being considered a parent project, and refuses to build in a jar, but just in a (at least for me) rather useless .pom file. I'm not sure how to proceed. Do I have to create a "parent" project for my bukkit plugin project which contains the api and the plugin project as modules? And if yes, how do I generate a .jar, and not a .pom?
The dream solution would be to have the UtilityAPI project, and being able to include it in any new plugins that I might write in the future. I'd also be a fan of having a simple way to create a jar with the newest sources of my plugin in it. Is this possible, and if yes, how?
In your Maven multi-module project your plugin would have to be another module (and not the parent, which has packaging type pom). This module would then have a dependency on the API module (dependencies between modules are possible).
However, multi-module projects are usually intended for projects which are tightly coupled. That does not appear to be the case for your scenario. It sounds like the plugins have (potentially) nothing in common except for the dependency on the API project. Maybe it would be better to have the API project as separate standalone Maven project and then deploy snapshot versions of it (or install them only to your local Maven repository) and use these in your plugin projects.

Add maven project and its dependencies to my project in Eclipse, best workflow

I am new to Maven, and this is a general workflow question. I have Eclipse Mars, and have added subclipse and m2e to it. I checked out a Java maven project (for instance MyLib), and can run its classes inside Eclipse.
Now I want to create my own project (for instance MyProject) that will use MyLib's classes. I right-clicked on MyProject/Properties/Projects and added all the subfolders from MyLib. However, this does not add MyLib's Maven dependencies to with it. I could manually look for all the jars in the .m2 folder and add them, but this sounds like the wrong workflow to me.
I have taken a look at http://maven.apache.org/guides/getting-started/, but the tool and Eclipse with its plugins and possibilities add quite to the learning-curve.
What would be the best workflow to be able to access MyLib (with its dependencies) in MyProject in Eclipse?
Do I have to convert MyProject to a maven project (Configure/Convert to Maven Project) and then (somehow) add a MyLib reference to pom.xml ? If so, how would the pom.xml have to be modified?
If this would be the right way, won't that download MyLib all over again?
EDIT: What I tried is to click on the pom.xml of MyLib, and create a new Module element in it, which then I called MyLib-MyProject. But this still does not add the depenendies of MyLib, and of course this would only be a temporary solution, because the same problem would appear again if I need another library via Maven, for example MyLib2, that also should be referenced from MyProject.
You could just add MyLib to the build path of MyProject.
Right Click Project->Properties->Java Build Path->Projects Tab->Add...
If you have trouble with dependencies, then you could compile MyLib into a jar, and add it as a library to the other project.
Right Click Project->Properties->Java Build Path->Libraries->Add JARs...
(or external jars if it's not in the workspace)
Here is a crunchify tutorial on creating a jar from a maven project.
The best way to achieve that is to use myLib as it was a normal maven project, on the mvn repository.
Of course I don't ask you to upload it on the global public repository, you should take a look at some nice proxy that allows you to have private repositories, like Nexus, Archiva, ...
I recommend you to read about Archiva, it's the easy one.
https://archiva.apache.org/index.cgi

How do I use classes from another project in IntelliJ IDEA?

I have two IntelliJ IDEA Java projects; ProjectA and ProjectB. I want to import and use code from ProjectA in ProjectB. How do I do this?
In Eclipse I would simply go to ProjectB's Build Path settings and add ProjectA.
You can create dependency between these projects (Make project B dependent on project A) What it does is essentially compiles project A first then put its compiled jar as dependency to Project B for compiling or running.
You can do this manually also.
Steps in IDEA ( You won't need these below steps if you follow below mentioned best practices):
Right click on project and select open module settings
Go to dependencies tab
click plus sign and add the module you want to use.
Best practices:
Never use project class in another project, always create a nice interface and use that interface in other projects.
If possible use Dependency Injection to manage different projects and their dependencies (this internally uses interfaces to do this)
Use build tool like ant/maven/ivy etc to manage build process.
Enjoy :)
Eclipse's concepts of "workspace" and "project" are matched by IntelliJ IDEA's "project" and "module".
So one way of doing this is to create a project, say ProjectAB, and import your two existing ProjectA and ProjectB as modules, I'll call them ModuleA and ModuleB.
Right after that make sure that in the project tree both modules have correct folders marked as "source" folders (in my case they are ModuleA/src/main/java and ModuleB/src/main/java).
Then you have to configure ModuleB to depend on ModuleA (ModuleB > Dependencies> Add > Module Dependency).
you have to create a module of your Project A and add in the path of your project B.
Follow this link for learn how to create module and use it in other project : Creating a module library
Hope i help you
EDIT : ok so you have to create a module or a package with your Project A a .jar.
after do this, go to File -> project structure.
On this windows go to Librairies -> click on + and select your module project B.jar.
and Now you have to import your librairie in your class like
import projectb.*;
now instancie the class you want to use and you can use all of the method of this class
hope i help you with that precision
With Gradle: if you only manually add the dependency, you will get javaCompile task errors when building the project.
You need to add the dependency in your build.gradle and load Gradle changes. In your build.gradle:
dependencies {
implementation project(':<name_of_project>')
}

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.

How to add Spring jars to Java Project mirroring folder in git repo

Long title, but pretty much explains it. I've pulled a git repo (spring rest tutorial http://spring.io/guides/tutorials/rest/) which contains multiple standalone java projects each in separate folders. Each of these projects contains gradle build files as well as gradle itself (no need to install gradle), AND the necessary Spring jars (likely in the gradle-wrapper jar). I've created a java project in eclipse that mirrors one of the folders and the changes in eclipse are picked up no problem and it builds fine.
The problem I have is that the Spring dependencies aren't on the Eclipse build path, so I can't use all of the nice Eclipse features (function completion, auto imports, etc.). There's only one jar in each separate folder from the git repo and it's called gradle-wrapper.jar. My guess is that this contains all the Spring dependencies, as the project builds fine, assuming I've typed everything correctly and manually added correct import statements.
Is there a way to set this up so that I'm not getting all these ugly errors in the java project? I attempted to add the gradle jar to the java project build path, but this had no effect. I suppose one option would be to add the spring dependencies separately, but then the Spring on the build path wouldn't necessarily match the Spring dependencies used by gradle for the actual build.
Gradle projects don't package all the dependencies in the repository; one of the primary reasons to use Gradle or Maven is that they'll handle dependencies for you. You need the Gradle Eclipse plugin and to Import->Existing Gradle projects.

Categories