Eclipse detects wrong maven dependency - java

I faced with foggy issue.
I am novice in project. I use Eclipse. all my colleagues use IDEA. I have checkout project from svn.
I performed corresponding maven tasks for building and deploying project. all works good.
But my Eclipse shows me problem.
in code:
sceneService.uploadFile(...);
eclipse shows that sceneService hasn't uploadFile method
I began researching. I show this class on PC of my colleague. But there aren't this issue. I noticed that we use different version of jar file of sceneService class.
We use same revision of the pom.xml.
dependency for jar in my pom.xml(for my module):
<dependency>
<groupId>com.day.cq.dam</groupId>
<artifactId>cq-dam-scene7</artifactId>
<scope>provided</scope>
</dependency>
when I type alt+shift+w I see that jar contains sceneService class takes from another module.

I think there is some issue in downloading the correct jar by Maven. your local repository may contain an earlier version of the jar.
Try these commands by going to the root folder of your project from command prompt.
mvn eclipse:clean
mvn eclipse:eclipse
mvn install
if still the problem persists try deleting your local .m2 repository and again rebuilding the project

I think my earlier comments about the "provided" scope are a red herring. The actual problem is likely due to conflicting versions.
By default, Eclipse enables workspace resolution of artifacts. This means it will find artifacts to use (i.e. cq-dam-scene7) from other projects in your workspace. It will also find them in the .m2 repository as well; I'm not sure which takes precedence.
Possible routes towards a solution include:
Specify a version for your artifact. This will ensure you use the correct JAR, even if it has to be found in the local .m2 repository.
<dependency>
<groupId>com.day.cq.dam</groupId>
<artifactId>cq-dam-scene7</artifactId>
<version>1.2.3</version>
<scope>provided</scope>
</dependency>
Ensure your local cq-dam-scene7 project contains the correct code - i.e. a version with the uploadFile() method defined.

I am facing a similar issue, I have two Maven projects in workspace. Main Project is using output JAR of a Helper project as an artifact.
Now the problem is that Main project is trying to Reference JUnit library of Helper project (which has older version 4.10) instead of it's own JUnit library having version 4.12. Because of this in-correct referencing I get build errors in main project.
Only work around which I found is to close the Helper project and have only Main project open in workspace.
Possibly this is an Eclipse bug.

Delete the repository folder in .m2 (in your user dir) and let maven rebuild it in next build cycle.
It will ensure no old jars are cached locally

Related

Local Maven Repository issue with multiple jars

I have local Maven repository in C:\Users\<User_Name>\.m2 directory. After getting "java.lang.NoSuchMethodError..." exception and navigating problem on the web, I see this page mentioning to remove unused jar version(s) from local repository.
My questions are:
1. When I look at C:\Users\<User_Name>\.m2\repository\org\mockito\mockito-core folder, there are 52 different version folder. I think it is similar for other jar libraries. So, should we clean unused jars periodically? Or should we keep unused versions of a jar library?
2. If I just have 3.0.0 version of mockito-core in my pom.xml, how the app use or mix another version(s) in the local repository? Normally, if I just a single mockito-core dependency in my pom.xml, may there be any problem as mentioned on that page (solving the problem after removing other jar version)?
1)
You do not need to "clean unused jars" from your local maven repo manually. How do you want to decide which jar, which version is unused? Maybe your next project will use the jar that you want to delete. Who knows.
If you have enough disk space then you can leave your local maven repo directory untouched for years. If this directory grows too big, then I suggest you delete the complete .m2 folder. Then the next time when you build a project, Maven will download automatically all dependencies that your project needs.
There is only one use-case when deleting your local maven repo can cause a headache: if you have installed some custom jars manually.
2)
It is highly possible that the different dependency versions that you see in your local Maven repo directory come from different projects that you built earlier.
Anyway, you can display your effective pon with the Apache Maven Help Plugin.

Maven is successfully built with the current version of a dependency, but still references to an older version

I have the current version of the dependency successfully installed in the local mvn .m2 registry with mvn clean install (confirmed by cd into .m2 repository).
I then modified the pom.xml of the project to include the new version number:
<dependency>
<groupId>com.xyz.lib</groupId>
<artifactId>xyz-lib-abc-model</artifactId>
<version>1.4.0-SNAPSHOT</version>
</dependency>
Maven was able to build successfully with 1.4.0-SNAPSHOT, but when I accessed the project in IntelliJ, it linked to an old version (0.1.1-SNAPSHOT) instead of the current version.
When I checked the dependency tree using `mvn dependency:tree, I have:
[INFO] +- com.xyz.lib:xyz-lib-abc-model:jar:1.4.0-SNAPSHOT:compile
Which looks correct to me.
I have tried using <dependencyManagement> but then it makes the project failed to build, and failed to detect the model.
Anyone knows what's going on and how to get maven to pull the correct version?
Thank you in advance!
Try re-importing the dependencies from local m2 in IntelliJ like :
Open the project view in IntelliJ
Right click the pom.xml file and
select Maven - Reimport If this works for you IntelliJ will add the
dependencies to the project
Check the if the dependencies you need are added in File - Project
Structure - Project Settings - Libraries and File - Project
Structure - Modules - Dependencies
I have seen this issue with IntelliJ.
If you don't need the old version anymore, you can cd to .m2 directory and delete the older version manually.
But you need to keep the both versions, you can open the project as a fresh project using the pom.xml. This will solve the issue.
Try the following approach:
Close the project in IntelliJ
File --> Open ...
Find pom.xml of your project, not the idea project files
Open this pom.xml. It will re-build all the dependency model in accordance with the information found in this new pom.xml (that I assume doesn't have an old dependency anymore).
DependencyManagement section is irrelevant in this case - mention it because you've said in the question that you've tried this - so don't bother, it will save you some time :)
my first guess goes to the used maven type in your IDEA is not the one that you use in the terminal. :)
You can set up the installed maven instead of the bundled one.
Or another solution is that you proceed a “refresh/reimport” in pom.xml. It will investigate the classpath based on your pom.xml and refresh the ones that needed.
You should see the "Reimport button here
If you don't see Maven at all, that means. you haven't added your project to IDEA as a Maven project.
Please then do the following
1. Right Click on pom.xml
2. 2. Add as a maven project

Compilation error occurs if jars are added manually in Intellij

I am using Maven in my project, and for some reasons, some additional jars should be added manually (I have followed the step like Correct way to add external jars (lib/*.jar) to an IntelliJ IDEA project).
The package can be imported successfully. However, the compilation error happens, which indicates the package does not exist and cannot find the symbol of the used object.
I have tried the following tips but it remain unchanged:
Invalid caches / restarts
reimport
delete .idea file and .iml file
The scenario is quit similar to this one : https://intellij-support.jetbrains.com/hc/en-us/community/posts/206821195--beginner-question-including-external-jar-compile-error.
Please see the following sample images. It may run successfully but cannot be compiled well.
The reason is that when you add a library manually via IntelliJ, only IntelliJ knows about them and when you compile your code using Maven, it can't be find by Maven because Maven only searches for dependencies you defined in pom.xml.
You should install your libraries in your (at least) local maven repository and add them as a normal dependency in your pom.xml. Then you don't need to add them manually in IntelliJ.
You should follow the steps mentioned at Guide to installing 3rd party JARs
Update:
Also you should note that if you're working as a team, you should install this on the local maven repository of all developers (which is not practical). The best solution is to install a Maven repository (e.g. Nexus, Artifactory or Archiva) in a server on your local network and upload your private jar files on those servers. Then all developers can define the address of the local Maven repository server in their local Maven settings and use artifacts/libraries from that servers. Plus it works as a local cache/proxy to fetch any Maven artifacts and prevents unnecessary calls to public maven repositories.

Created Simpl JAR file, imported to Maven project but Maven install is complainining "package does not exist"

I guess I haven't really had to do this much before because I am running into a strange issue. I am trying to generate a JAR from an existing Java project and then and putting it into a Spring Maven project. I'm sure I'm including it correctly, I have done this many times before with 3rd party JARs that I get (even though its a Maven project I have included some obscure JARs in it and put on buildpath, etc), with my JAR within Eclipse it is showing up fine as if its included, I have a test class that is importing a class from the JAR, instantiating it, etc and its not showing any errors (imports are fine in the IDE, etc), however when I go to do a Maven install I get:
[ERROR] /media/src/main/java/org/jadefalcon/automation/DataSetup/test.java:[11,15] package org.test does not exist
[ERROR] /media/src/main/java/org/jadefalcon/automation/DataSetup/test.java:[21,2] cannot find symbol
I have tried doing a Maven clean but still the same problem, the JAR class I am testing with is this: (was trying a more complex one but then tried this to troubleshoot the issue)
package org.test;
public class something {
public String main () {
return "it is definitely working fine";
}
}
Here is the JAR I generated (with sources visible):
https://docs.google.com/leaf?id=0BzB_xvrbRpbYODQyMjEzOWEtOTdjNS00YjM3LTlkZGUtNjY5NmIwN2RiNTRj&hl=en
I would appreciate any advice as I am rather perplexed and frustrated by this. Thanks
You can include a 'regular' jar in your maven project -just as you described- though it's not a best practice mainly because then you not even lose the functionality of Maven for that jar, but also the whole point of Maven dependency management: you will have to include that jar with your source to make it build.
You can of course also create a Maven artifact for that jar, build it with Maven, install it with Maven and use it as a normal Maven dependency.
You also can create a parent .pom and have your dependency project as a module in it and also your real application (or also your real app can be your parent). See further here.
Since this caused me quite a bit of grief, I figure I should post the solution I found. Apparently you aren't supposed to just include a regular lib JAR in a maven project (although I swear I have done it before and it worked), I found this way to include a local JAR that isn't form a repository from this post:
Can I add jars to maven 2 build classpath without installing them?
I did this and its doing the maven install properly (where version and artifactID are just made up value)
<dependency>
<groupId>org.test</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/testjar.jar</systemPath>
</dependency>

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