Compilation error occurs if jars are added manually in Intellij - java

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.

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.

Two Maven project with public module

I have a project that devided into three pieces, PCommon, PWebapp and PAdminConsole. PWebapp and PAdminConsole are dynamic web project in eclipse, PCommon is java project, and both two web project will use the api in PCommon as a jar file in lib folder.
In the past,I added import project in build path, I used Ant to compile and build PWebapp and PAdminConsole and in both build.xml file there is
<ant antfile="${common}/../build.xml" inheritAll="false"></ant>
to make PCommon into a jar file.
Now I will change all my projects to Maven Project. But I don't know how to make two web projects contain one public module, and how to package my PCommon into a jar file automaticlly when I run package maven command to package one web project.
Now I always deploy my PCommon.jar on nexus server. and then add dependcy in pom.xml in web projects. But I think there is no sense to deploy the jar on the public server, so I think it isn't the right way to archieve this goal. Is there any way that is more convenient?
I know I can make a parent project with a parent pom.xml. But I have two web project need the module, the pom.xml in PCommon can only extends one parent, can't it?
The common way to resolve dependencies in Maven is using a repository. The first time a dependency is needed, it is downloaded from your repository and installed in the repository on your local machine. If a dependency is not available in the remote repository it has to be installed to your local one in some other way. There are a few other ways to resolve depenencies without using the repositories but I wouldn't suggest to use the.
To make this a little more convenient, you can use a proper IDE. I use Eclipse with the m2e plugin. It supports something called "workspace resolution", which should be enabled by default. It scans your workspace for other Maven projects before falling back to the repository lookup. This has the advantage that every change you make in your common project is immediatly available in the other projects. I think it also gets installed to your local repository in the background but I'm not sure. Anyway you don't have to worry about it yourself.
Something similar works with IntelliJ IDEA but I don't have that much experience with it. I'm sure Netbeans has some kind of Maven support too.

Eclipse detects wrong maven dependency

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

Including the Jar of a Maven Project in another Maven Project does not work but including the Project in another Maven Project works

I have 2 Projects namely Project_1 and Project_2.
Both projects are Maven and I am using Netbeans.
I want to include the jar of Project_1 in Project_2 which I am doing like this.
The problem is when I include the jar I do not get any compile time error, however I get a NoClassDefFoundError exception at runtime.
When I include the Project_1 in Project_2 by performing the steps mentioned here. (The Open Project example). I do not get any errors. Neither runtime nor compile time.
Can you please explain me what am I missing here?
Update
Project_2 is deployed on a Server which is not in my local machine however Project_1 is in my local machine.
Inclusion of Project_1 into Project_2 as a project was done for testing in my local machine.
First of all, a good rule of thumb to adopt is never use the system scope and system path to pull in dependencies. In my experience there's always a better way :-)
If Project_2 depends on Project_1, then first install it's jar into the local repository:
cd Project_1
mvn clean install
Watch the output you'll discover the jar is placed somewhere under the following directory:
$HOME/.m2/repository
Once this is done the jar will be available as a normal dependency to the second build
cd Project_2
mvn clean compile
The local repository ensures the projects are now decoupled from each other. Assuming you're using snapshot revisions of Project_1, the Project_2 build will always retrieve the latest revision built and tested.
Update
You should use a Maven repository manager to share jars between machines/teams. Recommendations are contained in the following answer:
Share jar of the module with another team
How to configure Maven to use a repository like Nexus is described in it's documentation.
As described in the deploy plugin documentation you'll need to add a distributionManagement section to your POM (detailing the repository URL) and then upload the project's jar to your repository as follows:
cd Project_1
mvn clean deploy

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