Why is Springs auto generated pom file un parsable? - java

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.

Related

Project build error.Non resolvalbe parent POM in pom.xml in java

I am getting below error in pom.xml in springboot java application. I changed java version ,springboot version but error remains same. what should I do?
below is 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 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.13</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I saw some of the answers and played with relativePath but did not work what should I do?
This is an common problem facing now a days. This error causes because your editor haven't installed the spring boot properly. This you have to install all the maven files properly. Just follow the below steps to solve the problem.
Right click your spring boot project (demo) and select the option maven.
2. From the maven option you will get list of options. There you select update project or select download source.
Then your editor will install all the maven files for your spring boot project. And your error will be resolved.
Or
If it does not work just uninstall your spring tool suite and reinstall properly.
Any clarification please don't hesitate to comment your issue.. I am waiting for your comment.. Thank you

Difference between test and src directory/ code doesent work at test folder

Every tutorial about JUnit suggest code exact like that. In class within src catalog code work, but when I try to write it in my test class, it has a problem with compatibility.
What are the differences between these directories? How to fix that issue ?
**Error:(20, 29) java: incompatible types: java.util.List<"java.lang.Object> cannot be converted to
java.lang.Iterable <"java.lang.Object[]>
#Parameterized.Parameters
public static Iterable<Object[]> testData(){
return Arrays.asList(new Object[][] {
{2, BigDecimal.valueOf(123.456), BigDecimal.valueOf(123.45)},
{0, BigDecimal.valueOf(123.456), BigDecimal.valueOf(123)},
{1, BigDecimal.valueOf(123.4), BigDecimal.valueOf(123.4)}
});
}
Here is pom.xml
Java version here and in Configuration both at JUnit and Application is 11
<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.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Nevermind, that was the problem:
import org.assertj.core.util.Arrays;

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

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>

Spring boot deployments could not see .class file

In my project i build a multi modules dependency application , it is running perfectly in compilation/development mode , but when packaging it in a war file , it gives me NoClassDefFound for entity class which I used in Controller module.
The project structure is as follow :
1. Project root
1.1 Controller dependency module
1.2 Model dependency module
1.3 Core dependency module
and the root pom file is packaging as pom and all others are Jar except Controller module which is packaging as War file; as a test I tried to build only Controller with Model dependency only and the pom file for both are attached here :
Root pom 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.egabi.fatca</groupId>
<artifactId>fatca</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<name>fatca</name>
<url>http://maven.apache.org</url>
<description>Structure project for FATCA</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.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>
<modules>
<module>model</module>
<module>common</module>
<module>core</module>
<module>api</module>
</modules>
<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>
<!--<distributionManagement>-->
<!--<repository>-->
<!--<id>releases</id>-->
<!--<url>http://10.3.1.73:9990/content/repositories/releases</url>-->
<!--</repository>-->
<!--</distributionManagement>--></project>
The Model pom 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>fatca-model</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<url>http://maven.apache.org</url>
<name>model</name>
<description>Model layer for fatca</description>
<parent>
<groupId>com.egabi.fatca</groupId>
<artifactId>fatca</artifactId>
<version>1.0.0</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
and finally the Controller pom 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>fatca-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>api</name>
<description>Controller layer for fatca</description>
<parent>
<groupId>com.egabi.fatca</groupId>
<artifactId>fatca</artifactId>
<version>1.0.0</version>
</parent>
<properties>
<start-class>com.egabi.fatca.FatcaApplication</start-class>
<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>com.egabi.fatca</groupId>
<artifactId>fatca-model</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
<!--<repositories>-->
<!--<repository>-->
<!--<id>spring-snapshots</id>-->
<!--<name>Spring Snapshots</name>-->
<!--<url>http://repo.spring.io/snapshot</url>-->
<!--<snapshots>-->
<!--<enabled>true</enabled>-->
<!--</snapshots>-->
<!--</repository>-->
<!--<repository>-->
<!--<id>spring-milestones</id>-->
<!--<name>Spring Milestones</name>-->
<!--<url>http://repo.spring.io/milestone</url>-->
<!--<snapshots>-->
<!--<enabled>false</enabled>-->
<!--</snapshots>-->
<!--</repository>-->
<!--</repositories>-->
<!--<pluginRepositories>-->
<!--<pluginRepository>-->
<!--<id>spring-snapshots</id>-->
<!--<name>Spring Snapshots</name>-->
<!--<url>http://repo.spring.io/snapshot</url>-->
<!--<snapshots>-->
<!--<enabled>true</enabled>-->
<!--</snapshots>-->
<!--</pluginRepository>-->
<!--<pluginRepository>-->
<!--<id>spring-milestones</id>-->
<!--<name>Spring Milestones</name>-->
<!--<url>http://repo.spring.io/milestone</url>-->
<!--<snapshots>-->
<!--<enabled>false</enabled>-->
<!--</snapshots>-->
<!--</pluginRepository>-->
<!--</pluginRepositories>-->
<!--<build>-->
<!--<plugins>-->
<!--<plugin>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-maven-plugin</artifactId>-->
<!--</plugin>-->
<!--</plugins>-->
<!--</build>-->
</project>
Also note i figured out that the model jar is exist in the final generated war ; and after investigating i found that parent module like api cannot see the packages defined in its child modules like Model , Core ,etc.
I tried to use #ComponentScan and it worked between 2 modules only, so my issue now is to make api parent see all his child modules .
I searched for my question in some solution here but it did not solve my issue , so your help is appreciated.
A few things:
In your child project the dependency should be
<dependency>
<groupId>com.egabi.fatca</groupId>
<artifactId>fatca-model</artifactId>
<version>${project.version}</version>
</dependency>
Also your child projects needs only an artifactId, they will get their groupId and version from the parent, take those tags out (version and groupId) from the children.
Build the whole thing as one multi project from the root:
mvn clean install
i think you need to use this annotations
#EnableJpaRepositories(basePackages = "your.path.to.module.data.dao")
#EntityScan(basePackages = "your.path.to.module.data.entity")
you have set the #ComponentScan with the right path to find the file.
something like this.
#Configuration
#ComponentScan("com.your.path")
public class ContextWithJavaConfig {
}
or for multiple paths,
something like this.
#Configuration
#ComponentScan(basePackages={"com.firstpackage","com.secondpackage"})
public class ContextWithJavaConfig {
}

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