Package does not exist when using Maven - mongodb

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>mavenproject1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mavenproject1</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
When I try to import com.mongodb, I get a package does not exist error. I had to manually add the .jar file to make it work. How do I get Maven dependency to work?
Thank you

You have the maven coordinate correct, and the artifact does exist on maven central repository. So I strongly believe your maven settings had configuration problem causing it unable to fetch from central repository.
This commonly occur when you're behind a company internal repository such as nexus and it has stale index, or you set your ~/.m2/settings.xml to NOT lookup from the central repo.

Related

In pom .xml it's showing error: Failure to transfer org.apache.maven.shared:file-management:pom:3.0.0

I created the project from Spring Initializr: https://start.spring.io/.
After importing it as a maven project in eclipse it's showing error in the pom.xml file that:
Description Resource Path Location Type Failure to transfer
org.apache.maven.shared:file-management:pom:3.0.0 from
https://repo.maven.apache.org/maven2 was cached in the local
repository, resolution will not be reattempted until the update
interval of central has elapsed or updates are forced. Original error:
Could not transfer artifact
org.apache.maven.shared:file-management:pom:3.0.0 from/to central
(https://repo.maven.apache.org/maven2):
repo.maven.apache.org pom.xml /SpringApp line 1 Maven Configuration
Problem
Error marked only the first line of code.
My pom.xml :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<groupId>com.example</groupId>
<artifactId>SpringApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringApp</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
This is due to corrupted/failed downloads which are causing the problem to update your maven project.
If you are a windows user please run this command in CMD
cd %userprofile%\.m2\repository
for /r %i in (*.lastUpdated) do del %i
this will delete all the failed downloads from the local repository.
after this completes you can update dependencies of your project by
Right-click on the project->Maven->Update Project->check the checkbox "Force Update of Snapshots/Releases" and click OK.

Maven run tests specified in my Eclipse Folder(Path- src/test/java/pakage) but Test Results are not shown correctly

I have a Maven Project in eclipse. On running pom.xml file it runs my 2 tests mentioned on path- src/test/java/pakage.
But,When I check the test results in index.html file in the test-output folder - it shows only 1 test that too method name and class name is not correct.
Also in the info section of htlml file, location is little weird- shows like this -C:\Users..\AppData\Local\Temp\testng-eclipse--1598554117\testng-customsuite.xml (I am expecting my Project location which is- d drive).
Not sure what is going wrong.can somebody plz help.
My pom.xml is like this-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Maven_demo</groupId>
<artifactId>Maven_Project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<name>Maven_Project</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
my Project structure is like this- img attached --https://i.stack.imgur.com/1FqDe.jpg

SpringBootApplication can not be resolved to a type error in Spring tool suite and maven

I have created the maven project in spring tool suite. I created the pom.xml with required dependencies but facing error as "SpringBootApplication can not be resolved to a type".
I have tried a couple of solutions like Maven -> Update Project..., Clean Project, deleted local repository directory of Maven (.m2), etc... also I tried creating another maven project but still the same error.
I think this is eclipse spring tool suite problem with maven.
Is there any solution like patch by eclipse or anything else?
Thanks in advance!
Pom.xml as follows
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.javabrains.springbootquickstart</groupId>
<artifactId>cource.api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Java Brains Cource API</name>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<type>pom</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<properties>
<java.version>1.8</java.version>
</properties>
</project>
You did not add the correct dependencies.
You should add
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
as a dependency (in <dependencies>) instead of spring-boot-starter-parent.
Parent just defines the versions but does not actually add any dependency (you use it as a <parent> in Maven).
If you have test, also add spring-boot-starter-test.
I would also suggest to use https://start.spring.io/ to generate a working Spring Boot base project (also available from STS in File->new->Other->Spring Starter project).
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.javabrains.springbootquickstart</groupId>
<artifactId>cource.api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Java Brains Cource API</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<properties>
<java.version>1.8</java.version>
</properties>
</project>
Copy/Paste it in your POM.xml. Update the Maven project and it will fetch the required JARs.

Eclipse with Maven plugin does not work with latest revisions, but works with older ones

I have a simple project that uses Spring AOP. It uses annotation from AspectJ. The project is in Maven and it compiles ok with aspectjtools-1.7.4. With aspectjtools-1.8.9 it fails to see accept annotation #Aspect imported from org.aspectj.lang.annotation.Aspect. The same project gives no problems with Gradle and same JARs.
Why would my code not work with latest revision of AspectJ, but works ok with older one?
I am attaching pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>aspectjproblem</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>aspectjproblem</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<!-- WITH 1.8.9: FAILS to see org.aspectj.lang.annotation.Aspect -->
<!-- <version>1.8.9</version> -->
<!-- WITH 1.7.4: SEES OK: org.aspectj.lang.annotation.Aspect -->
<version>1.7.4</version>
</dependency>
</dependencies>
</project>
I guess it must have something to do with compliance-levels or similar things. Can anyone point me in the right direction? Many thanks.
It took me a while to understand what was the issue. I tried moving to Java8 and I played with dependencies. The problem I had was:
- Invalid LOC header (bad signature)
What means I guess Maven downloaded a corrupted file. I deleted it from repo (e.g. C:\Users\$USER.m2\repository\org\aspectj\aspectjtools ), updated maven and all started working again.

Eclipse m2e not generating Maven Libraries for Java Resources

I am currently trying to create a m2e maven-archtype-webapp project in Eclipse, but I notice that it does not generate the Maven Libraries under the Java Resources directory.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ord.code.ranch</groupId>
<artifactId>somewebapp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>somewebapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>somewebapp</finalName>
</build>
</project>
Eclipse Juno - Project Explorer