1

After upgrading the Spring parent POM application from 1.2.8.RELEASE to 1.3.8.RELEASE I noticed that properties defined in settings.xml could not be read. Does spring does not support reading from settings.xml in versions higher than 1.2.8.RELEASE. Code is available on GIT at : https://github.com/Ravivyas1989/demo

Here is the example of code snippet for my problem 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 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>1.2.8.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
    <java.version>1.8</java.version>
</properties>


<repositories>
    <repository>
        <id>spring-release</id>
        <name>Spring Releases</name>
        <url>https://repo.spring.io/libs-release</url>
    </repository>

    <repository>
        <id>atlassian-public</id>
        <url>https://maven.atlassian.com/repository/public</url>
        <snapshots>
            <enabled>true</enabled>
            <updatePolicy>never</updatePolicy>
            <checksumPolicy>warn</checksumPolicy>
        </snapshots>
        <releases>
            <enabled>true</enabled>
            <checksumPolicy>warn</checksumPolicy>
        </releases>
    </repository>

    <repository>
        <id>jr-ce-snapshots</id>
        <name>JasperReports CE Snapshots</name>
        <url>http://jaspersoft.jfrog.io/jaspersoft/jr-ce-snapshots</url>
    </repository>

    <repository>
        <id>redshift</id>
        <url>http://redshift-maven-repository.s3-website-us-east-1.amazonaws.com/release</url>
    </repository>

    <repository>
        <id>project-repo</id>
        <url>file:${project.basedir}/src/main/resources/repo/</url>
    </repository>

    <repository>
        <id>jcenter-release</id>
        <name>jcenter</name>
        <url>http://jasperreports.sourceforge.net/maven2/</url>
    </repository>

    <!-- bouncy-gpg Bouncy Castle with GPG -->
    <repository>
        <id>bintray</id>
        <name>bintray</name>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
        <url>http://jcenter.bintray.com</url>
    </repository>

</repositories>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.18</version>
    </dependency>

    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>



    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    
    <dependency>
       <groupId>javax.xml.bind</groupId>
       <artifactId>jaxb-api</artifactId>
       <version>2.3.1</version>
    </dependency>
</dependencies>

<build>
    <finalName>api</finalName>
    <plugins>


        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <configuration>
                <!-- Always download and attach dependencies source code -->
                <downloadSources>true</downloadSources>
                <downloadJavadocs>true</downloadJavadocs>
                <!-- To create project run: mvn eclipse:eclipse -->
                <wtpversion>2.0</wtpversion>
            </configuration>
        </plugin>



      <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.2</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <!-- specify UTF-8, ISO-8859-1 or any other file encoding -->
                <encoding>${project.build.sourceEncoding}</encoding>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <argLine>-Duser.timezone=US/Eastern ${argLine}</argLine>
                <!--<systemProperties>
                    <property>
                        <name>createJsonFiles</name>
                        <value>${createJsonFiles}</value>
                    </property>
                </systemProperties>-->

            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.19</version><!--$NO-MVN-MAN-VER$ -->
            <configuration>
                <parallel>methods</parallel>
                <useUnlimitedThreads>true</useUnlimitedThreads>
                <forkCount>1</forkCount>
                <argLine>-Duser.timezone=US/Eastern</argLine>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.0</version>
            <configuration>
                <excludes>
                    <exclude>
                        **/reporting-functions/**
                    </exclude>
                </excludes>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>report</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>

        <resource>
            <directory>src/main/reportTemplates</directory>
            <filtering>false</filtering>
            <excludes>
                <exclude>**/~$*.xlsx</exclude>
            </excludes>
        </resource>
    </resources>

</build>

Here is DemoApplication.java

package com.example.demo;

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

@SpringBootApplication
public class DemoApplication {

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

}

Here is settings.xml:

<settings>
<servers>

    <server>
        <id>snapshots</id>
        <username>${NEXUS_USERNAME}</username>
        <password>${NEXUS_PASSWORD}</password>
    </server>

    <server>
        <id>releases</id>
        <username>${NEXUS_USERNAME}</username>
        <password>${NEXUS_PASSWORD}</password>
    </server>

    <server>
        <id>configs</id>
        <username>${NEXUS_USERNAME}</username>
        <password>${NEXUS_PASSWORD}</password>
    </server>

</servers>

<profiles>

    <profile>
        <id>d02</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>

            <!-- General Database Properties -->
            <db.userName>appservers</db.userName>
            <db.password>appservers</db.password>

        </properties>
    </profile>
</profiles>

Testcontroller.java

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

 @Autowired
 private Environment env;

    @RequestMapping(method = RequestMethod.GET, value = "${db.user}")
    public String testing1() {
    System.out.println("db.user is "+env.getProperty("db.user"));
    return "hostName";
    
    }
}

application.properties

db.user=${db.userName}
platform=testlab
server.port=9090
Ravi Vyas
  • 410
  • 6
  • 12
  • I would suggest to upgrade to most recent versions (2.4.3 or maybe to 2.5.0-M2) because 1.5.X line End of Life since August 2018 I don't know how long 1.3.X line is End Of Life... – khmarbaise Mar 12 '21 at 08:15
  • I jumped to 2.4.3 and this problem persists in higher versions too, Also i dont want to make such a higher upgrade in one go because it will make the code unstable. – Ravi Vyas Mar 12 '21 at 09:49
  • Your code become unstable based on an upgrade? Hm. Do you have unit- and integration tests? Apart from that properties shouldn't be defined in `settings.xml` also I strongly recommend to use a repository manager that you don't need such things: `file:${project.basedir}/src/main/resources/repo/` ... – khmarbaise Mar 12 '21 at 10:15
  • Yes just upgrading from 1.2.8.RELEAE to 1.3.8.RELEASE is causing the issue. As you can see it is just a demo application and I did not write any test cases for it (but actual application does have J unit test cases which is irrespective of this issue). I know that properties should be defined in application.properties but in our current set up thats how they are doing it. – Ravi Vyas Mar 12 '21 at 10:22
  • I repeat myself, but I would really start with the assumption that the `settings.xml` is actually read, but the information is lost somewhere else. – J Fabian Meier Mar 17 '21 at 13:10

2 Answers2

0

"Notice the @property@ instead of ${property}. in the application.properties file.

The spring-boot-starter-parent pom redefines the standard ${} delimiter as @:"

My question is answered here:

Cannot get maven project.version property in a Spring application with @Value

Ravi Vyas
  • 410
  • 6
  • 12
-2

Maven always reads the properties from the settings.xml. There must be some other error.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142