MAVEN 绑定SVN 版本号,并在web app中使用

在SVN 中的版本号, 如果需要使用,可以通过maven 插件注入到配置文件中,并在webapp启动时注入到应用中

plugin:

<plugin>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>buildnumber-maven-plugin</artifactId>
 <version>1.0</version>
 <executions>
      <execution>
           <phase>validate</phase>
           <goals>
                <goal>create</goal>
           </goals>
      </execution>
 </executions>
 <configuration>
      <doCheck>false</doCheck>
      <doUpdate>false</doUpdate>
      <providerImplementations>
          <svn>javasvn</svn>
      </providerImplementations>
 </configuration>
 <dependencies>
      <dependency>
           <groupId>com.google.code.maven-scm-provider-svnjava</groupId>
           <artifactId>maven-scm-provider-svnjava</artifactId>
           <version>2.0.2</version>
      </dependency>
      <dependency>
           <groupId>org.tmatesoft.svnkit</groupId>
           <artifactId>svnkit</artifactId>
           <version>1.8.6</version>
      </dependency>
 </dependencies>
</plugin>

 原理是使用javasvn provider 来访问SVN

然后在src/main/resources 下面添加一个build.properties

build.properties:
build.version=${version}  # Maven version
build.revision=${buildNumber} # Source code revision number 
build.timestamp=${timestamp} # long value of check in time

这样在build之后,信息会添加到properties中

build.properties:
build.version=1.2.3
build.revision=9876
build.timestamp=12345459

 使用:

public class PageRequestInterceptor extends HandlerInterceptorAdapter {

@Value(“${build.version}”)
private String buildVersion;

@Value(“${build.revision}”)
private String buildRevision;

private DateTime buildTimestamp;

@Value(“${build.timestamp}”)
public void setBuildTimestamp(String timestamp) {
buildTimestamp = new DateTime(Long.parseLong(timestamp));
}

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

request.setAttribute(“buildVersion”, buildVersion);
request.setAttribute(“buildRevision”, buildRevision);
request.setAttribute(“buildDateTime”, buildTimestamp);

这里还有一个问题,每次eclipse自动编译的时候,会把这些配置覆盖,所以需要添加一个plguin来把这些exclude

在POM中添加:

<pluginManagement>
 <plugins>
 <!-- This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself. -->
      <plugin>
           <groupId>org.eclipse.m2e</groupId>
           <artifactId>lifecycle-mapping</artifactId>
           <version>1.0.0</version>
           <configuration>
                <lifecycleMappingMetadata>
                     <pluginExecutions>
                        <pluginExecution>
                            <pluginExecutionFilter>
                            <groupId>org.codehaus.mojo</groupId>
                            <artifactId>buildnumber-maven-plugin</artifactId>
                            <versionRange>[1.0,)</versionRange>
                            <goals>
                                <goal>create</goal>
                            </goals>
                            </pluginExecutionFilter>
                            <action>
                                <execute>
                                  <runOnIncremental>true</runOnIncremental>
                                </execute>
                            </action>
                       </pluginExecution>
                </pluginExecutions>
             </lifecycleMappingMetadata>
          </configuration>
      </plugin>
   </plugins>
 </pluginManagement>

添加之后可以通过右键project->MAVEN->Lifecycle Mapping来验证

相关推荐