0

I have a spring boot project (Project A) created which do contains commonly used methods and I have hosted this project in GitHub deployed artifacts and used it as a dependency in other projects (Project B, C ..) to reuse those methods. I have one common method created in Project A which does read the value from the application properties file which works fine locally but when I deploy the artifacts to GitHub and use it as a dependency it is not able to read the value from an application properties file and read as null. Maybe I am not including the properties files while deploying artifacts for Project A correctly.

pom.xml (Project A)

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.abc</groupId>
    <artifactId>api-authenticator</artifactId>
    <version>1.0.2</version>
    <name>API Authenticator</name>
    <description>Project to authenticate API usage</description>

    <distributionManagement>
        <repository>
            <id>internal.repo</id>
            <name>Staging Repository</name>
            <url>file://${project.build.directory}/${version}</url>
        </repository>
    </distributionManagement>

    <properties>
        <github.global.server>github</github.global.server>
        <java.version>11</java.version>
        <java-jwt.version>3.16.0</java-jwt.version>
        <jwks-rsa.version>0.18.0</jwks-rsa.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>${java-jwt.version}</version>
        </dependency>
        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>jwks-rsa</artifactId>
            <version>${jwks-rsa.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>resources</directory>
                <targetPath>src/main/resources</targetPath>
                <includes>
                    <include>**/**</include>
                </includes>
            </resource>
        </resources>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>11</source>
                        <target>11</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>com.github.github</groupId>
                <artifactId>site-maven-plugin</artifactId>
                <version>0.12</version>
                <configuration>
                    <message>Maven artifacts for ${project.version}</message>
                    <noJekyll>true</noJekyll>
                    <outputDirectory>${project.build.directory}</outputDirectory>
                    <branch>refs/heads/${version}</branch>
                    <includes>
                        <include>**/*</include>
                    </includes>
                    <merge>true</merge>
                    <repositoryName>api-authenticator</repositoryName>
                    <repositoryOwner>me</repositoryOwner>
                    <server>github</server>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>site</goal>
                        </goals>
                        <phase>deploy</phase>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.2</version>
                <configuration>
                    <files>
                        <file>
                            ${project.basedir}/src/main/resources/application.properties
                        </file>
                    </files>
                    <altDeploymentRepository>
                        internal.repo::default::file://${project.build.directory}/${version}
                    </altDeploymentRepository>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
Romil Patel
  • 12,879
  • 7
  • 47
  • 76
  • Remove the `maven-deploy-plugin` configuration.. The application properties will be packaged into the resulting jar while using spring-boot-maven-plugin... – khmarbaise Apr 11 '22 at 07:21
  • files are included automatically when in the proper location. property files should be in `src/main/resources` not sure where you are putting them. – M. Deinum Apr 11 '22 at 10:12
  • Hello @khmarbaise, If I remove `maven-deploy-plugin` and deploy and use it as a dependency and do return the same null value when I read the value from the application properties file of project A. If I add `spring-boot-maven-plugin` I am not able to resolve classes of Project A in other projects as discussed here (https://stackoverflow.com/q/71749753/10961238). Can you please suggest on it? – Romil Patel Apr 11 '22 at 10:25
  • Hello @M.Deinum. Yes I am placing the application properties file in `src/main/resources` in Project A and if I execute a test of Project A I am correctly getting the value but when I deploy the artifact of project A and use it as a dependency in project B I do get `null` value in the method which is written in project A which do read the value from the application properties file. Can you please guide on it? – Romil Patel Apr 11 '22 at 10:28
  • Only 1 `application.properties` will be read. So if project B has an `application.properties` as well only that will be read. – M. Deinum Apr 11 '22 at 11:12
  • Should not ideally happen , but check if this helps , Try to deploy the project with full resources and dependencies like the answer in following article https://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven – Harsh Apr 11 '22 at 11:13
  • @M.Deinum. Yes, project B do have its `application.properties` file. Is there any chance to load both `application.properties` file from project A and B? Can we use rename application.properties file and use spring profiles? – Romil Patel Apr 11 '22 at 11:27
  • That would be abusing profiles to achieve something. If you want to provide defaults why not hardcode them into your configuration instead of the step of putting them in a properties file. You can still override them from Project B if needed. – M. Deinum Apr 11 '22 at 11:33

0 Answers0