Spring Boot to exclude connecting to database during maven build

Viewed 6881

I have a scenario where I want to deploy the spring boot application in AWS but I just want to ignore the database connections happening internally during build locally as I do not have any test classes and I do not want to include H2 database.Is it not possible to build the jar file to be deployed in AWS without connecting to AWS database?

Application.properties

# ===============================
# = DATA SOURCE
# ===============================

# Set here configurations for the database connection

# Connection url for the database "netgloo_blog"

spring.datasource.url = jdbc:mysql://localhost:3306/auto_journey

# Username and password
spring.datasource.username = root
spring.datasource.password =auto123

# Keep the connection alive if idle for a long time (needed in production)
#spring.datasource.testWhileIdle = true
#spring.datasource.validationQuery = SELECT 1

# ===============================
# = JPA / HIBERNATE
# ===============================

# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager).

# Show or not log for each sql query
spring.jpa.show-sql = true

# Hibernate ddl auto (create, create-drop, update): with "update" the database
# schema will be automatically updated accordingly to java entities found in
# the project
spring.jpa.hibernate.ddl-auto = update

# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

server.servlet.context-path=/autofinance


server.port=9090


spring.mvc.static-path-pattern=/resources/**
6 Answers

Include this in pom.xml to solve the problem.

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
    </plugin>

mvn clean install -DskipTests will work i think

Have similar issue, there is no need to disable all test with this plugin.

Just above main test class comment out or delete annotation:

//@SpringBootTest

Then when Maven build app, it will still run tests inside this class but will not run SpringBoot app, so will not test connection to DB and build will be successful.

I too have a similar requirement, If you have solved this issue, dont hesitate to post the answer here.

If you used Spring Initializr to generate the Spring Boot project, an application test class YourApplicationTest was auto-generated inside the test package. This class was what triggered Maven SureFire to try to bring up your application which would attempt to connect to the database. Just delete this file to stop that test. No need to fully disable unit tests.

I am using Intellij IDEA for my Spring Boot project, and on Maven build.

To build jar package without database connection (in my situation, database server is not accessible from my computer), I add -DskipTests in maven run configuration.

enter image description here

Related