Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

[Cause] Talk about the effect to be achieved

My girlfriend recently received the task of setting Maven package to XXX-2021209252204.jar. Because it is a Springboot project, there are many examples online.

To start, use maven’s built-in time plugin. Add the time format Settings directly to the Properties TAB as follows:

<properties>
    <maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
</properties>
Copy the code

Then specify filename in the Build tag.

<build>
    <finalName> ${project.artifactId}-${maven.build.timestamp}</finalName>
</build>
Copy the code

The time zone of Maven’s built-in time component is UTC only, so we had no choice but to seek help.

[Problem] Time zone problems encountered during packaging

This problem is actually a component problem, since the built-in component can not solve the problem, so we use another component to replace it.

Push the buildnubmer-Maven-plugin to her and send the configuration items as follows:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>3.2.0</version>
    <executions>
        <execution>
            <id>timestamp-property</id>
            <goals>
                <goal>timestamp-property</goal>
            </goals>
            <configuration>
                <name>build.time</name>
                <pattern>yyyyMMddHHmm</pattern>
                <locale>zh_CN</locale>
                <timeZone>GMT+8</timeZone>
            </configuration>
        </execution>
    </executions>
</plugin>
Copy the code

I specially checked the version of this plug-in, 3.2.0 is the latest version of the current, use should not have any big problems, after all, is a format time.

Here we also need to manipulate the filename tag content as follows:

<finalName>${project.artifactId}-${build.time}</finalName>
Copy the code

Replace maven.build.timestamp with build.time because the time string declared in the buildnubmer-Maven-plugin component is used.

【 results 】

Very simple. No anger, no breakup, thank goodness.

The purpose of this column is to keep track of the questions that girlfriends ask and how many arguments they get into.