springboot 获取maven打包时间
pom
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.13.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!--指定时间格式 -->
<maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>
<!--maven.build.timestamp保存了maven编译时间戳 -->
<timestamp>${maven.build.timestamp}</timestamp>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources/</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
方式一
maven.properties
# maven打包时间
maven.package_time=@timestamp@
MavenProperties
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import lombok.Data;
@Configuration
@ConfigurationProperties(prefix = "maven", ignoreUnknownFields = false)
@PropertySource(value= "classpath:config/maven.properties",encoding = "utf-8")
@Data
@Component
public class MavenProperties {
/**maven打包时间*/
private String package_time;
}
测试
@Resource
private MavenProperties mavenProperties;
@GetMapping("/mavenTime")
public String ah() {
return "最新打包时间:"+modifyTime(packageTime);
}
/**
* 修改时间为东8区
*/
public String modifyTime(String date) {
Date oldDate=null;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
oldDate = simpleDateFormat.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(oldDate);
calendar.add(Calendar.HOUR_OF_DAY, +8);
return simpleDateFormat.format(calendar.getTime());
}
方式二
application.yml
maven:
package_time: '@timestamp@' # maven打包时间
测试
@Value("${maven.package_time}")
private String packageTime;
@GetMapping("/mavenTime")
public String ah() {
return "最新打包时间:"+modifyTime(packageTime);
}
/**
* 修改时间为东8区
*/
public String modifyTime(String date) {
Date oldDate=null;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
oldDate = simpleDateFormat.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(oldDate);
calendar.add(Calendar.HOUR_OF_DAY, +8);
return simpleDateFormat.format(calendar.getTime());
}