Dynamically create the version number within the Ambari's metainfo.xml file using maven build processes

限于喜欢 提交于 2020-01-16 04:00:18

问题


I don’t want to hardcode my service version into metainfo.xml, Can I do it?

    <service>
      <name>DUMMY_APP</name>
      <displayName>My Dummy APP</displayName>
      <comment>This is a distributed app.</comment>
      <version>0.1</version> --------------This I don't want to hardcode, Can I doit?
     <components>
     ...
    </components>
  </service>

I am using maven as my build tool.


回答1:


This can be done by using maven's resource filtering. Three steps are required:

  • Define a maven property that will hold the version number
  • Add that maven property in between filter tags in the metainfo.xml file
  • Notate in the pom that the metainfo.xml file needs to be filtered.

For example lets assume you want to use the same version as the projects maven version identifier, ${project.version}, as your version in the metainfo.xml file. You would replace <version>0.1</version> with <version>${project.version}</version>. Then in your pom file you would need to list that metainfo.xml file as needing to be filtered. The procedure for this step will vary depending on how you are bundling the custom Ambari service (rpm, assembly, etc.). In general whichever plugin you are using when you list the sources (content to include in the bundle) you will need to specify the path to the metainfo.xml file and make sure filtering is set to true.

Now lets assume you want to create an rpm that will install your artifact. It would look something like this:

Your project structure should be as follows:

--src
  --main
    --resources
      --configuration
      --scripts
      metainfo.xml

Your pom file would look like this:

<?xml version="1.0"?>
<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.example</groupId>
<version>0.0.1-SNAPSHOT</version>
<artifactId>com.example.project</artifactId>
<packaging>pom</packaging>

<properties>
    <hdp.name>HDP</hdp.name>
    <hdp.version>2.3</hdp.version>
    <stack.dir.prefix>/var/lib/ambari-server/resources/stacks</stack.dir.prefix>
</properties>
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>rpm-maven-plugin</artifactId>
            <version>2.1.2</version>
            <extensions>true</extensions>
            <executions>
                <execution>
                    <id>generate-hdp-rpm</id>
                    <phase>package</phase>
                    <goals>
                        <goal>attached-rpm</goal>
                    </goals>
                    <configuration>
                        <classifier>hdp</classifier>
                        <needarch>true</needarch>
                        <sourceEncoding>UTF-8</sourceEncoding>
                        <distribution>blah</distribution>
                        <group>something/Services</group>
                        <packager>company</packager>
                        <vendor>company</vendor>
                        <name>SERVICENAME-ambari-hdp</name>
                        <defineStatements>
                            <!-- I use the below line to prevent compiled python scripts from failing the build -->
                            <defineStatement>_unpackaged_files_terminate_build 0</defineStatement>
                            <defineStatement>platform_stack_directory ${stack.dir.prefix}/${hdp.name}/${hdp.version}</defineStatement>
                        </defineStatements>
                        <requires>
                            <require>ambari-server</require>
                        </requires>
                        <mappings>
                            <mapping>
                                <directory>${stack.dir.prefix}/${hdp.name}/${hdp.version}/services/SERVICENAME</directory>
                                <filemode>755</filemode>
                                <username>root</username>
                                <groupname>root</groupname>
                            </mapping>
                            <mapping>
                                <directory>${stack.dir.prefix}/${hdp.name}/${hdp.version}/services/SERVICENAME</directory>
                                <directoryIncluded>false</directoryIncluded>
                                <filemode>755</filemode>
                                <username>root</username>
                                <groupname>root</groupname>
                                <sources>
                                    <source>
                                        <location>src/main/resources/metainfo.xml</location>
                                        <filter>true</filter>
                                    </source>
                                </sources>
                            </mapping>
                            <mapping>
                                <directory>${stack.dir.prefix}/${hdp.name}/${hdp.version}/services/SERVICENAME/configuration</directory>
                                <filemode>755</filemode>
                                <username>root</username>
                                <groupname>root</groupname>
                            </mapping>
                            <mapping>
                                <directory>${stack.dir.prefix}/${hdp.name}/${hdp.version}/services/SERVICENAME/configuration</directory>
                                <directoryIncluded>false</directoryIncluded>
                                <filemode>755</filemode>
                                <username>root</username>
                                <groupname>root</groupname>
                                <sources>
                                    <source>
                                        <location>src/main/resources/configuration</location>
                                    </source>
                                </sources>
                            </mapping>
                            <mapping>
                                <directory>${stack.dir.prefix}/${hdp.name}/${hdp.version}/services/SERVICENAME/package</directory>
                                <filemode>755</filemode>
                                <username>root</username>
                                <groupname>root</groupname>
                            </mapping>
                            <mapping>
                                <directory>${stack.dir.prefix}/${hdp.name}/${hdp.version}/services/SERVICENAME/package/scripts</directory>
                                <filemode>755</filemode>
                                <username>root</username>
                                <groupname>root</groupname>
                            </mapping>
                            <mapping>
                                <directory>${stack.dir.prefix}/${hdp.name}/${hdp.version}/services/SERVICENAME/package/scripts</directory>
                                <directoryIncluded>false</directoryIncluded>
                                <filemode>755</filemode>
                                <username>root</username>
                                <groupname>root</groupname>
                                <sources>
                                    <source>
                                        <location>src/main/resources/scripts</location>
                                    </source>
                                </sources>
                            </mapping>
                        </mappings>
                    </configuration>
                </execution>
                <!-- You may have multiple executions if you want to create rpms for stacks other then HDP -->
            </executions>
        </plugin>
    </plugins>
</build>
<dependencies>
    <!-- List any dependencies you need -->
</dependencies>

This will create an rpm that when installed will add your service to the HDP 2.3 stack. After installing the rpm you would have to restart ambari-server to make sure the new service definition is picked up.

Update: To create additional RPMs for other stacks, you will need to:

  • Duplicate the execution block in the rpm-maven-plugin section of the pom.
  • Change the id element of the new execution to be unique.
  • Modify the mappings to reflect the directory/file structure you want for the other stack.


来源:https://stackoverflow.com/questions/32431879/dynamically-create-the-version-number-within-the-ambaris-metainfo-xml-file-usin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!