【问题标题】:Spring Boot to exclude connecting to database during maven buildSpring Boot 在 Maven 构建期间排除连接到数据库
【发布时间】:2021-06-17 12:18:35
【问题描述】:

我有一个场景,我想在 AWS 中部署 Spring Boot 应用程序,但我只想忽略在本地构建期间内部发生的数据库连接,因为我没有任何测试类并且我不想包含 H2 数据库。不连接AWS数据库就不能构建jar文件部署到AWS吗?

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/**

【问题讨论】:

    标签: spring-boot


    【解决方案1】:

    在 pom.xml 中包含这个来解决问题。

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

    【讨论】:

    • 这会跳过所有测试吗?
    【解决方案2】:

    mvn clean install -DskipTests 我认为会起作用

    【讨论】:

      【解决方案3】:

      有类似的问题,没有必要禁用这个插件的所有测试。

      就在主测试类的上方注释掉或删除注解:

      //@SpringBootTest
      

      那么当 Maven 构建应用程序时,它仍然会在这个类中运行测试,但不会不运行 SpringBoot 应用程序,因此不会测试与 DB 的连接并且构建会成功。

      【讨论】:

        【解决方案4】:

        我也有类似的需求,如果你解决了这个问题,不要犹豫在这里发布答案。

        【讨论】:

          【解决方案5】:

          如果您使用Spring Initializr 生成Spring Boot 项目,则会在测试包中自动生成应用程序测试类YourApplicationTest。此类是触发 Maven SureFire 尝试启动您的应用程序的原因,该应用程序将尝试连接到数据库。只需删除此文件即可停止该测试。无需完全禁用单元测试。

          【讨论】:

            【解决方案6】:

            我在我的 Spring Boot 项目和 Maven 构建中使用 Intellij IDEA。

            要在没有数据库连接的情况下构建 jar 包(在我的情况下,我的计算机无法访问数据库服务器),我在 maven 运行配置中添加了-DskipTests。

            【讨论】:

              猜你喜欢
              • 2018-10-10
              • 2021-07-14
              • 1970-01-01
              • 2017-11-09
              • 2019-01-13
              • 1970-01-01
              • 1970-01-01
              • 2018-09-23
              • 2018-11-23
              相关资源
              最近更新 更多