从 1.2.8.RELEASE 升级到 1.3.8.RELEASE 后无法读取 settings.xml
Posted
技术标签:
【中文标题】从 1.2.8.RELEASE 升级到 1.3.8.RELEASE 后无法读取 settings.xml【英文标题】:Cannot read settings.xml after spring upgrade from 1.2.8.RELEASE to 1.3.8.RELEASE 【发布时间】:2021-06-10 04:38:53 【问题描述】:在将 Spring 父 POM 应用程序从 1.2.8.RELEASE 升级到 1.3.8.RELEASE 后,我注意到 settings.xml 中定义的属性无法读取。 spring 是否不支持从高于 1.2.8.RELEASE 的版本中读取 settings.xml。 代码在 GIT 上可用:https://github.com/Ravivyas1989/demo
这是我的问题 POM.xml 的代码 sn-p 示例:
<?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>
这里是 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);
这里是 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
【问题讨论】:
我建议升级到最新版本(2.4.3 或者可能升级到 2.5.0-M2),因为 1.5.X 线自 2018 年 8 月起停产,我不知道 1.3 需要多久。 X线是生命终结... 我跳到了2.4.3,这个问题在更高的版本中也存在,而且我不想一次性进行这么高的升级,因为它会使代码不稳定。 您的代码因升级而变得不稳定?嗯。你有单元测试和集成测试吗?除此之外,不应在settings.xml
中定义属性,我强烈建议您使用不需要这些东西的存储库管理器:<url>file:$project.basedir/src/main/resources/repo/</url>
...
是的,只是从 1.2.8.RELEAE 升级到 1.3.8.RELEASE 会导致问题。如您所见,它只是一个演示应用程序,我没有为它编写任何测试用例(但实际应用程序确实有 J 单元测试用例,与此问题无关)。我知道属性应该在 application.properties 中定义,但在我们当前的设置中它们就是这样做的。
我再说一遍,但我真的会假设settings.xml
已被实际读取,但信息在其他地方丢失了。
【参考方案1】:
“请注意 application.properties 文件中的 @property@ 而不是 $property.。
spring-boot-starter-parent pom 将标准 $ 分隔符重新定义为 @:"
我的问题在这里得到解答:
Cannot get maven project.version property in a Spring application with @Value
【讨论】:
【参考方案2】:Maven 总是从settings.xml
中读取属性。一定有其他错误。
【讨论】:
以上是关于从 1.2.8.RELEASE 升级到 1.3.8.RELEASE 后无法读取 settings.xml的主要内容,如果未能解决你的问题,请参考以下文章