Can't import dependencies that have been downloaded to .m2 - java

I'm importing dependencies via Maven:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1203-jdbc4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
I checked C://Users/user/.m2/repository and I can see those dependencies in .jar files in respective folders/packages. However when I try to import those in code:
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
I get Cannot resolve symbol error:
I tried;
mvn clean
mvn install
mvn clean install
I get compile error on mvn install. I tried removing all source code fromt he project and then mvn install, it resulted in Build Success, however once the source code is added, I can't use the dependency classes.

This answer helped me. Turned out the dependencies Maven downloaded were assigned a Run time scope (whose idea was to invent this useless error-prone scope??). I set the scope to compile (File - Project structure - Modules - Dependencies - Scope) and it works fine.

Something is wrong you should check your project from first:
maven Repositories in your IDE Updated True?
run "mvn Clean" in IDE Console
Reimport All maven Project
go to pom.xml and check your dependencies.
Good luck.

Related

Order for building dependencies by mvn

Greeting, guys.
Could you please help me solve the issue:
In my pom.xml (for example application calls 'maven-Hell') i have 2 dependencies:
<dependency>
<groupId>com.dor.lub</groupId>
<artifactId>aaa</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.dor.dabu</groupId>
<artifactId>ddd</artifactId>
<version>3.3.3</version>
</dependency>
When i run mvn clean install for 'maven-Hell' application I want to build (clean install, as well) for two dependencies (see above) before 'maven-Hell' app.
P.S. They are also my modules, not like parent and child.
Any idea how to do it?
Thanks!
mvn clean install -pl MICRO-SERVICE-NAME -P mvnProfile -am -f pom.xml
Mvn will run clean install on owns modules from project

Cannot resolve symbol MockedStatic

I'm trying to import MockedStatic into a test but am not having any luck. My pom.xml file contains:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.10.0</version>
<scope>test</scope>
</dependency>
I use the following line to build the project:
mvn clean -U install appengine:run -Dapp.devserver.port=8888 -DskipTests=true
I am using IntelliJ and I get a "cannot find symbol" error for the import:
import org.mockito.MockedStatic;
Any idea what I'm doing wrong?
Turns out reloading project was all that was needed, as tgdavies suggested.
With Maven Helper plugin: right click on project folder > Run Maven > Reimport

Import JAR files to spring using maven

I tried to import some JAR files to my maven spring project using maven install plugin.
I placed the JARs in a lib folder in my base directory (where the POM.XML file is) and installed them one by one manually by running mvn install.
My xml looks like:
EDIT:
<dependency>
<groupId>com.keydoxWeb</groupId>
<artifactId>keydox</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>myPath\codecs.jar</systemPath>
</dependency>
<!-- and so on.. -->
Still telling me this error:
"Should use a variable instead of a hard coded path"
To import jars to your local repo, you generally would not have to or want to edit a pom.xml file. Rather there are shell commands that you can use to import the jars to your local maven repo (typically located at ~/.m2). Said commands are described here -- looks like this:
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
-DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
Once you do this, you'll also have to bring the dependencies into your projects pom.xml as explicit dependencies. That will look like this:
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.0</version>
</dependency>
...
</dependencies>
Hope it helps!
Usually you do not have to import jars manually - they are installed by maven into local repository. And eclipse needs to know where this maven repository is. You may regenerate eclipse project files via
mvn eclipse:eclipse
(or switch to IntelliJ IDEA which opens maven projects natively)

Unable To Use ch.qos.logback.classic Classes

I have spring project setup on Intellij Idea 2016.2 using Maven. For some reason I cannot import or use any class present in ch.qos.logback.classic package. I tried to invalidate cache, re-import maven dependencies.
For example with
import ch.qos.logback.classic.Level;
the IDE says 'cannot resolve symbol Level'. When compiling from command line it says 'package ch.qos.logback.classic does not exist'. Any suggestion what might be wrong?
Update - found the issue. I had set dependency scope to compile. Updating this fixed the issue.
Have you added the ch.qos.logback:logback-classic Maven artifact in your dependencies?
You should have something like that in your pom :
<dependencies>
...
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.3</version>
</dependency>
...
<dependencies>

maven dependecies can't be imported

I have set-up a mave project in Eclipse, I added the project dependencies to the pom.xml which was created by eclipse automatically.
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId> org.springframework.core </artifactId>
<version>3.0.6.RELEASE </version>
</dependency>
</dependencies>
Now when I import the Jdbc template in one of the classes, I get the import can't be resolved error
import org.springframework.jdbc.core.JdbcTemplate;
Are dependencies added during compilation time, or execution time only? if they are only available at execution time, then how can I compile the code?
One way to verify if maven dependencies are added to your project or not in eclipse is under the project ->libraries->Maven Dependencies, all the dependency you have added in your pom.xml should be present
In your case spring-jdbc{version}.jar should there else try to update the project, while updating the project by default all the dependencies would be downloaded to your home directory/.m2/repository. If you find your dependencies are not present check your proxy settings. http://maven.apache.org/guides/mini/guide-proxies.html
The simple problem you have is that the class org.springframework.jdbc.core.JdbcTemplate is contained in the following artifact:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
but not in the spring-core nor as transitive dep. Apart from that you should first check to build the project on command line with Maven and afterward import it into Eclipse.

Categories