简体   繁体   中英

SVN revision number accessible in compiled GWT package

I have a GWT project which has its source managed in SVN, is packaged using Maven and has its builds managed via Hudson. I want to get the SVN revision number of the latest check-in/build to be visible in a comment at the bottom of the application root HTML file. I don't care where in the development process this happens!

Here are the options I've Googled for so far, with no success:

  • Can I get Hudson to, after building, write the build/revision number to one of its build output files (namely the application root HTML file)? I've seen no way to do this.
  • Can I get Maven to write the SVN revision number to one of its build output files (namely the application root HTML file)? I've seen ways of Maven writing this to a JAR/WAR manifest file (which can then be accessed in the Java code), but I'm not sure that this works in GWT (I'm not particularly knowledgeable about the internals of GWT).
  • Can I get SubVersion to, as a pre-commit hook, write the version number to a particular file? I know it's easy to write the version number to the file you're editing, but not so sure about writing to a totally separate file (so that it's updated on every commit, regardless of whether it was changed in that commit).

Does anyone have a complete, end-to-end example of how to get any of these working? I keep finding little snippets of code/config which do one part of the job, but not anything that is exactly what I'm looking for.

Thanks!

You can achieve what you're looking for with a combination of Maven and Hudson. In this example let's imagine you want the file version.txt at the root of your web app to contain the revision.

version.txt :

${SVN_REVISION}

In your project's pom.xml enable filtering in the maven-war-plugin :

<plugin>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.1</version>
  <configuration>
    <webResources>
      <webResource>
        <directory>src/main/webapp</directory>
        <filtering>true</filtering>
        <includes>
          <include>version.txt</include>
        </includes>
      </webResource>
    </webResources>
  </configuration>
</plugin>

Make sure that Hudson is building your project via. Subversion checkout and it will set the SVN_REVISION environment variable for every build and Maven will fill it in.

This solution is for those who keep getting {SVN_REVISION} instead of the actual SVN_REVISION value inside the target file .

My solution was to also use filtering. However since I wanted the SVN_REVISION to appear inside my gwt app's main html page (as a means of "fighting" the user's cache, making sure that if we carry out a new build, then the user downloads the latest html file), I wasn't able to use Jason Terk's solution. The html file simply printed {SVN_REVISION} instead of the actual SVN_REVISION value.

So I defined a property inside <properties> :

<properties>
    ...
    <buildVersion>${SVN_REVISION}</buildVersion>
    ...
</properties>

I then made sure I was filtering the appropriate html file (like described in Jason's solution), and then "extracted" the SVN_REVISION in the html file like so:

<script type="text/javascript">
    ...
    var versionIdSuffix = '?v=${buildVersion}';
    ....
</script>

In a nutshell - I wasn't able to directly reference the {SVN_REVISION} property from inside the html file, so I "wrapped" it through <properties> , letting maven reference it instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM