Can’t access Maven properties with Spring

Hello I am new to Maven in general so I’d like to apologise in advance if I get things wrong.

I have a Maven project with spring in which I also access a MySQL database.
often the MySQL database is not accessible so I use a local database for testing, I swap between the two by changing the active profile.

I set up the profiles by putting this in my POM.xml:

<profiles>
        <profile>
            <id>Local</id>
            <dependencies>
                <dependency>
                    <groupId>org.hsqldb</groupId>
                    <artifactId>hsqldb</artifactId>
                    <version>2.3.3</version>
                    <classifier>jdk5</classifier>
                </dependency>
            </dependencies>
            <properties>
                <jdbc.url>jdbc:hsqldb:file:databaseName</jdbc.url>
                <jdbc.username>a</jdbc.username>
                <jdbc.password></jdbc.password>
                <jdbc.driver>org.hsqldb.jdbcDriver</jdbc.driver>
            </properties>
        </profile>
        <profile>
            <id>MySQL</id>
            <dependencies>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>5.1.38</version>
                </dependency>
            </dependencies>
            <properties>
                <jdbc.url>jdbc:mysql://mysql.website.ac.uk:3306</jdbc.url>
                <jdbc.username>user</jdbc.username>
                <jdbc.password>1234</jdbc.password>
                <jdbc.driver>com.mysql.jdbc.Driver</jdbc.driver>
            </properties>
        </profile>
    </profiles>

When I try to access the properties variables through for example ${jdbc.url} they are not converted to the actual values as defined in the profile and I get errors. I suspect it might have something to do with Spring Boot.

Answer

You specified properties in profile, so there is a workaround. Annotations such as @Value("${name}") are referencing to externalized configuration, e.g. from application.yml. You should use this file to load properties you want. @[email protected] loads property from your maven profile.

jdbc:
  url: @[email protected]
  username: @[email protected]
and so on...

Then these properties will be accessible in your code.