Debug different projects using sources instead of m2 in intellij - java

I have 2 projects (A & B) consisting of multiple modules which interact among each other. Let's say I debug a module of project A which at a later stage jumps into a module of project B. Intellij in this case uses a decompiled source code from the jar inside .m2 repo instead of the source files.
A possible workaround is to specifiy the sources when intellij shows the decompiled version but that is really tedious for multiple classes and modules.
How do I tell Intellij to use the sources?
Additional infos: Project A and B are in two separate folders and I have loaded Project A using its pom.xml. I manually use cmd to call 'mvn clean install'. Essentially, I need to specify an additional classpath to search for sources. At the moment the debug config is set to search the whole project but as project B is in another folder this is not possible.
I am not allowed to do changes to the pom.xml files inside each project and modules. I would also prefer to have both projects in separate folders.

Related

Let's say a.java is in folder x, and b.java is in the parent folder, how would i code b.java to be able to access a.java in b.javas code?

I can't find any info on how to call a different java file from a different folder anywhere.
Heres a diagram of what I wanna do
So for example
If the java file was in the same folder you would do:
HUD.HEALTH
but what would you do if the java file was in a different folder.
In Java you normally work in one folder, the so-called source folder. In this folder the classes can be divided into packages, usually using a certain pattern, for example com.company.something. In this case, a class is applied via import. The keyword import and the package name in which the class is located are specified. If they are external resources, the classes are included as external resources (.jar files), nowadays build tools like gradle and maven are used for this. Once the dependencies are imported, they can be used as well.
If you have two local projects, and want to access a class from project B in project A, for example, you work with the IDE. In Eclipse I would simply go to ProjectB's Build Path settings and add ProjectA. I recommend Intellij as IDE, there you solve it as follows:
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 tools like Gradle and Maven to manage build process.

Is there a way to run two depended modules in intellij project?

I migrated my workspace from Eclipse to Intellij,
in Intellij I have a project build from two modules, the run configuration are set on one module but the main class is in the second one.
in Eclipse it works fine, but in Intellij I get "Error: Could not find or load main class [class name]".
I created a dependency in the project structure, and played a lot with the running configurations which didn't help.
Any ideas? does any one have an experience with this structure in intellij?
Adding more details:
I imported two eclipse projects to intellij - UI project which uses a jar from core project.
The two projects are depended, my goal is to run the UI project which use a jar from the core project, and be bale to debug in the IDE the code
from both projects.
In Eclipse it is done by adding the core project to the classpath in the debug configuration classpath before the UI (running) project.
In intellij I added runtime dependency between the projects which didn't work.
The project is running but from the Jar and not from the code of the second project - so I can't debug the code.
Add Run Configurations -
The main class is in the core project,
I tried to delete the created core jar from the classpath of the UI project and run it with module dependency, but it failed on main class not found error.
Ideas?
Thanks
OK got it - the Core jar should be in the dependencies in compile scope, as well as the module in provided scope, the main class can be from the core project and it works.
Thanks to all respondents
"The project is running but from the Jar and not from the code of the second project - so I can't debug the code."
I was also looking to do this. Your answer is not that clear. Here's what I did and it worked in my case,
Project Structure (Alt+Shift+S)
> Modules
> on the Sources tab for the module that has dependencies, look on the right side,
click the + Add Content Root, then add the src folder of the dependency project.
I was able to then put breakpoints in those src files and IntelliJ would step to them in debugging.
(Note you may see the warning "Alternative source available for the class ...")
Reference
From https://www.jetbrains.com/help/idea/creating-and-managing-modules.html,
"Modules normally have one content root. You can add more content roots. For example, this might be useful if pieces of your code are stored in different locations on your computer."
(see also How to debug a project in Intellij while setting the breakpoints in another project)

How to separate subproject classpaths in Eclipse?

I know that there is a Git plugin for Eclipse ("Egit"), but I like to do Git stuff on the command line, and I like to code in Eclipse, so I want to keep them separate.
I cloned a Git repo (I don't think its important, but for good measure, it was https://github.com/spinscale/dropwizard-jobs.git). I then opened up Eclipse (Juno) and created a new Java project, and selected the root of the cloned repo as the base path to my project. Eclipse asked me if I wanted to associate the project with the Java facet, and I clicked 'OK'.
This project has a large dependency tree (if you like, check out its 4 POM files). I don't use Maven to build (I use Gradle) so I just ran a script that resolves the dependencies of these POMs into a directory, and then I created a lib directory in this Eclipse project and copied all the JARs into it. I then added all these JARs to the project's classpath.
I am now seeing 10 errors in the Problems view in Eclipse, and they're all similar errors:
The type ApplicationStartTestJob is already defined ApplicationStartTestJob.java /dropwizard-jobs/dropwizard-jobs-core/src/test/java/de/spinscale/dropwizard/jobs line 10 Java Problem
The type ApplicationStartTestJob is already defined ApplicationStartTestJob.java /dropwizard-jobs/dropwizard-jobs-guice/src/test/java/de/spinscale/dropwizard/jobs line 10 Java Problem
8 more all like this, etc.
Sure enough, when I expand the entire project, I see it has the following structure:
dropwizard-jobs/
dropwizard-jobs-core/
src/test/java/
de.spinscale.dropwizard.jobs
ApplicationStartTestJob.java
dropwizard-jobs-guice
src/test/java/
de.spinscale.dropwizard.jobs
ApplicationStartTestJob.java
dropwizard-jobs-spring
src/test/java/
de.spinscale.dropwizard.jobs
ApplicationStartTestJob.java
So it seems that the maintainers of this project like to rename their unit tests with the exact same package/class names, and for some reason, Eclipse sees them as all belonging inside the same package. To test this I renamed dropwizard-jobs-core/src/main/java/de.spinscale.dropwizard.jobs.ApplicationStartTest to something else, and did the same for dropwizard-jobs-guice/src/main/java/de.spinscale.dropwizard.jobs.ApplicationStartTest and sure enough, all the errors associated with ApplicationStartTest being already defined went away.
So my suspicion is confirmed: The intention of these subfolders (dropwizard-jobs-core, dropwizard-jobs-guice and dropwizard-jobs-spring) is that they are sub-projects with separate classpaths. Eclipse thinks all of these source folders are part of the same project, and so it is lumping all of their classes into the same classpath. Since each subproject uses the same unit test naming conventions (same package/class names for each subproject), Eclipse see multiple classes in the same package as having the same name.
OK, good! I figured out the problem. But what's the solution? Ideally I would be able to keep all of these inside the same project, but perhaps modify the .classpath file or do something similar that instruct Eclipse to keep the subprojects separated from a classpath perspective. Any ideas?
SImply download eclipse m2e plugin, then import the project(considering you have already checked-out at your workstation), and do spend sometime learning MAVEN commands. here you can find an pverview of maven parent project and modules. Maven parent pom vs modules pom
One possible solution would be to introduce maven, which allows to naturally define a parent project and sub-projects in a multi-module maven project.
You can actually test that configuration outside of Eclipse, and then use M2Eclipse in order to import parent and its dependencies, at the same time (as commented in this answer) in your Eclipse.
Actually, the M2Eclipse project itself has guice test project, which you can use as model for your own guive subproject, in the repo sonatype/m2eclipse-guice, with an adequate pom.xml.

Refactor classes into a separate project

I intend to extract several classes and packages from one Java project and place them into another (which will have a distributable jar). That much isn't too difficult, but of course with such a large refactoring there are consequences. Namely there are many classes in the original project that are subclasses of the classes I want to extract. What's the best method for approaching this sort of refactoring?
You can create separate projects and the main project will have dependencies for all these projects. So in your IDE you can navigate through source code easily.
When building your application, each dependency could be built into a jar and the main application will be bundled with all the dependents jars in its classpath.
Let take as example a web app using plugins, and plugins using common classes, utilities and so on stored in a project named common-plugins.
project/webapp: having dependency on plugin1, plugin2 and common-plugin
project/plugin1: having dependency on common-plugins
project/plugin2: having dependency on common-plugins
project/common-plugins: having no dependencies
When building your project, you could build the plugins and the common-plugins into jars, bundled with your web app
project/webapp.war/WEB-INF/lib/plugin1.jar
project/webapp.war/WEB-INF/lib/plugin2.jar
project/webapp.war/WEB-INF/lib/common-plugins.jar
This way in your IDE, I will take eclipse for instance, you will have a workspace having 4 projects with dependencies as described above. At build using maven, ant, ivy, or what you want, you will build the 3 projects that the webapp project depends on, then bundle the whole stuff.
So basically this is what I did:
Create a new project
Copy over the appropriate classes from the old project to a new package in the new project, reconfigure until everything builds
Test that project separately and build it in to a jar
add jar as a dependency
Delete the classes from the original project
Manually change all the imports from the old packages to the new packages
What I was really looking for was some way to automate or streamline step 6 to make sure I didn't break anything, but I'm not sure it exists beyond mass find/replace.

importing project in Eclipse

Hello I am new to Eclipse (and I am a novice in Java): I am creating a project which should make use of some classes from another project. Do I have to export necessarily this last project as JAR file and add in my project? Are there other alternatives?
There are several alternatives:
If project A depends on project B, and both projects are in the same Eclipse workspace, you can just project B to project A's build path (project properties / build path / project). This has the nice advantage that it will (optionally) automatically pull in project B's JARs, plus updates to B will be used automatically by A, and you can debug into B's code (even using hot code replace).
If A and B are fairly separate, just make B into a JAR (or several ones), then add these to A's build path.
If you have the source of those class files you can just link that source
Or add the other project as a dependent project.
Or create a jar of those classes and add that in lib.

Categories