1

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.

cjackson
  • 1,637
  • 1
  • 13
  • 25
user1393608
  • 1,299
  • 3
  • 16
  • 29
  • Are you purposely using gradle for some purpose? What is the end deliverable you're looking to create? A zip, tgz, etc? – cjackson Sep 24 '15 at 15:39
  • rpm is end deliverable. Can you please give me example/sample of creating a rpm and also using resource filtering using maven (pom.xml)? – user1393608 Sep 25 '15 at 04:46
  • I've updated my answer to include a solution for using maven rpm plugin. – cjackson Sep 25 '15 at 16:16

1 Answers1

0

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.
cjackson
  • 1,637
  • 1
  • 13
  • 25
  • Since I was not able to put all my content into comment, I answered the question and put all code there. – user1393608 Sep 23 '15 at 12:49
  • You could have just edited your question to include the new content. – cjackson Sep 24 '15 at 15:37
  • Thanks @cjackson. This is nice starting point for me. How can I add in pom.xml multiple rpms to build for different HDP stacks? I also want to build debian packages for same rpms, how can I acheive that? – user1393608 Sep 28 '15 at 06:53
  • If you found this answer useful, please accept it. I've edited my answer to comment on multiple rpms. Ask another SO question on how to create deb packages using maven. – cjackson Sep 29 '15 at 14:31