Maven build number plugin, how to save the build number in a file?

穿精又带淫゛_ 提交于 2019-12-03 04:56:06

问题


I've a Java project using Spring Framework and Git and I wanted to display a build number. I found the Build Number Maven plugin. With Git the build number is a Git hash. I dislike that and I thought a date was much more expressive.

I found this excellent blog article explaining how to use build number plugin with a different profile for SVN and Git. Since I just use Git, instead of creating a new profile, I just copied the plugin part in my build tag.

When I run "mvn package" it tells me:

[INFO] --- buildnumber-maven-plugin:1.0:create (default) @ sherd ---
[INFO] Storing buildNumber: 2011-08-04_21-48_stivlo at timestamp: 1312487296631

Which looks ok, but I wonder, where is it stored? "git status" doesn't detect any new file and it seems it's not in target/ too (target/ is in my .gitignore).

Maybe I've to change the configuration to store the build number in a file? How can I use the build number value?


Thanks to the hint of Michael-O, I read the chapter about how to filter resource files in Maven Getting Started Guide. I've created a file application.properties in src/main/resources/properties/application.properties with the following contents:

# application properties
application.name=${pom.name}
application.version=${pom.version}
application.build=${buildNumber}

I've added the following XML snippet within my build section:

<resources>
    <resource>
        <directory>src/main/resources/properties</directory>
        <filtering>true</filtering>
    </resource>
</resources>

Now when I call from command line "mvn package", this property file gets saved in target/classes/properties/application.properties, for instance with the following contents:

# application properties
application.name=Sherd Control Panel
application.version=1.0.1-SNAPSHOT
application.build=2011-08-05_05-55_stivlo

Everything works fine from command line, but, sigh, m2eclipse gives Build errors:

05/08/11 6.05.03 CEST: Build errors for obliquid-cp; 
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal
org.codehaus.mojo:buildnumber-maven-plugin:1.0:create (default) on project 
sherd: Cannot get the branch information from the scm repository : 
Exception while executing SCM command.

For some reason m2eclipse tries to connect to my repository, but it can't because it's a Git repository accessed with SSH and a private key. I wonder if I can tell m2eclipse to not connect to Git.


After more digging I found about revisionOnScmFailure option, set it to true and now also m2eclipse works. For reference, here is the full configuration of buildnumber maven plugin that I used (it goes in pom.xml in the build / plugins section).

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>buildnumber-maven-plugin</artifactId>
    <version>1.0</version>
    <executions>
        <execution>
            <phase>generate-resources</phase>
            <goals>
                <goal>create</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <doCheck>false</doCheck>
        <doUpdate>false</doUpdate>
         <revisionOnScmFailure>true</revisionOnScmFailure>
        <format>{0,date,yyyy-MM-dd_HH-mm}_{1}</format>
        <items>
            <item>timestamp</item>
            <item>${user.name}</item>
        </items>
    </configuration>
</plugin>

回答1:


Store it in a filtered properties file. See Using maven to output the version number to a text file




回答2:


You shouldn't set revisionOnScmFailure option to true, it doesn't expect a boolean. Set it to revision string you want to use when SCM is unavailable, like na or like that. It doesn't matter for your case since you override the build number format but it would be more correct.

See buildnumber-maven-plugin docs.




回答3:


I could not reproduce the problem reported by the OP. In my case both command line and m2eclipse work fine and the file is generated correctly in the target/classes folder. The answer provided by @KasunBG is incorrect. The buildNumber.properties is generated only if you use the following:

        <format>{0,number}</format>
        <items>
            <item>buildNumber</item>
        </items>

buildNumber.properties is used to store a number which can be incremented. For this reason ( I think ) the plugin doesn't generate this file if you use timestamp/scmVersion etc.




回答4:


The documentation page says that the properties files is stored at ${basedir}/buildNumber.properties, which is created when the buildnumber:create phase is ran.



来源:https://stackoverflow.com/questions/6947827/maven-build-number-plugin-how-to-save-the-build-number-in-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!