Import of jar in IntelliJ is successful and imports are resolved but mvn clean install and package fails - spring-boot

Some classes in my spring boot maven project were showing errors on some imports , so I had to add an external jar dependency in my IntellIJ (Community) . After adding it all the errors are gone , and i can see the jar is listed inside external libraries in IntelliJ . I try to run the spring boot project , works fine . But when i try to do a mvn clean install or mvn clean on the project directory , it fails showing import errors on the same classes for which purpose i had added the jar . I tried re-building the project , invalidating caches , but issue still remains the same . I am not able to understand if adding the JAR has resolved the import errors , and project also starts , why mvn phases are having failure as if no jar is added ?

In a Maven based project you must manage project build configuration, including dependency management in Maven pom.xml build files, not adding it via IDE interface. So you need to add the required dependencies using the Maven's dependency management mechanism, simply inside Maven dependencies section. e.g.
<dependencies>
...
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
...
</dependencies>
And by the way the IDE helps you with that.

Related

Cannot access class and methods from custom jars in Maven

I have created a project in java and compiled it as a jar with the mvn clean install command. The jar is in my local .m2 folder. I am trying to import it as a dependency in a new project. The jar is successfully imported into my new project but I cannot access any classes or methods in that jar.
<dependency>
<groupId>com.test</groupId>
<artifactId>rest-methods</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
This my custom maven dependency imported in my External Libraries
And this is when I try to use these class and any method inside it in my project:
When I try to add the maven dependency manually I can find it and I can click add but nothing happens
Am I missing some steps for using this class in my project? I am working on a Ubuntu 20.04 operating system
UPDATE: After looking in the Project Structure -> Project Settings -> Libraries I see that several files from the jar are marked as excluded (sources and JavaDocs):
Did they not get build properly when I created the jar ?

Intellij Spring maven install error "package does not exist"

Intellij version : Community 2019.2.3
Maven : 3.6.2
Spring : 2.2.0
I am trying to create a very simple Spring maven project with two sub-modules (one independent and another one dependent on independent one).
Root module - testmultimodule
Independent module - independent
Dependent module - dependent
testmultimodule pom.xml has all Spring related declaration and module definition
<modules>
<module>independent</module>
<module>dependant</module>
</modules>
Independent poom.xml is simplest and . only has parent maven declaration
<parent>
<artifactId>testmultimodule</artifactId>
<groupId>in.org.app</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
dependent module pom.xml has the dependency declaration as below to independent module
<dependencies>
<dependency>
<groupId>in.org.app</groupId>
<artifactId>independent</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
I have created a Test class under dependent module and using a User object from independent module. Initially, without the above dependency declaration, asa usual there was compilcation error.
As soon as I add the dependency and builld the project within Intellij IDE with the option "Build Prooject" option from "Build" menu, it successfully builds.
However, if I try to use Maven install option within Intellij right side window option. It always fails stating Error:(3,33) java: package in.org.app.independent.bo does not exist .
I am providing the GitHub URL for the test project , if you want to take a look and test by yourself.
GIT URL:
https://github.com/DhruboB/testmultimodule
I have tried all sort of tweaking found in internet so far e.g.
clearing Intellij Cache & restarting, mvn -U clean install, mvn scope verification, proxy etc.
Any further idea to resolve this? I need to solve this in the Community version of Intellij.
Your parent project includes the definition for the spring-boot-maven-plugin. This leads to each project defining this as a parent to be repacked to an executable JAR by this plugin. This repackaged JAR isn't useable as a dependency in another project.
Either you need to change the configuration of the spring-boot-maven-plugin for the project you want to use as a dependency. This is explained here in the Spring Boot Reference Guide. You now basically have 2 jars from this project, one plain and one executable.
If you don't need that project to be an executable JAR file then just move the spring-boot-maven-plugin to the project that needs to be. All other projects will no be basic JAR files again.
See also How to add a dependency to a Spring Boot Jar in another project?

#SpringBootApplication cannot be resolved to a type for Spring Boot version 2.0.3

I have created a project using Spring Initializr with Spring Boot V-2.0.3
I am using STS 3.9.4 on Ubuntu Machine.
I am getting these errors:
The import org.springframework.boot.autoconfigure.SpringBootApplication cannot be resolved
SpringBootApplication cannot be resolved to a type
I have already tried deleting the repository folder from .m2 folder and hitting the following commands
mvn clean dependency:tree
mvn clean compile
But still errors were not resolved than I added an Extra Dependency to the POM file
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-autoconfigure -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
I have updated the project by following the below steps after each step performed but still the error is not resolved.
STS Right click on project -> Maven -> Update Project
Go to STS Right click on project -> Maven -> Update Project.

Maven not importing External Library into project after adding to .pom file in Intellij

I'm having trouble correctly importing a library into a project that I'm running. I have added the library as a dependency in the .pom, refreshed the pom, run mvn clean install, and I have set auto-import up so that the project gets updated correctly, but the project does not get added as an External Library, and I can't use it in my project. I get no errors. What am I doing wrong?
Here is the relevant part of my pom
..properties
<crowd.version>2.5.0</crowd.version>
.. end properties
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.atlassian.crowd</groupId>
<artifactId>crowd-integration-springsecurity</artifactId>
<version>${crowd.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</dependencyManagement>
Here is the question I was following to debug my error:
Import Maven dependencies in IntelliJ IDEA
I think you missed the point of dependency management; read more in official docs. This is a feature that you can centralize common dependency information that is then shared been different projects. All by itself, this definioition will not import the dependency.
What you probably want is just a plain dependency: drop the dependencyManagement tags, and move you dependency into the correct block in the pom.

With a mysql-connector-java as "provided" dependency, my Maven build cannot connect to the data store

Because Tomcat tells us to have the mysql-connector-java in its lib/ directory, so that it can handle multiple projects, I had my dependency as provided:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
<scope>provided</scope>
</dependency>
And I extracted the jar archive mysql-connector-java-x.x.x-bin.jar from the downloaded dependency and copied it into the lib folder of the Tomcat server:
cp ~/.m2/repository/mysql/mysql-connector-java/5.1.36/mysql-connector-java-5.1.36.jar lib/
But when I now run a build the tests phase fails since it cannot connect to the data store.
The build would succeed if commenting out the provided scope.
There must be a simple way around this...
UPDATE: I could run the Maven Tomcat 7 command: mvn clean install tomcat7:run -Denv="preprod" after adding the mysql-connector-java dependency in the tomcat7-maven-plugin plugin. But I still cannot run the tests, I have a connection failed when running the maven-surefire-plugin tests.
I used both provided and test scopes as in:
<scope>provided test</scope>
and it now also make the connector in the tests phase.

Resources