Using maven to output the version number to a text

2019-01-08 11:00发布

I want to generate an zip file that will update an application with maven. The zip will be hosted on a server and I am using the assembly plugin to generate the zip. However I would like maven to automatically generate a text file that stores the current version number outside the zip. Is this possible?

EDIT: I successfully did it using the maven Assembly Plugin and two descriptor to create two custom assemblies. One has a directory-single goal and it just creates a folder with the updated version.txt based on filtering. Then another one with a single goal actually packages the zip file. This seems to be very inelegant and I guess it will not properly up date the maven repo with the whole updated folder. If there is a better way to do this please let me know.

10条回答
Bombasti
2楼-- · 2019-01-08 11:01

Use standard META-INF\MANIFEST.MF (Then you can use Java code getClass().getPackage().getImplementationVersion() to get version)

For .war use this configuration:

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1</version>
    <configuration>
        <archive>                   
            <manifest>
                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
            </manifest>
        </archive>
    </configuration>
</plugin>

That will add manifest during build, or you can call mvn war:manifest

See also How to get package version at running Tomcat?

查看更多
Rolldiameter
3楼-- · 2019-01-08 11:04

For a Spring Boot application, follow the accepted answer from above however substituting

${project.version}

with

@project.version@

Here's the link to the documentation https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-1.3-Release-Notes#maven-resources-filtering

查看更多
别忘想泡老子
4楼-- · 2019-01-08 11:12

One possibility is to store all project properties to the built .jar using maven-properties-plugin.
Then you can read these properties using standard (though not too practical) Java Properties API.

        <!-- Build properties to a file -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.0.0</version>
            <executions>
                <execution>
                    <phase>generate-resources</phase>
                    <goals> <goal>write-project-properties</goal> </goals>
                    <configuration>
                        <outputFile> ${project.build.outputDirectory}/build.properties </outputFile>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Be careful with this approach as it may leak properties that are not supposed to end up published, also from settings.xml.

查看更多
小情绪 Triste *
5楼-- · 2019-01-08 11:13

I just did this with an ant task.

<echo file="version.txt">${project.version}</echo>
查看更多
祖国的老花朵
6楼-- · 2019-01-08 11:14

What you are referring to is called filtering

You need to enable filtering on a particular resource, and then use ${project.version} which will be substituted as part of your build

查看更多
太酷不给撩
7楼-- · 2019-01-08 11:16

To add to Sean's answer, you can move the version file to a folder location within the jar by using the targetpath parameter within resource. The following code creates a folder called 'resources' within the jar and the text file (version.number) is found in that folder.

<resource>
    <directory>resources</directory>
    <targetPath>resources</targetPath>
    <filtering>true</filtering>
    <includes>
        <include>version.number</include>
    </includes>
</resource>
<resource>
    <directory>resources</directory>
    <filtering>false</filtering>
    <excludes>
        <exclude>version.number</exclude>
    </excludes>
</resource>
查看更多
登录 后发表回答