javaspringazuremaven

Get maven to build a java application using the values from the application-production.properties rather than from application.properties


I am trying to deploy a spring application in production mode. When I run the application locally with the profile production and set the -Dspring.profiles.active=production, the application will using my sql database. However, when I build with maven and deploy to azure, the application runs in deployment mode. I have confirmed that it is deployment mode as none of the work is stored on the database unlike when it is ran locally. I can also confirm there is no rollback due to having a version string which I have displayed on the login page to let me know if my code is actually being deployed.

Local - Production: Java 19 with Profile set to production and spring.profiles.active set to production

img1

Local - Dev: Java 19 with Profile set to development

img2

Deploy-Development:

mvn install -Pproduction;
az login;
az containerapp up --resource-group rg-group -n group-app-vaadin --source .;

Deploy-Production:

mvn install -Pproduction -Dspring.profiles.active=production; 
az login;
az containerapp up --resource-group rg-group -n group-app-vaadin --source .;

Docker:

FROM eclipse-temurin:21-jre
COPY target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app.jar"]

application.properties:

server.port=${PORT:8080}
logging.level.org.atmosphere=warn
spring.mustache.check-template-location=false

# Launch the default browser when starting the application in development mode
vaadin.launch-browser=true

# increases max file size the server will accept
spring.servlet.multipart.max-file-size=30MB
spring.servlet.multipart.max-request-size=30MB

# To improve the performance during development.
# For more information https://vaadin.com/docs/latest/integrations/spring/configuration#special-configuration-parameters
vaadin.allowed-packages=com.vaadin,org.vaadin,com.GROUP,de.f0rce.signaturepad
spring.jpa.defer-datasource-initialization=true
spring.jpa.show-sql=true

application-production.properties:

spring.jpa.defer-datasource-initialization=false
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url=jdbc:sqlserver://GROUP.database.windows.net:1433;encrypt=true;trustServerCertificate=false;loginTimeout=30;database=GROUP-db;
spring.datasource.username=USERNAME
spring.datasource.password=PASSWORD

pom.xml without dependecies

    <?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>
    <!-- Project from https://start.vaadin.com/project/ed548501-5760-489f-8284-368a0c13d375 -->
    <groupId>com.group.app</groupId>
    <artifactId>group-webapp</artifactId>
    <name>Group Web App</name>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <java.version>17</java.version>
        <vaadin.version>24.4.6</vaadin.version>
    </properties>

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

    <repositories>
        <!-- The order of definitions matters. Explicitly defining central here to make sure it has the highest priority. -->

        <!-- Main Maven repository -->
        <repository>
            <id>central</id>
            <url>https://repo.maven.apache.org/maven2</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>vaadin-prereleases</id>
            <url>
                https://maven.vaadin.com/vaadin-prereleases/
            </url>
        </repository>
        <!-- Repository used by many Vaadin add-ons -->
        <repository>
            <id>Vaadin Directory</id>
            <url>https://maven.vaadin.com/vaadin-addons</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <!-- The order of definitions matters. Explicitly defining central here to make sure it has the highest priority. -->
        <pluginRepository>
            <id>central</id>
            <url>https://repo.maven.apache.org/maven2</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>vaadin-prereleases</id>
            <url>
                https://maven.vaadin.com/vaadin-prereleases/
            </url>
        </pluginRepository>
    </pluginRepositories>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.vaadin</groupId>
                <artifactId>vaadin-bom</artifactId>
                <version>${vaadin.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>



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

            <plugin>
                <groupId>com.vaadin</groupId>
                <artifactId>vaadin-maven-plugin</artifactId>
                <version>${vaadin.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-frontend</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <!-- Production mode is activated using -Pproduction -->
            <id>production</id>
            <dependencies>
                <!-- Exclude development dependencies from production -->
                <dependency>
                    <groupId>com.vaadin</groupId>
                    <artifactId>vaadin-core</artifactId>
                    <exclusions>
                        <exclusion>
                            <groupId>com.vaadin</groupId>
                            <artifactId>vaadin-dev</artifactId>
                        </exclusion>
                    </exclusions>
                </dependency>
            </dependencies>
            <build>
                <plugins>
                    <plugin>
                        <groupId>com.vaadin</groupId>
                        <artifactId>vaadin-maven-plugin</artifactId>
                        <version>${vaadin.version}</version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>build-frontend</goal>
                                </goals>
                                <phase>compile</phase>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <profile>
            <id>development</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <activatedProperties>development</activatedProperties>
            </properties>
        </profile>
    </profiles>
</project>

First, I tried to debug the build and get maven to display which properties it was loading but couldn't pull it off.

Then I tried to mvn clean and mvn clean -Pproduction in case the build was failing to change the value then attempt another build.

Finally, I simply changed all my application.properties to be exactly the same and made them look what I expected application-production.properties to look like. That still failed to deploy with the values that I see in Local-Production.


Solution

  • The issue was being caused by the fact that my docker file was lacking the correct arguments. I had thought that the *.properties files affected the object at compile time but was elucidated by Jorge Campos that they simply affect the run time.

    Orginal

    FROM eclipse-temurin:21-jre
    COPY target/*.jar app.jar
    EXPOSE 8080
    ENTRYPOINT ["java", "-jar", "/app.jar"]
    

    Jorge Campos Corrected

    FROM eclipse-temurin:21-jre
    COPY target/*.jar app.jar
    EXPOSE 8080
    ENV SPRING_PROFILES_ACTIVE=production
    ENTRYPOINT ["java", "-jar", "/app.jar"]
    

    P.S.

    If you are using Azure, click the url for the specific container to force it to activate so you can see the logs as they are not stored anywhere. This will show you what runtime errors your running into.