How to override the Spring Data Mongo version from Parent Project? - java

In my project, we've Parent module which is none of in our control and has using mongodb-driver-core-3.6.4.jar and spring-data-mongodb-2.0.11.RELEASE.jar. But somehow I want to use latest versions of both mongodb-driver-core and spring-data-mongodb. How to override this from Parent module ?
I already went through the link: https://spring.io/blog/2016/04/13/overriding-dependency-versions-with-spring-boot, but doesn't works for me.
I want to either override Spring Boot starter version or want to use somehow mongodb-driver-3.8.2.jar and spring-boot-2.1.4.RELEASE.jar
I've pom.xml file something like below:
<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>com.example.ahr</groupId>
<artifactId>ahr-rest-api-starter-parent</artifactId>
<version>2.3.1</version>
</parent>
<groupId>com.example</groupId>
<artifactId>ahr-data</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ahr-data</name>
<description>ahr Data Service </description>
<properties>
<java.version>1.8</java.version>
<mongo.version>2.1.4.RELEASE</mongo.version>
</properties>
............
..................
.................

We simply need to override the spring-boot-dependencies from the Parent module, so that Spring Data Mongo starter can pick up the latest versions of itself.
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.4.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Here is the one more change we need to make -
<properties>
<java.version>1.8</java.version>
<spring.boot.version>2.1.4.RELEASE</spring.boot.version>
</properties>

Related

Maven: why are dependencies declared in parent pom not inherited by child pom?

When I declare a dependency in a parent pom like -
<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>
<groupId>com.demo</groupId>
<artifactId>deps</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<!-- not relevant for this question -->
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.12</version>
</dependency>
</dependencies>
</project>
above i have declared spring-core as a dependency for parent pom.
Now in child pom, i am importing the parent pom -
<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>
<groupId>com.demo</groupId>
<artifactId>deps2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.demo</groupId>
<artifactId>deps</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- not relevant for this question -->
</dependencies>
</project>
Now on looking at the dependencies inherited by child pom, there are none. Should NOT the spring-core jar be inherited by child project in all cases. As the parent pom directly depends on this jar and is it not passed on/inherited by child projects.
Note: This question is not about dependency management and versions
I understand dependencyManagement, which is to ensure that a set of projects have the same version and scope of a depencency.
There is a difference between placing a parent tag and Bill of materials (importing the pom in the dependency management section)
Using <parent> is a "real inheritance" in maven. You define this tag on the child pom and by that you will get all the dependencies defined in the parent pom automatically (also properties and plugins).
The Bill of Materials on the other hand (This is how its called in the official documentation) doesn't import any dependencies by itself, however it allows to avoid specifying the versions of the dependencies in the pom of your application, because you define them in this BOM.
So to answer your question, you should really rewrite the child pom as:
<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>
<groupId>com.demo</groupId>
<artifactId>deps2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>com.demo</groupId>
<artifactId>deps</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<!-- not relevant for this question -->
</dependencies>
</project>
Your child pom is a standalone pom because you didn't specified a parent. You define a parent by adding this tag :
<parent>
<groupId>yourpackage</groupId>
<artifactId>yourartifactid</artifactId>
<version>version</version>
</parent>
In your case, this block should do the trick :
<parent>
<groupId>com.demo</groupId>
<artifactId>deps</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
I know that your question was not about depedencies management; but for people that would not know the difference I'll write some words about that.
Note that by importing your pom in <dependenciesManagement> you don't have any impact given that it only defines intentions of use but not concrete import. The <dependencies> contains the concrete imports and it's only its content that you can use in your application.

Why is Springs auto generated pom file un parsable?

I've been trying to setup a spring websocket server using the stat.spring.io generator which generates a pom file etc.
It seems that the generated pomfile is unparsable for some reason and i've spent hours trying to fix it.
I've tried using Java 1.7 with spring 2.. to no avail.
Please can someone help explain why it might not be parsing:
Here is my full pomfile:
<?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.example</groupId>
<artifactId>ws-analytics-gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ws-analytics-gateway</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.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-websocket</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>
The pom.xml you have posted is not valid XML. it is missing a closing tag for project as mentioned above. Also, you mention using java 1.7 with spring 2.
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Release-Notes#java-8-baseline-and-java-9-support states:
Spring Boot 2.0 requires Java 8 as a minimum version.
I checked your XMl by pasting it here: https://www.xmlvalidation.com/index.php?id=1&L=0 you may wish to have that check pass first.

Project build error: Non-resolvable parent POM for Application

I am creating my first spring boot application using maven and i am getting this error:
Non-resolvable parent POM for
Bank_Application:Bank_Application:0.0.1-SNAPSHOT: Could not transfer
artifact
org.springframework.boot:spring-boot-starter-parent:pom:1.4.2.RELEASE
This is my 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>Bank_Application</groupId>
<artifactId>Bank_Application</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
</project>
You probably have a proxy issue, that's quite common. (especially in banks where security policies are often very strict). See the guide to Configuring a proxy, there are also a lot of questions on SO related to such issue.
If you don't know what your proxy configuration ought to be, discuss it with your IT services.

pom use a common dependency for 3 applications Spring-mvc

I have a dependency called "general-lib" which will be modified and used by 3 teams.
Admin, Child-ABC and Child-XYZ are those 3 projects. Those 3 apps
deployed in same server.
Child-XYZ and Child-ABC are communicating to
Admin app frequently.
When we change general-lib version, I want
that Child apps also should use same version what admin app uses.
Finally that particular dependency version should be managed at super application.
is there any to do that ? Please let me know if I need to explain better.
You can define a BOM (Bills of Materials) where you can move the dependecyManagement for the common artifacts and then declare it as a parent in your 3 projects. This is an example of BOM:
<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>your.group.id</groupId>
<artifactId>whatever-BOM</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<general-lib.version>1.0.2</general-lib.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>your.groupid</groupId>
<artifactId>general-lib</artifactId>
<version>${general-lib.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
For more details on BOM, you can read the article Spring with Maven BOM that even if it's related to Spring, it will explain in a detailed way what are BOMs and how to use them.
Ohter possibility, is to define those 3 projects as a module of a higher level project and manage in this one the dependencyManagement.
<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>your.group.id</groupId>
<artifactId>whatever</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>Admin</module>
<module>Child-ABC</module>
<module>Child-XYZ</module>
</modules>
<packaging>pom</packaging>
<properties>
<general-lib.version>1.0.2</general-lib.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>your.groupid</groupId>
<artifactId>general-lib</artifactId>
<version>${general-lib.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

Spring Boot/Web with MySql database

I have read various SO question, spring tutorials and looked at different example projects, but i still do not get it.
Among others were these:
unable to connect spring with mysql
http://www.codejava.net/frameworks/spring/spring-mvc-with-jdbctemplate-example
My goal is to get a GRADLE spring boot/web application that connects to my MySql database.
My problem is there are many different ways to configure your project.
I would like to use as few XML as possible.
So my question is, whether it is possible to achieve my goal without having a single XML file and if so - how?
Or if i need an XML file(it should then be the pom.xml I think).
I would like an explanation why I need it and what the file does in general.
Please NOTE: I have about zero knowledge how to configure spring, mysql, hibernate.
So also things that may be straightforward for you, could be unclear to me.
So I would really appreciate explanantions.
EDIT2:
I got an sample application running using Spring Initializer
BUT, this sample application uses Maven - to be exactly it uses this 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>
<groupId>org.test</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>demo.DemoApplication</start-class>
<java.version>1.7</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-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
Is there an eaasy way I can replace this code with gradle or some kind of tutorial that tells me how to replace this code with the correct gradle file?
The simplest way may be for you to download a "bootstrapped" project using the Spring Initializr. You can select the dependencies that you require and the build system, and it will generate your project for you.
For Spring Boot to connect to your MySQL instance, it should be sufficient to just have the following properties in your application.properties file:
spring.datasource.url=jdbc:mysql://localhost:3306/db
spring.datasource.username=user
spring.datasource.password=password
See the Spring Boot Reference for Working with SQL databases for more information.

Categories

Resources