Maven doesn't run unit tests in a multi-module project with Spring Boot - maven

Maven supports multi-module applications by having the child modules point to the parent module, but Spring Boot wants it's parent to be
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
According to the Spring docs I can still do this (use a maven parent-child relationship) if I add dependency management like this:
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
instead of the Spring Boot parent.
I've done this, but now my unit tests don't run when I run mvn from the command line. The unit tests still work from Intellij, but something in the new parent-child / changes is confusing mvn and keeping it from running tests.
Here's a pom.xml from a module that no longer runs its tests:
<project xmlns=...>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>abcd</groupId>
<artifactId>abcd.parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>entities</artifactId>
<name>entities</name>
<description>Entities and Repositories</description>
<properties>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
</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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
This module is a library module that does not contain a #SpringBootApplication but the tests ran before converting to the maven parent-child structure.
Edit:
This project has both junit 4 and junit-jupiter-api 5.5.2 in its dependencies. When using org.junit.jupiter.api.Test (5.5.2) no tests run. When using org.junit.Test (4.12) the test runs but the Autowired repository doesn't get injected (null). (The Autowired repository is in another module.)
Spring doesn't handle dependencies correctly when using a normal maven child-parent structure.

Related

I can't use a property for my project's parent version despite described as such in Spring Cloud doc

In Spring Cloud Config documentation, pom.xml's parent configuration is presented with a version variable spring-boot-docs-version as such 1:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>{spring-boot-docs-version}</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>{spring-cloud-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
So I added the following in my pom.xml's properties:
<properties>
<spring-boot-docs-version>2.1.8.RELEASE</spring-boot-docs-version>
<spring-cloud-version>Greenwich.SR3</spring-cloud-version>
</properties>
But it doesn't seem to work; even by prefixing the {spring-boot-docs-version} expression with $ doesn't make maven resolve the parent POM.
Note however that I had to add this extra $ in the <dependencyManagement>:
<version>${spring-cloud-version}</version>
I searched the Spring Boot documentation too, but only found out it's not using a variable 2:
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
</parent>
How to use such a variable in these (external) parents' <version>tag?
Note: I also read that if you don't inherit from one of these starters, you'll loose some maven pre-configuration (like plugins & so.)

Spring-boot(maven) actuator,devtools not working

I'm a newbie to spring-boot. Trying to create a simple web application using spring-boot maven. Basic static page display using dependency spring-boot-starter-web worked fine. When I included spring-boot-devtools and spring-boot-starter-actuator in pom.xml it is giving build error Re-run Maven using the -X switch to enable full debug logging.
I tried including spring-webmvc dependency in pom file, still the result is same.
<?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.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>jspTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jspTest</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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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>
</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>
I downloaded this project from spring.io (SPRING INITIALIZR website). Build project is getting failed.
<scope>runtime</scope> of spring-boot-devtools suppose not to be runtime. In fact according to the doc -
Developer tools are automatically disabled when running a fully
packaged application. If your application is launched from java -jar
or if it is started from a special classloader, then it is considered
a “production application”. Flagging the dependency as optional in
Maven or using a customdevelopmentOnly configuration in Gradle (as
shown above) is a best practice that prevents devtools from being
transitively applied to other modules that use your project.
So, change the dependency as below -
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
you can update your pom.xml and change the version number to any previous stable version.It worked for me.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>**xyz_previous_.RELEASE**</version>
<relativePath/> <!-- lookup parent from repository -->

Spring Boot: does #Entity Annotation exist?

I watched this Spring Boot tutorial: https://javabrains.io/courses/spring_bootquickstart/lessons/Creating-a-Spring-Data-JPA-Repository
and it puts an #Entity annotation before a class name. When I tried to do it in my code, I got an Error saying "can't resolve #Entity".
Here is my 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</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.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.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.1.0</version>
<scope>compile</scope>
</dependency>
<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.apache.derby</groupId>
<artifactId>derby</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>
Do you know what is the problem?
I also faced this problem recently. For me, the spring-boot-starter-data-jpa dependency was the issue. Although it was in the pom.xml file without any errors, but the jars were not there in classpath - even after syncing maven and importing dependencies multiple times in different ways ( mvn clean/update/install).
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
The IDE I was using was IntelliJ Idea.
What worked for me at last : I just put this dependency over all other dependencies inside the <dependencies> ... </dependencies> tag, i.e just changed the order of dependencies and synced. That worked in my case, not sure why.
Below is my pom.xml file contents for reference.
<?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>dev.sanskar.courses</groupId>
<artifactId>course-api</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<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>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api -->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
<!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.1-api -->
<!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.1-api -->
<!-- https://mvnrepository.com/artifact/javax.persistence/persistence-api -->
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Use spring-boot-starter-data-jpa
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
It works for me ... :)
Do not use the following one
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
I added this dependency
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
</dependency>
Add this and enjoy.
Spring STS usually shows this error when the set up isn't correct.
Go to the line containing the error
Click on the alert bulb so that it can suggest possible fixes
From the options, choose the "Fix project setup"
Follow the rest so that the project then re-aligns the import order.
suppose you have missed a dependency.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
There are useful sample spring boot projects in this github page.it will be helpful to have a look.
I had the same problem. After I configured the new dependency on pom.xml file, the #Entity annotation is not found because these dependencies is not found. To solve it, the IntelliJ IDE then suggested me to load the Maven changes. On the pom.xml screen, click on the icon to reload Maven.
Make sure you Maven projects are imported successfully.
In the lower right corner of your IntelliJ there will a prompt asking you if you want to import the changes. It will also ask you if you want to enable Auto-Import.
In my case, by adding the spring-boot-starter-data-jpa dependency to the classpath it worked.
Do a force update mvn clean install -U or you can right click on your project -->maven--> update if you are using eclipse/STS. this synchronizes the IDE with pom dependencies.
Seems you are working with EclipseLink JPA implementation,
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.1.0</version>
<scope>compile</scope>
</dependency>
Spring Data JPA works with Hibernate implementation by default. We need to exclude unnecessary Hibernate dependencies from spring-boot-starter-data-jpawhen working with other JPA implementations.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</exclusion>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</exclusion>
</exclusions>
</dependency>
If you are working with Hibernate, just spring-boot-starter-data-jpa is enough in your case.
I also seen "can't resolve #Entity" issue because of my IDE. In eclipse, click Project option -> Clean the project. This is solved my issue when project dependencies out of sync with IDE.
Click on the red bulb next to the #Entity annotation.
Choose "Add dependency" - choose "javax.persistence.api" (NOT jakarta)
Most of the answers here are hit and try types which doesn't explain why #Entity annotation was not working and why it started working after adding certain dependencies.
Here's how it works-
For using persistence annotations you need to add relevant dependencies with their version, let's say we add spring-boot-starter-data-jpa as follows-
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.6.6</version>
</dependency>
Now when you are using Intellij Idea as your IDE, you need to reload your maven project to re read the dependencies from your pom.xml.
Once you do that, #Entity annotation would be available for use.
go to File -> Project Structure -> Project Settings -> Facets in Intellij Idea
click "+" and add "JPA" module
i faced same issue, after add this dependencies fixed my issue
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
</dependency>
What Worked For Me In Last:-
Go To File -> Project Structure -> Modules -> Click On Your Project Name
Then Go To Dependencies click "+" and Then -> Library -> From Maven
Then Search For javax.persistence.api Then Click Ok And Apply Changes
I had the same issues recently and i resolved it using adding this dependency that given below. I think the version change in spring boot made this error, now javax.persistence is not available instead we can use jakarta.persistence
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.6.Final</version>
<type>pom</type>
</dependency>
put this in your file at the top as an import statement
import javax.persistence.*;

Spring Boot: The managed version is 1.3.2.RELEASE The artifact is managed in org.springframework.boot:spring-boot-dependencies:1.3.2.RELEASE

I create a skeleton application use Spring boot. This 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lynas</groupId>
<artifactId>SpringMVCHibernate</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpringMVCHibernate</name>
<description>SpringMVCHibernate</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<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-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</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-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I stuck at this step:
Spring Boot: The managed version is 1.3.2.RELEASE The artifact is
managed in
org.springframework.boot:spring-boot-dependencies:1.3.2.RELEASE
When I try add Hiberante 5.1.0.Final manually, this notice appear:
Overriding managed version 4.3.11.Final for hibernate-core
Help me resolve these problem.
Spring Boot provides dependency management for Hibernate. The warning is Eclipse telling you that you've overridden this dependency management by declaring a version directly on a dependency. That's a risky thing to do as you may end up with a mixture of Hibernate versions on the classpath. In fact, looking at your pom, you've overridden the version of hibernate-core but not of hibernate-entitymanager. This means you'll have 5.1.0.Final of the former and 4.3.11.Final of the latter on the classpath. That will almost certainly lead to problems at runtime.
A safer way to use Hibernate 5 is to override Boot's dependency management. As you are using spring-boot-starter-parent as your pom's parent you can do that by overriding the hibernate.version property:
<properties>
<hibernate.version>5.1.0.Final</hibernate.version>
</properties>
This will ensure that all Hibernate modules for which Spring Boot provides dependency management will have the desired version.
Finally, a note of caution. Hibernate 5.1 is very new and contains some breaking changes, even from 5.0.x. As a result, you may run into some incompatibility problems. If you don't want to be right on the bleeding edge, 5.0.x may be a safer choice. It will become the default Hibernate version in Spring Boot 1.4.
Spring Boot automatically defines version for dependencies as listed in this appendix.
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#appendix-dependency-versions
Eclipse is just reminding about it. You can ignore the warning if you really want to change the version for that dependency.
Update:
See Andy's answer: https://stackoverflow.com/a/35385268/1433665
In addition to the answers above. My issue was an old version of STS/Eclipse. After reinstalling with the latest and greatest Spring Tools the error was resolved.
https://spring.io/tools

Should you use the parent if you are using the spring platform bom?

Some dependency versions are not in so I've added the spring platform BOM, is the parent declaration still useful?
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.1.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>1.1.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
I personally prefer to use platform-bom as a parent, i.e.
<parent>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>1.1.1.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
In this way I don't have to define spring-boot version number and it is automatically updated with newer version of spring platform and I don't have to worry about any inconsistencies.
See http://docs.spring.io/platform/docs/1.1.1.RELEASE/reference/htmlsingle/#appendix-dependency-versions for complete list of all managed dependencies.
EDIT: As pointed out by Andy Wilkinson, spring platform inherits spring-boot-starter-parent so all "sensible defaults" as described in http://docs.spring.io/spring-boot/docs/1.2.1.RELEASE/reference/htmlsingle/#using-boot-maven apply as well.
There is a important difference between importing a BOM (in the dependencyManagement section) and using a parent
The BOM imported in dependencyManagement only provides defaults for dependencies, but a Parent-way include the other sections too (plugins, plugin-managent, dependencies, dependencyManagement...)
So when you remove the parent spring-boot-starter-parent then you have to copy the the plugin-managent stuff you need first.

Resources