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>')
}
Related
I'm trying to replicate in Intellij something easily done in Eclipse.
I want to run this Main class from the mainProject (nevermind the errors due to the use of fictional names):
Now, the problem is I need to be able to import a configuration folder from another project, in order to run the Main class. This is easy in Eclipse:
But I don't know how to do this is Intellij. Ideas?
This is actually why I despise the Eclipse workspace. It lets developers cheat and use another project's source as a dependency of another project. This is a problem because this isn't how it works outside of the IDE.
What you need to do is create a jar of classes you depend on, then include that jar as a dependency of the project that depends on them. If you use dependency management and have a local repository (like nexus or artifactory) you can publish your jar to your local repository and then in your other project just include it in your pom.xml if you are using Maven or build.gradle if you are using Gradle.
If you are instead including libraries in your source folder, copy the jar to your project, then right click on the jar in IntelliJ and select "Add as Library...". You can also add a dependency through File->Project Structure->Modules->Dependencies tab. Add as Library is a shortcut to adding a library here and the dependency shows up here if you use Add as Library.
IntelliJ does let you import a module from another project, but again this is cheating because it will just confuse you down the road because it will only work from within the IDE, not running as a standalone application. (File->Project Structure->Modules->Plus (+) Sign->Import Module)
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
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.
I have 3 projects
Webapp1 and Webapp2 and a 3rd project i called SharedProject
Both webapps1,2 are using the sharedproject classes.
I would like to include the sharedproject as a jar in both of them.
In eclipse i could set for each project that it depends on the 3rd project and on every war file i export it will attach the jar automagically.
Is there a similar easy way with gradle? I couldnt find any tutorials for this specific concept.
Thanks!
Adding the shared project as a dependency in the other projects should do it, e.g.:
dependencies {
compile project(':SharedProject')
}
I have a project with a number of POM files. I have done a clean install and eclipse:eclipse. I would like to import the project as a normal project rather than Maven project. When I imported the project as normal project , I was not able to see the Maven dependencies present in POM in eclipse's build path. Is importing the project as Maven project only way of doing it? When I import the project as Maven project I see a number projects in eclipse workspace (one for each POM). However I would like to view this as a single project in eclipse workspace.
UPDATE: The classpath files of the child projects contain the dependent jar files , however it is not added to build path when the main project is added. Only the entries in classpath file of the main project is being added.
Given that you refer to "Maven project", are you using an eclipse plugin for Maven like M2Eclipse?
Assuming you are using M2Eclipse, the Maven Dependencies Library is automatically enabled for a Maven project. For a normal Java project, importing the library would force you to configure the project as a Maven project (On the context menu for the project, select Configure -> Convert to Maven Project).
Having said that, while you import your Maven project, you will notice an "Advanced" option (towards the lower section of the "Import Maven Projects" dialog). When you expand this option, there will be a checkbox to "Resolve Workspace Projects" - this is checked by default and is hence responsible for importing the child modules as separate projects. If you uncheck this, it will import only the parent module as the project and all children modules will be in sb folders under the parent module.
IMO, resolving the projects provides greater clarity, but then again this is very much a personal choice.
Hope that helps.
Eclipse doesn't support nested projects, and Maven kinda forces you to. So you can't really push everything into one project.
Doing a cursory google search led me to this, which might be helpful: http://warpedjavaguy.wordpress.com/2011/08/08/how-i-defeated-the-maven-release-plugin-in-a-flat-structured-multi-module-project/
You might also check out the following two online books from Sonatype:
Maven: The Complete Reference and Developing with Eclipse and Maven
Personally, I create Working Sets for each multi-module project and add all the individual projects to it. This enables me to see them all grouped together, this might be to your liking.
http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2Fconcepts%2Fcworkset.htm
Once you have created the working set you can enable/disable them in the menu in Project Explorer and configure the project explorer to display the working set as the root element in the tree rather than the project.