Access parent's application.properties from dependent project in spring boot - java

I am developing a multi-module project in Spring Boot where the project structure is as follows:
com.app.parent <- parent pom with version numbers and common dependencies (POM)
com.app.core <- repository and service layer, models, DTOs (JAR)
com.app.rest <- rest API (WAR)
com.app.soap <- soap API (WAR)
The pom.xml file for the parent project is:
<artifactId>app-parent</artifactId>
<packaging>pom</packaging>
<name>app-parent</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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 file for the core project is:
<artifactId>app-core</artifactId>
<packaging>jar</packaging>
<name>app-core</name>
<parent>
<groupId>com.app</groupId>
<artifactId>app-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../app-parent/pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
The pom.xml for the rest project is:
<artifactId>app-rest</artifactId>
<packaging>war</packaging>
<name>app-rest</name>
<parent>
<groupId>com.app</groupId>
<artifactId>app-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../app-parent/pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.app</groupId>
<artifactId>app-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
The entry point for the app-core project is:
#SpringBootApplication
public class CoreApplication {
public static void main(String[] args) {
SpringApplication.run(CoreApplication.class, args);
}
}
The entry point for the app-rest project looks exactly the same.
I have created an application.properties file within core/src/main/resources/ which contains database config etc. and I can compile/test the app-core project just fine. However, when I try to run or test the app-rest project I get errors related to the absence of application.properties file
Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath.
How do I get the child project to load the parent's application.properties file? Do I have to create a symlink to the parent file, or do I expressly load the parent's property file via #PropertySource? What do others do in this scenario?

Create another module for configurations (let's say config) and put your application.properties in config/src/main/resources/.
Add the config module as dependency to any module which use the same configs and there you go.

Related

No Tests Were Executed when using JUnit5 and Spring-boot-starter-parent

I have a very simple JUnit test class that I can not run unless I remove parent spring-boot-starter-parent from the pom, which wouldn't be a possibility for our production application. The error we get on is No Tests Were Executed and below is the mvp with the parent piece that whenever not commented out would block the tests. If I can get any guidance to know how to fix this please.
<?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>
<!-- Piece to be disabled for tests to run -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hmhco</groupId>
<artifactId>crs-v2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>crs-v2</name>
<description>Content Recommendation Service V2</description>
<properties>
<java.version>1.8</java.version>
<junit.jupiter.version>5.6.2</junit.jupiter.version>
<maven.surefire.version>3.0.0-M5</maven.surefire.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.version}</version>
</plugin>
</plugins>
</build>
import org.junit.jupiter.api.Test;
public class TestingTest {
#Test
public void checking() {
System.out.println("checking------------");
}
}
Thank you.
The problem is related to that you trying to use a different version of junit jupiter. The version which is predefined by spring-boot-parent (2.2.4.RELEASE) is 5.5.2. The simplest solution is to remove the versions of junit-jupiter parts (version tag) and use the one which is inherited from spring-boot-parent. The best recommendation I can give is to start with a newer version of Spring Boot (2.3.3.RELEASE most recent one) which uses a more recent version.
If you can't go that way you have to use the junit-bom file instead like this:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.6.2</version>
<scope>import</scope>
<type>pom</type>
</dependency>
Then it should work fine. Afterwards you have to use the dependencies without defining the version tag anymore in your pom.xml file.
Adding -DfailIfNoTests=false to the mvn command seems to make it work.

error:java package xxx does not exist despite IDEA being aware of them

I'm leanring springboot with some source code.Recent days,when I begin learning new lessons and want to import the example project I found I fail to import all the packages when I open the example project. Although previous example projects works fine.
It's especially strange that the editor actually can identify those packages - it even offered me the appropriate classes when I manually deleted the import statements, I can see the library in the Project tree under External Libraries,I even can skip to see where the package is through ctrl+click. However, I always get a list of "java: package ... does not exist" upon compilation.How should I solve the problem?Thanks!!
I hava tried following solutions:
reimport in maven settings
invalidate and restart
3.check pom.xml
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.neo</groupId>
<artifactId>spring-boot-file-upload</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
some of the wrong import
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
error messages while compiling:
fail to import all the packages
therefore can't resolve all the related symbols
Your project is missing some dependencies. You are not only using Spring Boot, but also Spring Web and Spring MVC.
Try adding this to your pom.xml:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
You may want to adapt depending on the Spring version you are using.

EnableConfigServer is not working for native location in spring boot

I am trying to configure a configuration server for all the properties in our application using #EnableConfigServer in spring boot. Please see the code below :
#EnableConfigServer
#SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cdk.config</groupId>
<artifactId>configserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>configserver</name>
<description>Contains all the configurations/properties required by all the services</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
spring.application.name=config-server
server.port=9090
spring.cloud.config.server.native.searchLocations=file://Users/Sankest/StarterProjects/MicroServices/AllConfigurations/
spring.profiles.active=native
Copied all the property files to : /Users/Sankest/StarterProjects/MicroServices/AllConfigurations/
But when I try to access url at http://localhost:9090/config-server/default I am not seeing any property files and getting the following response:
{"name":"config-server","profiles":["default"],"label":null,"version":null,"state":null,"propertySources":[]}
Add dependencies
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
Correct value should be spring.cloud.config.server.native.searchLocations=file:///Users/Sankest/StarterProjects/MicroServices/AllConfigurations/ with 3 front slashes after file:. One way to verify whether path is correct or not, even without running the application, is to paste the path in browser and check whether it shows all the files.
For default profile make sure either file name is application.yml or application.properties.
For other profiles e.g. dev, file name should be application-dev.yml or application-dev.properties (if all are in the same folder), then http://localhost:9090/config-server/dev would show both dev and default profile entries.
I don't think so you are accessing the correct endpoint.
For example :
If you are having three files in your AllConfigurations folder.
The file can be yml or properties.
application-dev.yml
application-test.yml
application-prod.yml
The name in the left side of the - is application name and name in right side is profile.
So the endpoint for application-dev.yml will be
 http://localhost:9090/application/dev
From the client to access the specific profile file in the config-server you need to set the active profile.
If you client application name is test
spring.profile.active=dev
Then it will return test-dev file to your client from config server.

Make a maven dependency provided based on active spring profile

So I am building a spring boot web application, packaging as a war, and deploying to a tomcat application server.
I have the following dependency in my pom.xml for tomcat:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
The scope of this dependency needs to be provided in order to be able to deploy it to a tomcat instance. However, when I want to run the war via the spring boot CLI or via IntelliJ's default spring boot run configuration, I need to remove the <scope>provided</scope> in order for it to run the embedded tomcat.
My question is, is there some way to make the dependency conditionally provided based on an active spring profile, or some other method?
In your specific case, you can just do :
In the dependencies to run spring-boot with embedded tomcat :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
And in a profile to deploy under tomcat
<profile>
<id>tomcat</id>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>
After, to build for a specific profile
mvn clean install -Ptomcat
You can't control dependencies via spring profiles. However you can control spring profiles by maven profiles and it can solve your problem.
You can declare several maven profiles in your application and provide different set of dependencies for each of them.
Then you can configure maven profiles to use particular spring profile.
Take a look on maven profiles and an example of such configuration in this thread
This is a solution that will work with both, jar and war packaging:
pom.xml
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>...</artifactId>
<groupId>...</groupId>
<version>0-SNAPSHOT</version>
<packaging>${packaging.type}</packaging>
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<start-class>...</start-class>
<packaging.type>jar</packaging.type>
...
</properties>
<dependencies>
<dependency>
<!-- Brings in Embedded Tomcat dependencies -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
...
</dependencies>
<profiles>
<profile>
<id>tomcat-war</id>
<properties>
<packaging.type>war</packaging.type>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
...
</profiles>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
...
Build artifact as jar:
mvn clean package
Build artifact as war:
mvn clean package -Ptomcat-war
Main class, that goes in <start-class> in pom.xml:
package ...
public class Application extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

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