I am trying to create a multi-module project by using JPMS and Gradle multi-project functionalities. I have 3 modules for the testing: app, teacher and student. The student depends on the teacher and the app depends on both. Student and teacher modules have their own controller, service, entity, and repository. Each module has module-info.java file.
module app {
requires java.sql;
requires org.hibernate.orm.core;
requires spring.boot;
requires spring.data.jpa;
requires spring.boot.autoconfigure;
requires spring.core;
requires spring.beans;
requires spring.context;
requires teacher;
requires student;
opens az.chaypay.app to spring.core;
exports az.chaypay.app to spring.beans, spring.context;
}
module teacher {
requires static lombok;
requires spring.web;
exports az.chaypay.teacher to app;
exports az.chaypay.teacher.api to student;
exports az.chaypay.teacher.controller to spring.beans;
exports az.chaypay.teacher.service.impl to spring.beans;
opens az.chaypay.teacher.controller to spring.core;
opens az.chaypay.teacher.repository.dao to spring.core, com.fasterxml.jackson.databind, org.hibernate.orm.core;
}
module student {
requires static lombok;
requires com.fasterxml.jackson.databind;
requires spring.web;
exports az.chaypay.student to app;
exports az.chaypay.student.controller to spring.beans;
exports az.chaypay.student.service.impl to spring.beans;
opens az.chaypay.student.controller to spring.core;
opens az.chaypay.student.repository.dao to spring.core, com.fasterxml.jackson.databind, org.hibernate.orm.core;
requires teacher;
}
When I run my application with these settings everything works. Applications are successfully built and run. But IntelliJ complains about imports.
Package 'org.springframework.data.jpa.repository' is declared in module 'spring.data.jpa', but module 'student' does not read it
I don't understand that if Java has a problem with imports then why does my application run without errors. Why does my application run without requires statement?
CodePudding user response:
The reason that it "seems" to work is that spring-data-jpa
is not a modular dependency. It can be included in a modular project using the automatic module name spring.data.jpa
.
When you add the requires spring.data.jpa
to the app
module, it is available on the modulepath (with all its packages exported) in the unnamed module.
But once there, student
can actually see that same unnamed module when you execute it, so it "happens" to work, but is incorrect.
Both your app
and student
modules depend on classes in the org.springframework.data.jpa.repository
package, so the IntelliJ suggestion to add requires spring.data.jpa
to the student
module descriptor is correct.