For some reason, I'm unable to get a module within my project to detect that it has the dependencies in place to pick up #EnableJpaRepositories. Despite having implementation 'org.springframework.boot:spring-boot-starter-data-jpa' specified in my build.gradle file, when I run gradle to try and compile, I get the following error:
./gradlew clean build
> Task :rest:compileJava FAILED
/Users/pasdeignan/git/pasciifinance/rest/src/main/java/com/pasciitools/pasciifinance/rest/PasciifinanceApplication.java:22: error: cannot find symbol
#EnableJpaRespositories(basePackages="com.pasciitools.pasciifinance")
^
symbol: class EnableJpaRespositories
1 error
FAILURE: Build failed with an exception.
Background
I have a multi-module Spring project that I'm building where I'm trying to store the JPA pieces in a common module and the REST services in a rest module. The rest module is also where my SpringBootApplication lives. When I start up the application, I get the following error:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field accountRepo in com.pasciitools.pasciifinance.rest.PasciifinanceApplication required a bean of type 'com.pasciitools.pasciifinance.common.repositories.AccountRepository' that could not be found.
The injection point has the following annotations:
- #org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.pasciitools.pasciifinance.common.repositories.AccountRepository' in your configuration.
Project Structure
pasciifinance
└── build.gradle (empty)
└── settings.gradle
└── common
└── build.gradle
└── src
└── main/java/com/pasciitools/pasciifinance/common
└── entity
└── repository
└── AccountRepository.java
└── rest
└── build.gradle
└── src
└── main/java/com/pasciitools/pasciifinance/rest
└── PasciiFinanceApplication.java
└── restservice
└── RestService.java
PasciiFinanceApplication.java
package com.pasciitools.pasciifinance.rest;
import com.pasciitools.pasciifinance.common.entity.Account;
import com.pasciitools.pasciifinance.common.entity.AccountEntry;
import com.pasciitools.pasciifinance.common.repository.AccountEntryRepository;
import com.pasciitools.pasciifinance.common.repository.AccountRepository;
import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import java.util.List;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
#EnableEncryptableProperties
#EnableJpaRespositories(basePackages="com.pasciitools.pasciifinance")
#SpringBootApplication(scanBasePackages = "com.pasciitools.pasciifinance")
public class PasciifinanceApplication {
#Autowired
private AccountRepository accountRepo;
#Autowired
private AccountEntryRepository entryRepo;
private static final Logger log = LoggerFactory.getLogger(PasciifinanceApplication.class);
public static void main(String[] args) {
SpringApplication.run(PasciifinanceApplication.class, args);
}
}
build.gradle (rest module)
plugins {
id 'org.springframework.boot' version '2.4.3'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.pascii-tools'
version = '0.0.1-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation project(':common')
developmentOnly 'org.springframework.boot:spring-boot-devtools'
implementation 'org.apache.poi:poi:5.0.0'
implementation 'org.apache.poi:poi-ooxml:5.0.0'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
implementation 'org.seleniumhq.selenium:selenium-java:3.141.59'
implementation 'com.github.ulisesbocchio:jasypt-spring-boot:3.0.3'
implementation 'org.springframework.boot:spring-boot-starter-batch'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
runtimeOnly 'com.h2database:h2'
}
test {
useJUnitPlatform()
}
build.gradle (common)
plugins {
id 'org.springframework.boot' version '2.4.3'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.pascii-tools'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 11
repositories {
mavenCentral()
}
bootJar {
enabled = false
}
jar {
enabled = true
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
You have a typo in #EnableJpaRespositories.
You'll need to rename it to #EnableJpaRepositories, as you've asked in your question.
Related
I am migrating multi-module Gradle Java project from Gradle 4.10 to Gradle 6.3.
Project structure looks like the following:
├── build.gradle
├── our-webapp
│ ├── build.gradle
│ └── src
│ ├── main
│ ├── integrationTest
│ └── test
└── cli-util
├── build.gradle
└── src
├── main
├── integrationTest
└── test
cli-util has a dependency on webapp to use it's classes.
build.gradle looks like:
plugins {
id 'java'
}
sourceSets {
integrationTest {
compileClasspath += sourceSets.main.output + sourceSets.test.output
runtimeClasspath += sourceSets.main.output + sourceSets.test.output
java.srcDir 'src/integrationTest/java'
resources.srcDir 'src/integrationTest/resources'
}
}
configurations {
integrationTestImplementation.extendsFrom testImplementation
integrationTestRuntime.extendsFrom testRuntime
}
dependencies {
implementation project(':our-webapp')
}
task integrationTest(type: Test) {
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath
}
And now what is interesting is that tests under cli-util/src/intergrationTest are working fine in Intellij IDEA but they are failing from terminal whenever there is an import of a class defined in webapp project. The error is java.lang.NoClassDefFoundError: com/example/web/SomeClass.
I tried different approaches already, including the use of java-library plugin in cli-util project and importing webapp project like api project(':our-webapp') but it still fails.
Same issue in Gradle 5.6.x.
Is there some special change I have take into account to make it work?
I have a dependency problem in a gradle project and I do not know how to solve it.
I have a root project "project-root" and two subprojects "project-sub-a" and "project-sub-b".
The "project-sub-a" has a dependency from "project-sub-b".
The structure is:
project-root
|-settings.gradle
|-build.gradle
project-sub-a
|-build.gradle
project-sub-b
project-root:settings.gradle:
include 'project-sub-a', 'project-sub-b'
project-root:build.gradle:
plugins {
id "io.spring.dependency-management" version "1.0.8.RELEASE"
}
subprojects {
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'java-library'
repositories {
mavenCentral()
}
dependencies {
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-gradle-plugin
compile group: 'org.springframework.boot', name: 'spring-boot-gradle-plugin', version: '2.1.7.RELEASE', ext: 'pom'
implementation 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.10.3'
}
dependencyManagement {
imports {
mavenBom("org.springframework.boot:spring-boot-dependencies:2.2.6.RELEASE")
}
}
}
project-sub-a:build.gradle:
plugins {
id 'org.springframework.boot' version '2.2.6.RELEASE'
}
apply plugin: 'org.springframework.boot'
dependencies {
implementation project(':connection-service-common')
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
implementation 'org.springframework.boot:spring-boot-starter-web'
}
After ./gradlew build i get the following errors:
> Task :project-sub-b:compileJava
.../Pojo.java: error: package com.fasterxml.jackson.annotation does not exist
import com.fasterxml.jackson.annotation.JsonProperty;
and more ...
Does anybody know how to solve this issue? Thanks.
Looks like your are missing:
'com.fasterxml.jackson.core:jackson-annotations:2.10.3'
which (and a few other jackson jars) is a transitive dependency of
'org.springframework.boot:spring-boot-starter-web'
That is why project a does not complain. Because of that there is no need to put jackson in subprojects. Subproject a is getting it anyway.
So I have the following project structure:
.
├── Central
│ ├── app
│ │ ├── build.gradle
│ │ └── src
│ ├── build.gradle
│ ├── gradle.properties
│ └── settings.gradle
└── Client
├── app
│ ├── build.gradle
│ └── src
├── build.gradle
├── gradle.properties
└── settings.gradle
In the Central app, I define a service and along with it an AIDL interface. In the AIDL, one of the functions returns a custom object (which extends Parcelable). In the Client app, I put the exact same AIDL file (under the same package in the src/aidl directory). I try to import the custom class by declaring a gradle dependency to the Central app.
Here is the Client's settings.gralde
rootProject.name='Client'
include ':app'
include ":Central"
project(":Central").projectDir = file('../Central/app')
The Client's app/build.gralde:
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.Patel.Cli3n5"
minSdkVersion 28
targetSdkVersion 29
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation project(path: ':Central', configuration: 'default')
}
And the aidl file (present in both apps):
package com.Patel.Central;
import android.graphics.Bitmap;
parcelable Info;
interface MusicCentral {
List<Info> getAllSongsInfo();
Bitmap getSongImage(int songNum);
}
And note that the Info class is defined in the package com.Patel.Central.
When I try to build the Client app, I get the following error:
error: cannot find symbol
#Override public java.util.List<com.Patel.Central.Info> getAllSongsInfo() throws android.os.RemoteException
In summary, the problem is that there is a custom class and I need to import from an app that is in another directory.
You need to define both files
Info.java
and
Info.aidl seprate aidl file for Info.java
with same package name at both client and remote application.
means package should have
MusicCentral.aidl
Info.java
Info.aidl
in com.Patel.Central package.
I have a project structure as follows:
└── src
├── main
│ ├── java
│ │ ├── MessageServlet.java
│ └── webapp
│ ├── META-INF
│ │ └── context.xml
│ ├── WEB-INF
│ │ └── web.xml
│ ├── index.html
│ └── style.css
└── test
└── java
I also have a tomcat server which I run using the gradle below. The tomcat server starts and displays the index.html page. But, when I try to run the servlet i get following error message:
HTTP Status 500 - Error instantiating servlet class MessageServlet
build.gradle
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.bmuschko:gradle-tomcat-plugin:2.2.4'
}
}
apply plugin: 'com.bmuschko.tomcat'
apply plugin: 'war'
apply plugin: 'idea'
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile 'org.mongodb:mongo-java-driver:3.3.0'
compile 'com.google.code.gson:gson:2.2.4'
def tomcatVersion = '8.0.42'
tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
"org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}",
"org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}"
}
QUESTION
I want to run the project on my tomcat server via this gradle. How do I make the gradle execute MessageServlet.java together with tomcat?
I'm trying to use application.properties to bean datasource but it seems that spring boot does not find the file or something like.
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
Here my structure:
.
├── build.gradle
└── src
└── main
├── java
│ └── com
│ └── companies
│ ├── CompanyApplication.java
│ ├── config
│ │ └── WebMvcConfig.java
│ ├── controller
│ │ └── HelloWorldController.java
│ └── model
│ ├── Article.java
│ ├── daoInterface
│ │ └── ArticleDaoInterface.java
│ ├── daoTemplates
│ │ └── ArticleDao.java
│ └── mappers
│ └── ArticleMapper.java
├── resources
│ └── application.properties
└── webapp
└── WEB-INF
└── pages
└── hello.jsp
I've try to move application.properties file from resources to config and nothing.
application.properties:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/name
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
build.gradle
buildscript {
repositories {
//Required repos
mavenCentral()
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
}
dependencies {
//Required dependency for spring-boot plugin
classpath "org.springframework.boot:spring-boot-gradle-plugin:1.2.6.RELEASE"
}
}
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'spring-boot'
jar {
baseName = 'companies'
version = '0.2'
}
war {
baseName = 'companies'
version = '0.1'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-web'
compile("org.springframework.boot:spring-boot-starter")
compile("org.springframework:spring-jdbc")
compile('org.springframework.boot:spring-boot-starter-jdbc:1.2.6.RELEASE')
testCompile("junit:junit")
//Required dependency for JSP
compile 'org.apache.tomcat.embed:tomcat-embed-jasper'
}
And where I'm trying to autowire the dataSource:
package com.companies.model.daoTemplates;
import com.companies.model.Article;
import com.companies.model.daoInterface.ArticleDaoInterface;
import com.companies.model.mappers.ArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import javax.sql.DataSource;
import java.util.List;
#Repository
public class ArticleDao implements ArticleDaoInterface {
private JdbcTemplate jdbcTemplateObject;
private final String DB_NAME = "articles";
#Override
#Autowired
public void setDataSource(DataSource ds) {
this.jdbcTemplateObject = new JdbcTemplate(ds);
}
#Override
public List<Article> listArticle() {
String SQL = "select * from " + DB_NAME + " where inactive = false ORDER BY name";
List <Article> article = jdbcTemplateObject.query(SQL,
new ArticleMapper());
return article;
}
}
CompanyApplication.java
package com.companies;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
#Configuration
#ComponentScan
#EnableAutoConfiguration
public class CompanyApplication {
public static void main(String[] args) {
SpringApplication.run(CompanyApplication.class, args);
}
}
I cannot find where I'm failing at.
As #M. Deinum mentioned in his comment it seems to be a dependency configuration problem. You need a dependency on spring-jdbc for an embedded database to be auto-configured.
Please make sure you've followed on the documentation
You should also check out this spring-boot-jdb sample
Spring boot is mostly based on the principle than putting a specific jar in the classpath will trigger the activation of the related functionality. Spring boot is scanning the classpath at startup and will start "everything he found" except if you disable it by using annotation.
So to have Spring Boot initializing a DataSource you must have one of the following dependencies:
- spring-boot-starter-jdbc : will allow to use the DataSource and JDBC stuff.
- spring-boot-starter-data-jpa : will load the JPA and so the DataSource as a sub-module
I got a similar error and I solved it by adding the following dependency
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.5.0</version>
</dependency>
This should be the correct declaration of your runner class:
package com.companies;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class CompanyApplication {
public static void main(String[] args) {
System.exit(SpringApplication.exit(
SpringApplication.run(CompanyApplication.class, args)));
}
}
Amidst other things, it will auto-initialize your DataSource from the application.properties.
EDIT: in your application.properties you should have entries similar to these, which are specific for an Oracle DataSource:
spring.datasource.url=jdbc:oracle:thin:#<hostaddr>:<port>:<instance_name>
spring.datasource.username=<username>
spring.datasource.password=<password>
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
I've faced this problem and figured out that implementation of DataSource is placed in Tomcat libs. So for 8th tomcat you going to include org.apache.tomcat.jdbc.pool.DataSource class that placed in org.apache.tomcat:tomcat-jdbc:jar:8.0.36
I had the same problem. In my case I solved it by adding the dependency for the mysql java connector.