java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot maven打包时间

SpringBoot获取maven打包时间的两种方式

作者:指尖‖舞者

这篇文章主要介绍了SpringBoot获取maven打包时间的两种方式,文章通过代码示例给大家讲解的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下

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());
    }

到此这篇关于SpringBoot获取maven打包时间的两种方式的文章就介绍到这了,更多相关SpringBoot maven打包时间内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文