如何获取jenkins版本号、svn修订号..并将其显示在我的JSF页面上



我有一个JSF、Spring、Hibernate项目。在一页的底部,我必须有版本号。使用jenkins(hudson),我安装了"参数化构建"。但它似乎不起作用。如何获取jenkins构建号、maven项目版本、svn修订号并将其显示在JSF页面上?

我在网上找不到有用的例子。

感谢

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>

.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>

这是我的工作解决方案:

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="maven-plugin@1.501">
    <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="subversion@1.45">
        <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>

好吧,我建议拥有一个属性文件,该文件通过Maven过滤将这些值作为可替换值。

例如,将属性文件设置为如下所示:

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}

然后在pom.xml中打开过滤并使用buildnumber maven插件:

<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>

有关更多详细信息,请查看此处和此处。

相关内容

  • 没有找到相关文章

最新更新