8

I have a simple main app:

// Application.java
package com.my.application;

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

@SpringBootApplication(scanBasePackages = "com.my")
public class Application
{
    public static void main(String[] args)
    {
        SpringApplication.run(Application.class, args);
    }
}

Just a class:

// TestClass.java
package com.my.application;

public class TestClass
{
    public TestClass()
    {

    }
}

With config:

//ApplicationConfiguration
package com.my.configuration;

import com.my.application.TestClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@Configuration
public class ApplicationConfiguration
{
    @Autowired
    Environment env;

    @Bean TestClass getTestClass()
    {
        System.out.println(env.getProperty("test"));

        return new TestClass();
    }
}

this is my pom file:

// 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>com.my</groupId>
    <artifactId>test</artifactId>
    <version>1.3.0</version>
    <packaging>pom</packaging>
    <name>test</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <version>1.23</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

the application.yml file is just:

// src/main/resources/application.yml

test: 123

Property "test" always = null. What I was wrong? Tried with @Value, @ConfigurationProperties, @EnableConfigurationProperties, @PropertySource("classpath:src/main/resources/application.yml") annotations, without the snakeyaml library, with another spring-boot versions, but result always the same.

4 Answers 4

10

It has to do with Maven - application.properties is not part of the build when you're using <packaging>pom</packaging> in your pom file - hence when you start the Application, the file is not there to be read.

Remove <packaging>pom</packaging> from your pom and you should be good to go.

Sign up to request clarification or add additional context in comments.

1 Comment

my app is modular, I need my parent packaging to be pom. Is there a way to use module yaml, since my module packaging is jar?
5

I also faced the same issue. Tried lot of things, but it nothing worked.

In the end, when I added below dependency in pom, my application is able to read application.yml file.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-configuration-processor</artifactId>
</dependency>

Comments

3

try adding dependency org.springframework.boot:spring-boot-configuration-processor

Comments

0

In my case, I fixed it editing pom.xml and including the file in the resources directory.

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>*.properties</include>
                    <!-- Include application.yml -->
                    <include>application.yml</include>
                    <include>public/**</include>
                    <include>templates/**</include>
                </includes>
            </resource>
        </resources>   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.