Displaying POM version number in AngularJS

2019-05-30 19:59发布

问题:

What's the most efficient way of displaying the version number contained in a POM file in my Angular App?

I have a REST application running on Tomcat.

I guess there are a couple of options

  1. Have a REST service that just delivers the version number of the application
  2. Create a text file during the mvn build and copy that into the web app, then read the file from the front end

Has anyone encountered this before?

回答1:

The typical way of doing that is by adding a placeholder for the version in some file of your web-application and replacing that placeholder at compile time:

  1. Add the placeholder ${project.version} in a file of your choice under src/main/webapp.
  2. Configure the maven-war-plugin like this:

    <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
        <configuration>
            <webResources>
                <resource>
                    <directory>src/main/webapp</directory>
                    <filtering>true</filtering>
                </resource>
            </webResources>
        </configuration>
    </plugin>
    

    This plugin will replace the placeholder ${project.version} with the current Maven version at build time.

  3. The rest of your application can now access the Maven version as if it were a simple hard-coded String.



回答2:

You can use maven replacer plugin to replace specific pattern in files (web pages) by version from pom during build. example:

            <plugin>
                <groupId>com.google.code.maven-replacer-plugin</groupId>
                <artifactId>replacer</artifactId>
                <version>1.5.3</version>
                <executions>
                    <execution>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>replace</goal>
                        </goals>                    
                    </execution>
                </executions>
                <configuration>      
                    <file>${warSourceDirectory}/index.html</file>
                    <replacements>
                        <replacement>
                            <token>%PROJECT_VERSION%</token>
                            <value>${project.version}</value>
                        </replacement>
                    </replacements>
                </configuration>
            </plugin>