【问题标题】:Spring boot properties vs maven pom packaging tagSpring Boot 属性 vs maven pom 打包标签
【发布时间】:2019-08-18 00:08:24
【问题描述】:

我在使用 spring boot 和 maven 时遇到了一些问题。

似乎<packaging>pom</packaging> 标签添加到pom.xml 时,不知何故让spring 完全不知道父级applications.properties 配置文件。运行时,无论声明的属性如何,spring 仍然会显示横幅和信息级别的日志记录。为什么会这样?有没有办法将这些属性添加到我的父级,以便所有模块都可以在给定的配置下运行?这会成为反模式吗?

主类:

package com.example.app; //could also be inside the app-client module

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

application.properties:

spring.main.banner-mode=off
logging.level.root=info

(父)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>
    <packaging>pom</packaging>
    <modules>
        <module>app-client</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example.</groupId>
    <artifactId>app</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>app</name>
    <description>Demo app</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

</project>


【问题讨论】:

  • 你为什么将&lt;packaging&gt;pom&lt;/packaging&gt;添加到一个看起来会生成包含基于Spring Boot的应用程序的jar文件的模块中?
  • 该项目已初具规模,因此这是第一次尝试向其中添加许多未来模块中的一个。想法是有一个 pom 包装父模块和 jar 包装子模块。

标签: java maven spring-boot pom.xml spring-properties


【解决方案1】:

完全同意安迪对您的问题的评论我想指出,如果您想将工件表示为某种模块容器是基础项目,则使用 packaging 类型 pom 是合适的你为你的(子)模块收集依赖信息。

但是,在您的案例中,@SpringBootApplication 向我们表明您希望使用您的工件来运行业务代码。另一方面,这会导致packaging 类型如jarwar。将其切换到其中之一,一切都应该运行良好。

更多信息请咨询Maven reference for packaging

【讨论】:

  • 做一些研究,似乎我的 Maven 模块文件夹结构也很糟糕。我保留了父级的 pom 包装,添加了一些子(或子)模块,将 Mainapplication.properties 移动到其中一个,现在这些属性又可以工作了。子模块内的 Jar 包装和父模块的 pom 包装对我有用。
  • @EvandroTeixeira 我已经构建了相同的结构(父-> pom,子模块-> jar),但是我收到了与数据源相关的错误,这意味着 application.yml 没有加载?跨度>
  • @aleksaRazer22 我已经很久没有接触那个项目了。我只记得父 pom 没有直接关联的源代码,只有模块,每个模块都有自己的源代码和pom.xml,包括 Main 所在的那个。我的经验法则是查看任何流行的基于 java/kotlin maven 的 spring 项目并复制它们的 pom 结构。
猜你喜欢
  • 2015-02-02
  • 2014-11-05
  • 2020-07-08
  • 2015-11-07
  • 1970-01-01
  • 2021-08-19
  • 2011-04-10
  • 2015-01-27
  • 2016-01-29
相关资源
最近更新 更多