How to package Ark packages

Updated at:

This sample project demonstrates how to use the Maven plug-in to package a Spring Boot Web project into an executable Ark package in a standard format.

Prerequisites

The sample project depends on sample-ark-plugin. You need to install the Ark Plugin on your local machine in advance.

Use tools

SOFAArk provides the Maven plug-in sofa-ark-maven-plugin by default. You can package common Java projects into a standard Ark plug-in by simple configuration. The coordinates of the plug-in are as follows:

<plugin>
     <groupId>com.alipay.sofa</groupId>
     <artifactId>sofa-ark-maven-plugin</artifactId>
     <version>${sofa.ark.version}</version>
</plugin>
Note

For more information, see Ark.

Procedure

  1. Create a Spring Boot Web project.

    Download a standard Spring Boot Web project from the Spring official website.

  2. Introduce sample-ark-plugin.

    Add the Ark Plugin dependency generated by packaging another sample project to the main pom.xml of the project. For more information, see How to package an Ark Plugin. The configuration reference is as follows:

    <dependency>
         <groupId>com.alipay.sofa</groupId>
         <artifactId>sample-ark-plugin</artifactId>
         <classifier>ark-plugin</classifier>
         <version>${sofa.ark.version}</version>
    </dependency>
  3. Configure the packaging plug-in.

    Configure the Maven plug-in sofa-ark-maven-plugin in the main pom.xml of the project. The configuration reference is as follows:

    <build>
        <plugins>
            <plugin>
                <groupId>com.alipay.sofa</groupId>
                <artifactId>sofa-ark-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>default-cli</id>
                        <!--goal executed to generate executable-ark-jar -->
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <!--specify destination where executable-ark-jar will be saved, default saved to ${project.build.directory}-->
                            <outputDirectory>./target</outputDirectory>
                            <!--default none-->
                            <arkClassifier>executable-ark</arkClassifier>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    In this sample project, although only a part of the configuration items is configured, it is enough to generate a usable executable Ark package. The meaning of each configuration items is as follows:

    • outputDirectory: The directory in which the output Ark package file is stored after being packaged by using the mvn package command.

    • arkClassifier: specifies the classifier value contained in the Maven coordinates of the published Ark package. This parameter is empty by default.

    Important

    The default value of the arkClassifier configuration items is null. If no classifier is specified, the JAR package uploaded to the repository is actually a runnable Ark package. If you want to distinguish it from normal packaging, you need to configure this parameter.

  4. Package, install, and publish Ark packages.

    Similar to common engineering operations, you can use mvn package, mvn install, and mvn deploy commands to complete the installation and release of plug-in packages.

  5. Run the Ark package.

    You can use one of the following methods to start an engineering application on an Ark container:

    • Start from the command line

      Use the java -jar to launch the application directly.

    • Starting in the IDE

      When you start an application in the IDE, you need to add additional dependencies.

      • Spring Boot project

        The Spring Boot project needs to add the following dependencies:

        <dependency>
              <groupId>com.alipay.sofa</groupId>
              <artifactId>sofa-ark-springboot-starter</artifactId>
              <version>${sofa.ark.version}</version>
        </dependency>
      • General Java projects

        Compared with Spring Boot projects, common Java projects need to add another dependency:

        <dependency>
              <groupId>com.alipay.sofa</groupId>
              <artifactId>sofa-ark-support-starter</artifactId>
              <version>${sofa.ark.version}</version>
        </dependency>

        In addition, you also need to start the container at the beginning of the project main method, as follows:

        public class Application{
        
             public static void main(String[] args){
                    SofaArkBootstrap.launch(args);
                     ...
              }
        
        }
  6. Run the test case.

    SOFAArk provides two implementation classes for org.junit.runner.Runner: ArkJUnit4Runner and ArkBootRunner. The two are used to integrate the JUnit4 test framework and Spring Test, respectively. For the TestNG test framework, an annotation @TestNGOnArk is provided. For any TestNG test case, only the test case with @TestNGOnArk will run on Ark Container, otherwise it will be the same as the general example.

    • ArkJUnit4Runner

      ArkJUnit4Runner similar JUnit4, you can use the annotation ArkJUnit4Runner to run common JUnit4 test cases on the SOFAArk container. The sample code is as follows:

      @RunWith(ArkJUnit4Runner.class)
      public class UnitTest{
          @Test
          public void test(){
              Assert.assertTrue(true);
          }
      }
      

      ArkJUnit4Runner and JUnit4 use are basically identical, JUnit4 the other features of the test framework are fully compatible.

    • ArkBootRunner

      ArkBootRunner similar SpringRunner. For more information about how to use SpringRunner, see Spring official documentation. To run Spring Boot test cases on the SOFAArk container, simply use @RunWith(ArkBootRunner.class) alternative @RunWith(SpringRunner.class). The sample code is as follows:

      @RunWith(ArkBootRunner.class)
      @SpringBootTest(classes = SpringbootDemoApplication.class)
      public class IntegrationTest {
          @Autowired
          private SampleService sampleService;
      
          @Test
          public void test() {
              Assert.assertTrue("A Sample Service".equals(sampleService.service()));
          }
      }

      ArkBootRunner and SpringRunner use are basically the same.

    • TestNGOnArk

      The annotation @TestNGOnArk is provided by SOFAArk for developers to mark which TestNG use cases run on SOFAArk and which are just ordinary use cases. For example:

      @TestNGOnArk
      public class TestNGTest {
          public static final String TEST_CLASSLOADER = "com.alipay.sofa.ark.container.test.TestClassLoader";
          @Test
          public void test() {
              ClassLoader tccl = Thread.currentThread().getContextClassLoader();
              ClassLoader loader = this.getClass().getClassLoader();
              Assert.assertTrue(tccl.equals(loader));
              Assert.assertTrue(tccl.getClass().getCanonicalName().equals(TEST_CLASSLOADER));
          }
      }
      

      The preceding use case is @TestNGTest. Therefore, when the test case is executed, the Ark Container is started first.