Search code examples
mavenjenkinsbuildserverarchive

Jenkins - create a tar.gz from various ear and deliver it on a distant server


I have a maven build which produces a target/myModule.ear for each module it builds.

I want to create a Jenkins job which :

1) Build the project with a "mvn clean install"

2) Archive all the ear into a tar.gz

3) Deliver this tar.gz on a distant server

I am very new to Jenkins so I don't know how to do 2) and 3). The only solution I can come up with is to create a script, but if I do that there is no point in using Jenkins.


Solution

  • You need a parent project which has a war/jar and an ear project to wrap the war, then a distribution project to assemble the tar.gz.

    parent looks like:

    <?xml version="1.0" encoding="UTF-8"?>
    <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.greg</groupId>
      <artifactId>ear-example</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>pom</packaging>
    
      <properties>
            <myproject.version>1.0-SNAPSHOT</myproject.version>
      </properties>
    
      <name>ear-example</name>
      <modules>
        <module>example-ear</module>
        <module>example-war</module>
        <module>distribution</module>
      </modules>
    
    </project>
    

    ear project has dependency to the war project and looks like:

    <?xml version="1.0"?>
    <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <modelVersion>4.0.0</modelVersion>
    
      <parent>
        <groupId>com.greg</groupId>
        <artifactId>ear-example</artifactId>
        <version>1.0-SNAPSHOT</version>
      </parent>
    
      <artifactId>example-ear</artifactId>
      <packaging>ear</packaging>
    
      <dependencies>
        <dependency>
          <groupId>com.greg</groupId>
          <artifactId>example-war</artifactId>
          <version>${project.version}</version>
          <type>war</type>
        </dependency>
      </dependencies>
    
      <build>
       <plugins>
         <plugin>
            <artifactId>maven-ear-plugin</artifactId>
            <version>2.10.1</version>
            <configuration>
                    <modules>
                            <webModule>
                                    <groupId>com.greg</groupId>
                                    <artifactId>example-war</artifactId>
                                    <contextRoot>/appname</contextRoot>
                            </webModule>
                    </modules>
            </configuration>
          </plugin>
        </plugins>
      </build>
    
    </project>
    

    The distribution project looks like this:

    <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>
    
      <parent>
        <groupId>com.greg</groupId>
        <artifactId>ear-example</artifactId>
        <version>1.0-SNAPSHOT</version>
      </parent>
    
      <artifactId>distribution</artifactId>
      <packaging>pom</packaging>
    
      <name>Distribution</name>
    
      <dependencies>
        <dependency>
          <groupId>com.greg</groupId>
          <artifactId>example-ear</artifactId>
          <version>${project.version}</version>
          <type>ear</type>
        </dependency>
      </dependencies>
    
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
              <execution>
                <id>distro-assembly</id>
                <phase>package</phase>
                <goals>
                  <goal>single</goal>
                </goals>
                <configuration>
                  <descriptors>
                    <descriptor>src/assembly/assembly.xml</descriptor>
                  </descriptors>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    
    </project>
    

    The assembly.xml which builds the zip looks like:

    <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
    
      <id>bin</id>
      <formats>
        <format>zip</format>
      </formats>
      <includeBaseDirectory>false</includeBaseDirectory>
      <moduleSets>
        <moduleSet>
          <useAllReactorProjects>true</useAllReactorProjects>
          <binaries>
            <outputDirectory>modules/maven-assembly-plugin</outputDirectory>
            <unpack>false</unpack>
          </binaries>
        </moduleSet>
      </moduleSets>
    </assembly>
    

    The deployment you will have to do yourself