SpringBoot: Adding 3rd Party jars in a Spring Boot Project - maven

I have set MAVEN paths and variables. I am able to run a sample SpringBoot project in Eclipse but what I want is I have a custom jar and I am using classes from that jar in my Spring Boot Project. When I include this custom jar and build the SpringBoot application, I get the following errors
[ERROR] The goal you specified requires a project to execute but there
is no POM in this directory (D:......\SpringBootDemo\target).
Please verify you invoked Maven from the correct directory. -> [Help
1]
org.apache.maven.lifecycle.MissingProjectException: The goal you
specified requires a project to execute but there is no POM in this
directory (D:......\SpringBootDemo\target).
Please verify you invoked Maven from the correct directory.
My POM.xml is as follows:
<groupId>com.demo</groupId>
<artifactId>SpringBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpringBootDemo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
Am I missing anything?
Any help will be greatly appreciated.
Thanks

Sometimes you will have 3rd party JARs that you need to put in your local repository for use in your builds, since they don't exist in any public repository like Maven Central. The JARs must be placed in the local repository in the correct place in order for it to be correctly picked up by Apache Maven. To make this easier, and less error prone, we have provide a goal in the maven-install-plugin which should make this relatively painless. To install a JAR in the local repository use the following command:
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
-DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
With version 2.5 of the maven-install-plugin it gets even better. If the JAR was built by Apache Maven, it'll contain a pom.xml in a subfolder of the META-INF directory, which will be read by default. In that case, all you need to do is:
mvn install:install-file -Dfile=<path-to-file>
From a Maven Guide to installing 3rd party JARs.

Related

ApplicationContext not being recognized even after adding Maven dependency

I started a course on spring boot and I am at dependency injection.
I ran into a problem with ApplicationContext which I can't seem to resolve:
Screenshot of the problem
I added the maven dependency as you can see in the pom file:
pom file screenshot
But it still doesn't recognize the ApplicationContext and I cannot find a solution anywhere.
EDIT: whole pom.xml file:
<?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 https://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.5.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>DependencyInjectionDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>DependencyInjectionDemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
You don't need to explicitly add spring-context dependency, since it is a transitive dependency of spring-boot-starter. Other than that it seems fine to me, but we can do three things:
Check that you are actually importing org.springframework.context.ApplicationContext and not some other class;
Force the reload of all your dependencies in IntelliJ (right click your project --> Maven --> Reload project);
Clear your .m2 folder (or at least org/springframework sub-path folder inside it). You might have some corrupted JARs in there.
From given screenshots I assume, you are using a Maven-project in IntelliJ.
Adding imports from classpath
Try to import the class org.springframework.context.ApplicationContext in the java class where your main method is (as shown in screenshot).
Usually IntelliJ should find the class ApplicationContext in your classpath. From your given POM and dependency spring-boot-starter the classpath should contain the class.
See:
ApplicationContext import in spring
Re-importing Maven dependencies
As João suggests try reimporting by context-action on your POM file (CTRL + SHIFT + O).
See:
Import Maven dependencies in IntelliJ IDEA
Maven dependencies | IntelliJ IDEA
Repair actions
In case nothing of the above work, either your IntelliJ project notconfigured correctly, or something got out of sync between IntelliJ and Maven. Then try following repair-actions (in order listed below):
Run mvn clean install -U (see --update-snapshots), either from IntelliJ's Maven tools or terminal, as explained in
Force re-download of release dependency using Maven
In IntelliJ's menu select Invalidate Caches / Restart... to reindex your project. This can help if IntelliJ and Maven got out of sync.
Delete your local Maven repository, usually the (hidden) folder .m2 in your user's home folder (see Clear the Maven Cache) and reload/reimport your POM in IntelliJ.

IDEA import of Maven GitHub projects not working/running -> "Cannot resolve symbol"

#### UPDATE v2 ####
Ok I found out the issue, it was in fact an IDEA bug. More precisely, it's the git extension, gitflowincrementalbuilder, which from 3.8+ breaks Idea. Changing version to 3.7 solves it, for now.
https://github.com/vackosar/gitflow-incremental-builder/issues/91
Intellij/git, please fix it
------ Old Update v1 -----
I just tried running the project with Eclipse... works perfectly without any issues whatsoever, at first try... So it's kind of a Intellij-IDEA bug/problem (...)
I am trying to run some examples from Github, the spring spring-boot ones from baeldung.com; more specifically this one (no one works in idea): https://github.com/eugenp/tutorials/tree/master/spring-mvc-simple-2
While it works using Maven commands, "mvn clean install"
and then "mvn spring-boot:run", wont work in Idea (it does clean and install ok, but no run). Project is imported using "New"->"Project from Existing Sources" (check images below for settings).
I think there is some problem with the pom imported configuration, especially since there is a multi module structure (parent tag); cannot even resolve #SpringBootApplication.
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-mvc-simple-2</artifactId>
<packaging>war</packaging>
<name>spring-mvc-simple-2</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-2</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
</plugin>
</plugins>
<finalName>spring-mvc-simple2</finalName>
</build>
I have been trying to add a SpringBoot configuration manually using IDEA gui but it doesnt recognize the application class (?). What partially works though is replacing the parent pom with:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
Tests still wont work, but I dont think u're supposed to change/edit the pom file manually to make things works...
I have been trying alot already: invalidate cache, maven reimport & Generate sources and update folders, using mvn first, checking all jdk configuration... nothing works.
You didn't build the parent module so IntelliJ does not find this in your local Maven repository.
You should run mvn install in the project:
https://github.com/eugenp/tutorials/blob/master/parent-boot-2/pom.xml
But also exchanging the parent helps like you described yourself.
To make the tests run you have to add the test dependency from the parent:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
</dependency>

jboss maven project add jar not exist in public repository

I need to add a jar not exist in publi repo to my maven project i have using system scope like this :
<dependency>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>X.Y.Z</version>
<scope>system</scope>
<systemPath>${user.home}/jars/my.jar</systemPath>
</dependency>
This solution working fine in local machine but not in distant server. when i google it i find that system scope is aleardy a bad practice so there is another solution to add a jar to project?
You have two options here.
First:
<dependency>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>X.Y.Z</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/yourJar.jar</systemPath>
</dependency>
Second:
Install this jar in your local repository and it will be included in your built project.
You can add jar in you local repository like this:
mvn install:install-file
-Dfile=<path-to-file>
-DgroupId=<group-id>
-DartifactId=<artifact-id>
-Dversion=<version>
-Dpackaging=<packaging>
-DgeneratePom=true
Where: <path-to-file> the path to the file to load
<group-id> the group that the file should be registered under
<artifact-id> the artifact name for the file
<version> the version of the file
<packaging> the packaging of the file e.g. jar
Edit your pom.xml
<dependency>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>X.Y.Z</version>
</dependency>

Non-resolvable parent POM: Could not transfer artifact org.springframework.boot

I am creating simple spring boot web application
I am using the project generated by spring Initializer
I have set my JDK and maven in the path
Also done setting for JAVA_HOME
While running the application getting following error
Non-resolvable parent POM: Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom:1.5.3.RELEASE from/to central (http://repo.maven.apache.org/maven2): This is usually a temporary error during hostname resolution and means that the local server did not receive a response from an authoritative server (repo.maven.apache.org) and 'parent.relativePath' points at no local POM # line 14, column 10 -> [Help 2]
following is my POM in the project
<?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>
<groupId>com.iot.tme</groupId>
<artifactId>PowerBIDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>PowerBIDemo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
try to update maven dependencies by hands mvn dependency:purge-local-repository.
Incase you're working for an organisation and need VPN to work. You must connect to VPN before downloading the dependencies. This is what resolved my issue.
In my case, I was using wrong JDK version (1.7) and changing it to (1.8) worked.
For me what worked was that I downloaded the cacerts file provided in the same repository and stored that in the JAVA_HOME/jre/lib/security folder and then I right clicked the Project in the Package Explorer view of Eclipse, Project -> Maven -> Update Project (Alt+F5), and after that it started downloading the dependencies, from the various repos which were unreachable earlier, and later the problem got resolved.
I changed jdk 1.7 to jdk 1.8 and It worked and Don't forget also update your environment variables.
i have already answered it here.
try deleting the dependencies in .m2 and update and clean.
and also check the settings.xml is properly configured in window-->preferences-->maven-->User Settings.
For me it work by changing option settings of Netbeans.
Tools > Options > Team > Maven > Maven Home
Update by downloading maven manually from original website
check maven by using mvn -v. extra inst.(http://maven.apache.org/install.html)
Once Maven is installed, add the maven directory to Maven Home:
Tools > Options > Team > Maven > Maven Home

Passing a property value in Maven goal is not working. pom.xml variable does not reflect in to artifact pom.xml at maven repository

I am trying to get dependency version at run time from command line in maven, but it does not reflect into artifact pom at maven repository.
My project pom is like :-
<parent>
<groupId>com.company.project</groupId>
<artifactId>parentProject</artifactId>
<version>5.6.0.14</version>
</parent>
<properties>
<my.version>${my.version}</my.version>
</properties>
<groupId>com.company.project</groupId>
<artifactId>childProject</artifactId>
<dependencies>
<dependency>
<artifactId>someArtifact_one</artifactId>
<groupId>com.company.project</groupId>
<version>${my.version}</version>
</dependency>
<dependency>
<artifactId>someArtifact_one</artifactId>
<groupId>com.company.project</groupId>
<version>${my.version}</version>
</dependency>
</dependencies>
My command is like - mvn install -Dmy.version=5.6.0.12, project is build successfully and uploaded at maven repository , but when I verify artifact pom.xml at maven repository its same as.
<dependency>
<artifactId>someArtifact_one</artifactId>
<groupId>com.company.project</groupId>
<version>${my.version}</version>
</dependency>
I think it should be like below at maven repository.
<dependency>
<artifactId>someArtifact_one</artifactId>
<groupId>com.company.project</groupId>
<version>5.6.0.12</version>
</dependency>
How could I resolve this issue, or if someone has some other solution for this issue please suggest.
This is mainly because, you are over-writing the value you pass in goal -Dmy.version=5.6.0.12 with the properties tag in pom.xml. To fix it, Either you can remove this properties tag completely (or) set it as below
<properties>
<my.version>5.6.0.12</my.version>
</properties>
Another way is to pass the arguments as : -Darguments="-Dparam1=val1 -Dparam2=val2" in CI environment
As mentioned in this article : enter link description here

Resources