Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get jenkins build number, svn revision number... and display it on my JSF page?

I have a JSF, Spring, Hibernate project. At the bottom of a page I have to have version number. Using jenkins(hudson) I have installed "Parameterized Build" . But it seems it is not working. How can I get jenkins build number, maven project version, svn revision number and display it on my JSF page?

I couldn't find a useful example on the web.

Thanks

main/resources/com/example/build.properties

custom_version  = Test 1.0
pom_version     = ${pom_version}
svn_revision    = ${svn_revision}
build_tag       = ${build_tag}
build_number    = ${build_number}
build_id        = ${build_id}

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">

    <application>
        <resource-bundle>
            <base-name>com.example.build</base-name>
            <var>build</var>
        </resource-bundle>
    </application>

</faces-config>

version.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    >
    <center>

        <p>Version:  #{build['custom_version']}.#{build['build_tag']}.#{build['build_number']}.#{build['build_id']}.#{build['svn_revision']}</p>

    </center>

</ui:composition>

Here is my working solution:

main/resources/com/example/build.properties

custom_version  = ${env.CUSTOM_VERSION}
custom_message  = ${env.CUSTOM_MESSAGE}

pom_version     = ${pom.version}
pom_name        = ${pom.name}
pom_description = ${pom.description}

svn_revision    = ${env.SVN_REVISION}
build_tag       = ${env.JOB_NAME}
build_number    = ${env.BUILD_NUMBER}
build_id        = ${env.BUILD_ID}

pom.xml

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>jenkins_build_deploy</artifactId>
    <packaging>war</packaging>
    <version>1.0.0</version>
    <name>jenkins_build_deploy</name>
    <description>jenkins_build_deploy application</description>
    <url>http://example.com/jenkins_build_deploy</url>

    <scm>
        <url>http://127.0.0.1/dummy</url>
        <connection>scm:svn:http://127.0.0.1/dummy</connection>
        <developerConnection>scm:svn:https://127.0.0.1/dummy</developerConnection>
        <tag>HEAD</tag>
    </scm>

    <build>
        <finalName>jenkins_build_deploy</finalName>

        <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.properties</include>
                </includes>
            </resource>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <filtering>false</filtering>
                <excludes>
                    <exclude>**/*.properties</exclude>
                </excludes>
            </resource>
        </resources>



        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1-alpha-2</version>
                <configuration>
                    <webappDirectory>${project.build.directory}/${project.artifactId}</webappDirectory>
                    <warName>${project.artifactId}</warName>
                    <webResources>
                        <resource>
                            <filtering>true</filtering>
                            <directory>${basedir}/src/main/resources</directory>

                            <directory>${project.build.directory}</directory>
                            <directory>${project.build.sourceDirectory}</directory>
                            <targetPath>WEB-INF/classes</targetPath>
                        </resource>
                    </webResources>
                    <nonFilteredFileExtensions>
                        <nonFilteredFileExtension>gif</nonFilteredFileExtension>
                        <nonFilteredFileExtension>ico</nonFilteredFileExtension>
                        <nonFilteredFileExtension>jpg</nonFilteredFileExtension>
                        <nonFilteredFileExtension>png</nonFilteredFileExtension>
                        <nonFilteredFileExtension>pdf</nonFilteredFileExtension>
                        <nonFilteredFileExtension>jasper</nonFilteredFileExtension>
                    </nonFilteredFileExtensions>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <!-- here the phase you need -->
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target/WEB-INF/classes</outputDirectory>
                            <resources>
                                <resource>
                                    <filtering>true</filtering>

                                    <directory>${basedir}/src/main/resources</directory>

                                    <directory>${project.build.directory}</directory>
                                    <directory>${project.build.sourceDirectory}</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.glassfish.maven.plugin</groupId>
                <artifactId>maven-glassfish-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <debug>true</debug>
                    <echo>true</echo>
                    <terse>true</terse>
                    <force>true</force>
                    <!-- <autoCreate>true</autoCreate> -->
                    <!-- <skip>${test.int.skip}</skip> -->
                    <glassfishDirectory>${glassfish.glassfishDirectory}</glassfishDirectory>
                    <user>${glassfish.user}</user>
                    <adminPassword>${glassfish.adminPassword}</adminPassword>
                    <!-- <passwordFile>${glassfish.home}/domains/${project.artifactId}/master-password</passwordFile> -->
                    <domain>
                        <name>${glassfish.domain.name}</name>
                        <host>${glassfish.domain.host}</host>
                        <adminPort>${glassfish.domain.adminPort}</adminPort>
                    </domain>
                    <components>
                        <component>
                            <name>${project.artifactId}</name>
                            <artifact>${project.build.directory}/${project.artifactId}.war</artifact>
                        </component>
                    </components>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>redeploy</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit47</artifactId>
                        <version>2.12.4</version>
                    </dependency>
                </dependencies>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>buildnumber-maven-plugin</artifactId>
                <version>1.1</version>
                <executions>
                    <execution>
                        <id>buildnumber</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>create</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <doCheck>false</doCheck>
                    <doUpdate>false</doUpdate>
                    <revisionOnScmFailure>unknownbuild</revisionOnScmFailure>
                    <format>{0,number}</format>
                    <items>
                        <item>buildNumber</item>
                    </items>
                </configuration>
            </plugin>

        </plugins>
    </build>

    <dependencies>
        <dependency>
            ****
        </dependency>

    </dependencies>

</project>

jenkins/config.xml

<?xml version='1.0' encoding='UTF-8'?>
<maven2-moduleset plugin="[email protected]">
    <actions/>
    <description>jenkins_build_deploy</description>
    <keepDependencies>false</keepDependencies>
    <properties>
        <hudson.model.ParametersDefinitionProperty>
            <parameterDefinitions>
                <hudson.model.StringParameterDefinition>
                    <name>CUSTOM_VERSION</name>
                    <description>custom_version  = Test 1.0 ${env.CUSTOM_VERSION}</description>
                    <defaultValue>1.0.1</defaultValue>
                </hudson.model.StringParameterDefinition>
                <hudson.model.StringParameterDefinition>
                    <name>CUSTOM_MESSAGE</name>
                    <description>Test Env.</description>
                    <defaultValue>Test Env.</defaultValue>
                </hudson.model.StringParameterDefinition>
            </parameterDefinitions>
        </hudson.model.ParametersDefinitionProperty>
    </properties>
    <scm class="hudson.scm.SubversionSCM" plugin="[email protected]">
        <locations>
            <hudson.scm.SubversionSCM_-ModuleLocation>
                <remote>http://example.com/svn/jenkins_build_deploy/trunk</remote>
                <local>.</local>
                <depthOption>infinity</depthOption>
                <ignoreExternalsOption>false</ignoreExternalsOption>
            </hudson.scm.SubversionSCM_-ModuleLocation>
        </locations>
        <excludedRegions/>
        <includedRegions/>
        <excludedUsers/>
        <excludedRevprop/>
        <excludedCommitMessages/>
        <workspaceUpdater class="hudson.scm.subversion.UpdateUpdater"/>
        <ignoreDirPropChanges>false</ignoreDirPropChanges>
        <filterChangelog>false</filterChangelog>
    </scm>
    <canRoam>true</canRoam>
    <disabled>false</disabled>
    <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
    <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
    <triggers class="vector"/>
    <concurrentBuild>false</concurrentBuild>
    <rootModule>
        <groupId>com.example</groupId>
        <artifactId>jenkins_build_deploy</artifactId>
    </rootModule>
    <goals>clean install -Dmaven.test.skip=true glassfish:deploy</goals>
    <aggregatorStyleBuild>true</aggregatorStyleBuild>
    <incrementalBuild>false</incrementalBuild>
    <perModuleEmail>true</perModuleEmail>
    <ignoreUpstremChanges>false</ignoreUpstremChanges>
    <archivingDisabled>false</archivingDisabled>
    <resolveDependencies>false</resolveDependencies>
    <processPlugins>false</processPlugins>
    <mavenValidationLevel>-1</mavenValidationLevel>
    <runHeadless>false</runHeadless>
    <disableTriggerDownstreamProjects>false</disableTriggerDownstreamProjects>
    <settings class="jenkins.mvn.DefaultSettingsProvider"/>
    <globalSettings class="jenkins.mvn.DefaultGlobalSettingsProvider"/>
    <reporters/>
    <publishers/>
    <buildWrappers/>
    <prebuilders>
        <hudson.tasks.Shell>
            <command>echo CUSTOM_VERSION: $CUSTOM_VERSION
echo CUSTOM_MESSAGE: $CUSTOM_MESSAGE</command>
        </hudson.tasks.Shell>
    </prebuilders>
    <postbuilders/>
    <runPostStepsIfResult>
        <name>FAILURE</name>
        <ordinal>2</ordinal>
        <color>RED</color>
    </runPostStepsIfResult>
</maven2-moduleset>
like image 562
ahmet Avatar asked Feb 20 '13 09:02

ahmet


1 Answers

Well, I would recommend having a propertes file which has these values as replaceable ones via Maven filtering.

For example, set your properties file to look something like this:

custom_version  = Test 1.0
pom_version     = ${pom.version} 
svn_revision    = ${buildNumber}
build_tag       = ${build_tag}   # Will probably need your own implementation
build_number    = ${env.BUILD_NUMBER}
build_id        = ${env.BUILD_ID}

Then in your pom.xml turn on the filtering and use the buildnumber-maven-plugin:

<project ...>
   <build>
      <resources>
         <resource>
           <directory>${basedir}/src/main/resources</directory>
           <filtering>true</filtering>
         </resource>
      </resources>

      <plugins>
         <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>buildnumber-maven-plugin</artifactId>
            <version>1.1</version>
            <executions>
               <execution>
                  <phase>validate</phase>
                  <goals>
                     <goal>create</goal>
                  </goals>
               </execution>
            </executions>
            <configuration>
               <doCheck>true</doCheck>
               <doUpdate>true</doUpdate>
            </configuration>
         </plugin>
      </plugins>

   </build>
</project>

For more details, check here and here.

like image 183
carlspring Avatar answered Nov 01 '22 15:11

carlspring