Resolving Maven Dependency in Eclipse - java

I'm trying to resolve a maven dependency in my eclipse project.
The dependency has already been downloaded into my maven dependencies folder (as can be seen below)
However, the import statement still isn't resolving.
In my java build path, the Maven Dependencies folder is present.
So far, I've tried rebuilding the project on the command line and refreshing it in eclipse; I've tried rebuilding it in in eclipse itself; and I've tried updating the project in eclipse.
Nothing seems to work. Any help would be appreciated.
EDIT: Here's my POM file for those of you who asked.

Your dependency is set as under the test scope. This means that it's only available in test classes (e.g. src/test/java). If you want to use it in your main app src/main/java remove the test scope.

I don't use eclipse but the general idea is to delete all dependencies in your dependency folder and rebuild or you can also change the version of the dependency that is throwing the error. Sometimes, the name of the class you are calling may be changed in the version you are using.

With a right-mouse click on the project node in the Package Explorer and selecting menu Maven -> Update Project..., you can update the Maven dependencies.
You also have to keep in mind, that Maven has often dependencies in a "test" scope in the pom.xml. In that case you can use these dependencies only under the src/test/java path.

Related

Intellij IDEA 2017.2 cannot resolve symbol

In Intellij IDEA 2017.2, I am trying to import an existing multi-module Maven project which builds fine outside the IDE via Maven. When the import is complete, I noticed that in one of the Maven submodules, a symbol is unresolved, lets call this submodule submodule-problem. The unresolved symbol is a class that is defined in another submodule, lets call that submodule-other. The thing is that in submodule-problem, other classes defined in other submodules at the level of submodule-other are found ok(those submodules are defined as dependencies in the pom file of submodule-problem). Furthermore, submodule-other isn't declared as a dependency in submodule-problem's pom file. But I should NOT have to do this because as I mentioned mvn clean install -Dmaven.test.skip=true works fine. So does mvn test-compile. I am using the latest Maven 3.5.0 and have instructed IDEA to use the same for the said imported project in the project's Maven settings. Note: I don't have permission to show the code or concerned POM files.
Things I've tried to resolve the issue so far are:
Invalidate caches and restart IDEA, many times
Re-import as Maven project, many times
Delete the .idea folder and all *.iml files from the said project and re-import as Maven project, a few times
Delete IDEA's system preferences folder and retry all previous steps again many times (this step was painful as I had to install all my plugins and configure IDE setting over again)
Imported the same project into Eclipse-Oxygen and saw that it doesn't have any issues with the importing of the project.
I have solved the problem. It turns out that it wasn't any problem with the IntelliJ IDEA IDE (at least I think not). In production, my top level project is a child project of another parent Maven project and so it's version in it's pom file references the parent's version using ${project.version}. Whereas in development it IS the parent with the lower level projects being it's own children. So in all pom.xml files, where ${project.version} was being used by my top level project to refer to the production parent and also in the children poms where they were referring to my top level project as their parent, I changed it to something like a literal like 1.0.0-SNAPSHOT and all problems went away, I didn't even have to re-import the project into the IDE.
Now, as I mentioned in the original post, even without having to do this, Maven had no problem building the project. I'd love to know why if anyone knows. Perhaps the IDE needs me to do this because it is trying to enforce some best practice? I was led to the solution by watching idea.log where it complained about some POMs being invalid and indeed in those pom files the version tag had a red squiggly underline.

Use classpath of multiple modules in IntelliJ IDEA

I have a Maven project, imported from Eclipse, where the dependencies are set to scope provided. When the project is deployed, the jars are deployed as well so that works fine.
While developing, however, I use a "debugging project" that calls the Maven project, and when it runs I get a bunch of Class Not Found errors when the Maven dependencies are set to provided.
If I change the scope of the Maven dependencies to Compile then the project works fine.
If I change the scope of the dependencies to compile, would that change the output of the project? i.e. add a bunch of jars? That would be undesirable.
I also tried to change the Debug Configuration settings and specified the Maven project in "Use classpath of module", but then the files of the debugging project are not found.
How can I specify the classpath to be of both the Maven project and the debugging project, so that classes from both projects including the dependencies will be on the classpath?
Thanks!
There are 3 types of dependency scope: compile, test, and provided,
compile: the dependency library will be used in all steps: compile , test and run,
test: the dependency library will only be used in the test
provided: the dependency library will only be used in compile and test, but in the run time, the dependency library must be provided by the container otherwise it will throw class no find issues.
Your issues is that you did not provide the dependency library in the run environment ( container) when running your project.
hope this can help you
How did you import the project to Idea? If the project is opened as a Maven projects, it should work out of box.
Can you try to open the project by selecting pom.xml?

Make maven generate dependencies even with errors

This feels like a really stupid question but I haven't been able to find an answer.
I'm working on a maven project but I do most of my development in eclipse. Is there any way for me to force maven to generate all of my dependencies under target even if there are errors in the code? I set my eclipse project's build path to use the jars under target/dependencies/jars, but calling mvn clean kills them and if there are any errors in my code causing it to not compile mvn package won't create the dependencies but will instead just crash saying BUILD FAILURE. This makes the problem even worse since instead of seeing the actual errors my eclipse will just bombard me with errors everywhere since all of its dependencies just died.
Or maybe the way I'm working with it is just stupid and there's a better way.
Are you using the m2e plugins for Eclipse to process maven projects, or simply importing the projects as general ones?
If the latter, you should use the m2 plugins (simply go to the Eclipse Marketplace and search for Maven), as they interrogate your POM and set up your dependences properly. You can then concentrate on any compile errors in your code.
You should not point to the jars in the target folder for dependent JAR's since this is where the products of building your project are stored. Performing a mvn clean removes this folder.
To use Maven with Eclipse install the m2e plugin in Eclipse. This makes Eclipse understand the structure of Maven projects.
Once installed you can import your Maven project into Eclipse. I use Import... | Existing Maven Projects for this. But you can also directly import form a versioning system.
During the import Eclipse will set up the Eclipse project to use the Maven dependencies to locate the required JAR's. These are taken from the repository as configured with the used Maven installation.

how to change the code of maven dependency using eclipse

i have a project which is using several components to perform the functionality.All the components are managed by maven dependency control.now i want to change the code of one of the component just to enhance the functionality.That component is visible to me in eclipse project explorer i can access the files present in that component.But when i change the code in component,that change is not accessible to main project which is using that component as dependency.i have tried maven:clean , update dependencies but nothing works.i am just new to maven so i am sure i am missing something.
any help is really appreciated.Thanks
When using maven with eclipse, dependencies can be used either directly as jar (as they were downloaded from the repositories) or, if the dependency is also defined in a project open in the same workspace, eclipse should use this project instead of the original jar.
You should check :
that "disable workspace resolution" is not checked (when your right-click on your project in the project explorer, in the "maven" sub-menu).
that in your project properties, in the "Java Build Path"( "Librairies" tab ) eclipse displays your dependency as a project and not as a jar from the local maven repository.
Also be careful to rename the component (and to publish it somewhere or to include the modified code in your project) to make sure that another developer building the project use the same modified dependecy and not the original one.
After you updated pom file with dependency, have you tried maven install ?

JAR in project-specific local Maven repo, how to see library in Eclipse project?

I've been trying to add a custom .jar (ftp://ftp.ncbi.nlm.nih.gov/pub/eutils/soap/v2.0/java/axis2_1.5.2_jdk_6.0.12/eutils_axis2.jar) to a project that doesn't have a central corporate maven repository and that instead will have the custom JARs checked into SCM in the project directory tree. I was following this post to make it happen: Maven: add a dependency to a jar by relative path (awesome post btw).
What I did was:
Add local repository to pom.xml
install the file into the local repository
Add dependency to pom.xml
Based on what I see in m2eclipse, the library has been successfully recognized by Maven and added to the dependency list (or it'd be called ? : ? or something similar)
The problem is that Eclipse still doesn't see the referenced lib, so this still fails:
import gov.nih.nlm.ncbi.www.soap.eutils.*;
Pardon my maven newbiness, but what are changes / next steps I need to make to get to:
Have Eclipse see the library so that autocomplete works (and the import can be resolved)
Be able to compile the project
Be able to execute the jar produced by mvn package?
Thanks!
If you see the JAR under "Maven Dependencies" in your project, Eclipse should be able to see and use it. If it's not there, then m2eclipse wasn't able to resolve the dependency properly.
If it is missing, m2eclipse was unable to download the dependency from your local repository for some reason. Check the Maven 2 Console for errors and the Problem View.
Lastly, the JAR itself might be corrupt. Maven doesn't check for that, it simply adds the file to the classpath. If Eclipse can't open the JAR, you can also get the errors you mentioned.

Categories

Resources