Intellij not able to find changes made in dependency modules - java

I have a Maven project which has dependency on other Maven projects. I have managed to import those dependency modules via 'project structure' into my project. Able to mvn clean install and able to run the program. Also in my main project, if I were to 'command click ' on a file that is located in the dependency modules, it will nicely go to that .java file (instead of heading to the .class file).
But changes made under the dependency project does not reflect when I run the program. Re building or a new mvn install doesn't make any difference.
For example, I have tried just adding a new variable declaration in a class located in the dependency modules. This variable does not even exist when I run the program and check via debug.
Similarly created an entire method in a class inside the dependency module.
That method doesn't exist either. When I mouse over a breakpoint I placed under this method, I get following message:
No executable code found at line 112 in class com.name....
Information which may help.
This project was working fine without having to add these dependencies cos they existed as jar files as part of this project. Since I needed to make changes in those dependencies, I have removed those jar files 'via Project Structure' and instead imported those dependencies as modules 'via Project Structure'.
As mentioned above, this is working. I am able to build and launch the app. Just not able to see changes when I make them.
Another issue which surfaced and again I don't get it. There was a parameter which was not working on my main project (compilation was failing) until I made the change in my dependency. Now it is working fine in terms of compilation. But when I run it, under debug I get the correct param value, but after the next step, it just jumps to InvocationTargetException. Added short snippet on the code change below.
// Under Main Project
someMethod(Doable do){
// do something
}
DoSomething doSomething = null;
someMethod(doSomething); // compilation error unless I implement Doable
// Under dependency
// implement Doable is the change which compiles now
class DoSomething implements Doable{
//implement something
}
Added the project structure for reference.
Please advice. Thank you.
MyMainProject
mvn_structure
pom.xml
MainDependency
SubDependency1
mvn_structure
pom.xml
SubDependency2
mvn_structure
pom.xml
SubDependency3
mvn_structure
pom.xml
pom.xml

Your project relies on the dependency jars in your maven repo, not on the dependency source codes (the other projects). So in order to have your changes in the other project available to your project, you need to build the other projects first so that their jars in the repo are updated. Only then are those changes available to your project.

Related

How to add a Maven project as dependency in another maven project and make live changes to it instead of JAR dependency

I tried to find a lot of answers but could not find any which works as expected. So posting here. It can be a possible duplicate but unable to find any right answers.
Let's consider project A as my maven project and Project B as EclipseLink JAXB/Moxy library code. I am using Intellij IDE for my application.
I have a (My JAXB Project) Project A which is using the 3rd party library Eclipselink Moxy (Project B) and I believe Project B has some issue due to which Project A is not providing the output as expected. I would like to debug Project B to find the issue.
So I have downloaded the source code from Github for the Project B. Now I would like to include this Project B code from this local as my maven dependency in my Project A.
I know I can create a JAR for Project B using the mvn clean install -Dskiptests and include that JAR in my Project A but I would like to make few changes during the debug. As of now, if I make any changes to Project B then every time I need to create the JAR. The library files are very huge and it has many modules and submodules due to which it takes a lot of time (around 5-10 mins). Also, I am unable to edit the class file within JAR so need to edit the class file of Project B again to create JAR, and again add it to the project A. This is too much of a task and waiting time.
Hence, I want to know how can I add the Project B directly from local so that I do not have to create JAR every time and make changes to project B and see the changes updated directly when I run the Project A.
I tried a lot of things since yesterday but nothing seems to work till now so thought of asking the same.
In short, all I want to do is:
Add the dependency from local project so when I click on the source for my code I can open the class file from my local project because opening the class file from JAR does not provide me an option to edit.
Change version in main pom.xml of Project B to one which is not there in you repo.
install Project B using install -U command.
Use the custom version of Project B in Project A. You can also put break point now in workspace of Project A to debug.
You can make a multi module project in Intellij.
First create a maven project for project A in Intellij, then you can add Project B as an additional module in the same project.
This way you would be able to debug and hot replace the code of Project B as well.
Help related to adding a module can be found here.

A class is looking for methods of another class in the maven repository instead of the source code

I have two (really more, but we are interested in these two now) projects in the Eclipse workspace. One of them has got a class A that just obtained several new static methods.
I want another one class B, in another project, to call these methods. But I see these methods underlined by red, with message "The method ... is undefined for the type A". If I go into "open implementation" for the class A, Eclipse tries to open the source code attached to the old jar that lies in the maven repository. No source is attached, so I see only the tab with the name of the A class (it is there), but that is the old class version, without new methods.
Clean, Maven Clean - don't help. The same with restart Eclipse, clean all projects, reopening projects.
Please, don't propose to reinstall Eclipse - what I have is the picture JUST after reinstalling of everything, updating the project, maven installing it (worked), and adding these functions.
A Maven Install does not work for the same reason - falling on these references to new methods.
If I clean the repository by hand - on Maven Clean I am getting a FATAL error, with a demand for that old jar.
I have put the reference to the correct source of the A class to the Maven task source. I have added the reference to the correct project of the A class to the project of the B class in the Eclipse Properties of the project. Nothing changed.
There are similar problems here on SO, but I managed to get the some multiplication of all of them: both Eclipse and Maven had gone mad, no added references help, no cleaning helps.
Edit.
I have tried to make maven to create new snapshots, but the result is absolutely the same picture... "cannot find symbol" that is in the source! What is interesting, after removing of the repository the Eclipse starts to see the symbols OK. But I must run maven and it won't run with removed repository. Just as a crazy idea: Is there some maven tool to clean the repository so that it will know there is nothing there and won't try to look for things in it?
I have met with three unpleasant situations combined:
The local maven repository was damaged
The jar written in it was deprecated.
Due to some committing to local CVS branch and pulling and checkout from the common CVS repository it happened that parent snapshot referenced to by some poms and the main snapshot declared in the root pom had different versions.
Solution
The first problem can not be normally corrected using the plugin maven only. You have to install the standalone maven, add its bin to PATH, an launch in folder of your root pom (exit the Eclipse at first and stop jar that you are repairing, if you have it running):
mvn dependency:purge-local-repository -DactTransitively=false -DreResolve=false
Then, if you want to work in standalone maven, you should put the correct settings into youruserroot/.m2 folder.
After that,
mvn -U -X -e install
That will fail on every local pom with incorrect parent Snapshot. Go into every local lesser poms and correct the references. Repeat, until it stops to fail on snapshots.
I had found advice to run
mvn -U -X -e clean install -DskipTests=true
for installing all, but without test running. (I had repaired an error in the line, so don't try to google for it). But you can already use the plugin Maven at this stage, too.
So, you run Maven clean and Maven Install in the plugin... And hurra! - the classes see other classes as they are in the source code and not in some old jar, and that works in both maven and Eclipse.

GWT maven build eclipse

I had a GWT app, and I wanted to automate its build and deploy system, since I do it manually. But I did not find a way how to build the app from command line, so it can than be automated. I had to click the Google button, then compile GWT project and then click Compile.
I found out that it is possible to create a GWT maven project and that it should then be possible to compile my project from commandline with mvn gwt:compile.
So I created a new project using this plugin. Copied my sources from the old project to this new one.
Now the structure is like this:
/src
---/main
------/java -> here are all my sources including my Project.gwt.xml file.
------/webapp
---/test
pom.xml
Now I have 2 problems.
1. I thought that I add dependencies to the pom.xml, and then when I build the app, it will create the jars and I can use those libraries in my GWT app. I guess 'mvn clean install' should do this, but so far I'm getting compile errors.
2. I did not get mvn clean install to work, so I added all the jars manually again... And then yes! I was able to build the app using the plugin GWT button! So I was thinking that now I can use 'mvn gwt:compile', but it fails with:
Unable to find: "com/company/project/Project.gwt.xml" on your classpath; could be a typo, or maybe you forgot to include a classpath entry for source?
EDIT:
So I fixed my <moduleName> element in pom.xml, so now it finds the Project.gwt.xml. I'm trying to run:
mvn clean install gwt:compile
But I am getting compile errors. I think, it tries to build my project without the actual dependecies because it tells few classes don't exist, but those classes are part of an external library. (specifically this one). But I have it in the dependencies, so I don't know what more to do.
<dependency>
<groupId>com.github.tdesjardins</groupId>
<artifactId>gwt-ol3</artifactId>
<version>3.0.0</version>
</dependency>
Also in eclipse I had to manually add the jars to my project, so that was why it worked there and not in the command line. So I would also like to ask how to tell eclipse to get those jars and include them to the project, because otherwise eclipse is missing those dependencies and displays many errors.
First I had a problem with <moduleName> in my pom.xml was missing com.company.project prefix before the actual module name.
Then I had errors in my Java files, which was caused by RELEASE version of GWT-OpenLayers 3 library missing some of the features that I previously used by building the JAR from the GitHub repository.

GWT maven project compiles and run from maven but don't compile and run in eclipse

I have multi module maven project, that is build with gwt as a main web framework. Here is github repo.
Everythink works as it should as I supose. I haven't got anything wrong.
Unfortunatelly the problems occur when I try to import this project into eclipse (as maven project). I can not compile this project (using RMB on project in Package Explorer -> Google -> GWT Compile).
I am getting an error:
Loading inherited module 'src.main.resources.pl.derp.parent'
Loading inherited module 'pl.derp.shared'
[ERROR] Unable to find 'pl/derp/shared.gwt.xml' on your classpath; could be a typo, or maybe you forgot to include a classpath entry for source?
When I tried to RMB on Project name in Package Explorer -> Run -> Run as Web application (GWT Super Dev Mode)
I've got:
Working directory does not exist: /home/danielo/eclipseGwt/gwt_2.7.0_maven_eclipse/web/src/main/webapp
I've added modules to build path.
I've followed this instruction
I think, that I've done everything to make it work, but still can make it work.
I think the problem is in packages name, and the way they are treated by maven and eclipse. They are treated in diferent ways.
Maven easily find every class (f.e.: package pl.derp.web;) needed and compile. The eclipse is trying to find them by f.e.: src.main.java.pl.derp.web but can't found it.
Really I don't know how to resolve this issue?
I am not sure witch package name patter is better (src.main.java.pl.derp.server or pl.derp.server)- for me the shorter is better.
To run this project in maven (it is well described here):
mvn clean install
mvn tomcat7:run-war-only
and in second shell:
mvn gwt:run -pl web
And in Eclipse I think I am running built in Jetty server
Please give me some help.
You apparently have a single Eclipse project, with server, shared, and web declared as source directories. When using M2Eclipse, you should have 3 projects, each with their src/{main,test}/{java,resources} declared as source directories. At a minimum, without M2Eclipse, the source directories must be the src/{main,test}/{java,resources}, whether you have 1 or 3 projects.
This is why Eclipse looks for src.main.java.pl.derp.server instead of pl.derp.server, and why GWT fails too (classpath is similarly wrong).

Adding referenced Eclipse projects to Maven dependencies

Right now, I have two Eclipse projects - they both use Maven 2 for all their jar-dependency goodness.
Inside Eclipse, I have project Foo included in project Bar's build path, so that I can use Foo's classes from project Bar. This works really well in Eclipse land, but when I try:
mvn compile
inside Bar's directory, it fails because Maven doesn't know about the project-to-project relationship in Eclipse's build path.
If I were using Ant, I would just use it to do something silly like copy foo.jar into project Bar's classpath, but as far as I can tell, things are done a lot less hackishly in Maven-land.
I'm wondering if there's a standard workaround for this type of problem - it seems like it would be fairly common, and I'm just missing something basic about how Maven works.
Maybe you are referencing the other project via Eclipse configure-> build path only. This works as long as you use Eclipse to build your project.
Try running first mvn install in project Bar (in order to put Bar in your Maven repository), and then add the dependency to Foo's pom.xml.
That should work!.
Check out the m2eclipse plugin. It will automatically and dynamically update the project build path when you change the pom. There is no need for running mvn eclipse:eclipse.
The plugin will also detect if any dependency is in the same workspace and add that project to the build path.
Ideally, if you use m2eclipse, you would never change the project build path manually. You would always edit pom.xml instead, which is the proper way to do it.
As has been previously stated, Maven will not know about the Eclipse project build path. You do need to add all dependencies to the pom, and you need to make sure all dependencies are built and installed first by running mvn install.
If you want to build both projects with a single command then you might find project aggregation interesting.
I just needed to do this and I needed it to build with the external mvn clean install command. Here is the proper way to configure this in Eclipse. (With project B as a dependency of A)
Open the pom.xml for project A in Eclipse.
Go to the Dependencies tab.
Click the Add... button in the middle of the page (for the left side Dependencies box)
In the popup, there should be a box under a line with text above it saying Enter groupId, artifactId or sha1 prefix or pattern (*):. Enter the artifact ID for project B into this box.
Double click the jar you want to add as a dependency to this project
You may need to update the project after.
Right click project A in you Package explorer
Maven -> Update Project...
Then hit OK in the popup.
You might want to try an alternative approach, where you have a parent maven project and two children project. let's say:
Parent (pom.xml has references to both children projects/modules)
--> A (depends on B)
--> B
then when you run mvn eclipse:eclipse from the root of Parent, maven will generate eclipse projects for A and B, and it will have B as a required project in the classpath of A.
You can run mvn install from the root of Parent to get both projects to compile.
To complete your setup, you'll have to import both A and B into Eclipse, making sure you don't check "Copy projects into workspace".
I think the best way to handle it is to make Bar a Maven project just like Foo, and then mvn install it so it is available in your local Maven repository. The drawback is that you have to install that project every time you want Maven to see the changes you make to Bar.
Not a complete answer:
Bar's pom needs to include Foo in order to use maven to compile it.
I'm interested in this question too, but from the perspective of how to get eclipse to recognise a maven-added dependency is actually another project in the same workspace. I currently alter the build path after performing mvn eclipse:eclipse
If you reference a local project, but its version has been updated (usually increased), it could maybe only be found in your local repo and you have to update the (likely fixed) version of it in your POM(s).
We have a "common project" (used everywhere) which does not necessarily need to be versioned since we tag it via source control. so either
keeping it at a fixed version or
referencing it with the special LATEST version
are good workarounds to always be on the safe side.

Categories

Resources