Goal 1. The generated war package name is automatically generated according to the format of [project name]_[system version number]_[SVN version number]_[package date].
Objective 2. The system homepage can display the latest version information of the current system (version information in the above format).
2. Implementation ideas:
For goal 1: [project name], [system version number] are configured according to the parameters in the pom.xml file;
[SVN version number] is obtained from the SVN server;
[package date] timestamp;
according to the above information, automatically Generate custom format war package file.
For target 2: append the final version number generated by target 1 to the META-INF/MANIFEST.MF file in the form of a custom attribute; and load the META-INF/MANIFEST.MF file when the system starts, take it out and display it to Specifies the page file.
3. Implementation process:
1. Configuration of pom.xml file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.sinowel</groupId> <artifactId>UFM</artifactId> <version>1.0.20</version> <packaging>war</packaging> <name>Unified Flow Management</name> <url>http://http://10.180.50.30:8099/scpnf</url> <properties> <maven.build.timestamp.format>yyyyMMdd</maven.build.timestamp.format> </properties> <scm> <connection>scm:svn:https://192.168.22.34/svn/UMPlatform4J/SCPNF/banches/UFM</connection> <tag>HEAD</tag> <url>https://192.168.22.34/svn/UMPlatform4J/SCPNF/banches/UFM</url> </scm> <dependencies> <!-- Omitted--> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>buildnumber-maven-plugin</artifactId> <version>1.4</version> <executions> <execution> <phase>validate</phase> <goals> <goal>create</goal> </goals> </execution> </executions> <configuration> <doCheck>false</doCheck> <doUpdate>true</doUpdate> </configuration> <dependencies> <dependency> <groupId>com.google.code.maven-scm-provider-svnjava</groupId> <artifactId>maven-scm-provider-svnjava</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>org.tmatesoft.svnkit</groupId> <artifactId>svnkit</artifactId> <version>1.8.10</version> </dependency> </dependencies> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <failOnMissingWebXml> false </failOnMissingWebXml> <encoding>UTF-8</encoding> <warName>${project.artifactId}_v${project.version}r${buildNumber}b${maven.build.timestamp}</warName> <archive> <manifest> <addDefaultImplementationEntries>true</addDefaultImplementationEntries> </manifest> <manifestEntries> <svn-version>${buildNumber}</svn-version> <deploy-version>${project.artifactId}_v${project.version}r${buildNumber}b${maven.build.timestamp}</deploy-version> </manifestEntries> </archive> </configuration> </plugin> </plugins> </build> </project>
2. Create the ManifestUtils.java file to read the META-INF/MANIFEST.MF file.
public class ManifestUtils { private static final String MANIFEST_DIRECTORY_LOCATION = "META-INF" + File.separator + "MANIFEST.MF"; private static final String MANIFEST_ENTRY = "META-INF/MANIFEST.MF"; /** * * Creates a {@link Reader} for the manifest in the supplied exploded JAR * directory. * * * * @param directory * the exploded JAR directory. * * @return the <code>Reader</code> or <code>null</code> if the manifest * cannot be found. */ public static final Reader manifestReaderFromExplodedDirectory(File directory) { if (directory == null || !directory.isDirectory()) { throw new IllegalArgumentException("Must supply a valid directory"); } try { File manifestFile = new File(directory.getAbsolutePath() + File.separator + MANIFEST_DIRECTORY_LOCATION); if (manifestFile.exists()) { return new FileReader(manifestFile); } else { return null; } } catch (IOException e) { throw new RuntimeException( "Unable to read MANIFEST for exploded directory '" + directory.getAbsolutePath() + "'.", e); } } /** * * Creates a {@link Reader} for the manifest in the supplied JAR file. * * * * @param file * the JAR file. * * @return the <code>Reader</code> or <code>null</code> if the manifest * cannot be found. */ public static final Reader manifestReaderFromJar(File file) { JarFile jar = null; try { jar = new JarFile(file); JarEntry entry = jar.getJarEntry(MANIFEST_ENTRY); if (entry != null) { StringWriter writer = new StringWriter(); FileCopyUtils.copy(new InputStreamReader(jar.getInputStream(entry)), writer); jar.close(); return new StringReader(writer.toString()); } else { return null; } } catch (Exception e) { throw new RuntimeException("Cannot read MANIFEST.MF from jar '" + file.getAbsolutePath() + "'.", e); } finally { if (jar != null) { try { jar.close(); } catch (IOException ioe) { throw new RuntimeException("Failed to close jar '" + file.getAbsolutePath() + "'.", ioe); } } } } /** * Read "deploy-version" release version number information from META-INF/MANIFEST.MF file. * * @param realPath * Web publishing real path * @return */ public static String getDeployVersion(String realPath) { File file = new File(realPath); Reader reader = manifestReaderFromExplodedDirectory(file); BufferedReader bufferedReader = new BufferedReader(reader); String lineTxt = null; String deployVersion = ""; try { while ((lineTxt = bufferedReader.readLine()) != null) { if (lineTxt.startsWith("deploy-version:")) { deployVersion = lineTxt.replace("deploy-version:", ""); return deployVersion; } } } catch (IOException e) { e.printStackTrace (); } finally { if (null != bufferedReader) { try { bufferedReader.close(); } catch (IOException e) { } } if (null != reader) { try { reader.close(); } catch (IOException e) { } } } return deployVersion; } }
3. Call the ManifestUtils.getDeployVersion method in the system startup location such as ContextLoaderListener to obtain the release version and store it. E.g:
String deployVersion = ""; deployVersion = ManifestUtils.getDeployVersion(servletContext.getRealPath("")); servletContext.setAttribute("deployVersion", deployVersion);
4. Obtain the stored version number on the specified page:
<div id="deployVersion ">版本:${deployVersion }</div>