Package into Ark JAR

Edit
Update time: 2019-10-16

Introduction

This sample project shows how to build an executable-ark-jar based on a springboot project with the tool of sofa-ark-maven-plugin.

Preparation

As this project depends on the ark-plugin generated by the project of sample-ark-plugin, please ensure the sample sample-ark-plugin installed in your local maven repository before run this project.

Tools

The Maven plugin of sofa-ark-maven-plugin is provided to build a standard executable-ark-jar, and just needs some simple configurations. Its maven coordinates is:

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

Refer to the document of plugin use for details

Step By Step

Based on the sample project, we will describe step by step how to package a Spring Boot Web project to an executable Ark package

Creating Spring Boot Web Project

Download a standard Spring Boot Web project from the official website https://start.spring.io/

Introducing sample-ark-plugin

Configure items as follows under the main pom.xml of the project, and add the Ark Plugin dependency generated from another sample project, reference documents

<dependency>
     <groupId>com.alipay.sofa</groupId>
     <artifactId>sample-ark-plugin</artifactId>
     <classifier>ark-plugin</classifier>
     <version>${sofa.ark.version}</version>
 </dependency>

Configuring Packaging Plugin

Configure the Maven plugin (sofa-Ark-maven-plugin) as follows under the main pom.xml of the project:

<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, we have configured only a fraction of the items, but they are enough to generate an executable Ark package. The meaning of each configuration item is shown below: * outputDirectory: the directory for the output Ark package files after packaging of mvn package;

  • arkClassifier: the value of classifier included in the Maven coordinates of the Ark specified for release, which is null by default;

Note that arkClassifier is null by default. If you do not specify a classifier, the Jar package uploaded to the repository is actually an executable Ark package. If you need to distinguish it from common packaging, you need to configure a value for this item.

Packaging, Installation, and Release

Similar to the operation of common projects, you can complete the installation and release of plugin package with mvn package, mvn install, and mvn deploy;

Run the application

We offer two ways to start project applications on the Ark container: with the command line or in the IDE. To start in the IDE, additional dependencies need to be added. With the command line, we can directly use java -jar to start project applications. Now we will talk about how to start Ark applications in the IDE;

  • Spring Boot project: We only need to add the following dependency:
<dependency>
    <groupId>com.alipay.sofa</groupId>
    <artifactId>sofa-ark-springboot-starter</artifactId>
    <version>${sofa.ark.version}</version>
</dependency>
  • Common Java projects: Compared with Spring Boot projects, we need to add another dependency to common Java projects:
<dependency>
    <groupId>com.alipay.sofa</groupId>
    <artifactId>sofa-ark-support-starter</artifactId>
    <version>${sofa.ark.version}</version>
</dependency>

In addition, we need to execute container start at the very beginning of the main approach of the project, as follows:

public class Application{
    
    public static void main(String[] args) { 
        SofaArkBootstrap.launch(args);
        ...
    }
    
}

Running Test

SOFAArk provides two implementation classes of org.junit.runner.Runner: ArkJUnit4Runner and ArkBootRunner, which are used to integrate JUnit4 test framework and Spring Test respectively. For TestNG test framework, the annotation @TestNGOnArk is provided. For any TestNG test cases, only those annotated with @TestNGOnArk will run above the Ark Container, otherwise they are the same as common cases.

ArkJUnit4Runner

ArkJUnit4Runner: Similar to JUnit4, we can run common JUnit4 test cases above the SOFAArk container with the annotation ArkJUnit4Runner. A sample code is as follows:

@RunWith(ArkJUnit4Runner.class)
public class UnitTest {

    @Test
    public void test() {
        Assert.assertTrue(true);
    }

}

The use of ArkJUnit4Runner and JUnit4 is almost the same, and other features of JUnit4 test framework are fully compatible,

ArkBootRunner

ArkBootRunner is similar to SpringRunner, refer to the documentto learn the usage of SpringRunner. To be able to run Spring Boot test cases above the SOFAArk container, we only need to use @RunWith(ArkBootRunner.class) to replace @RunWith(SpringRunner.class); A 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()));
    }

}

The use of ArkBootRunner and SpringRunner is almost the same;

TestNGOnArk

The annotation @TestNGOnArk is provided by SOFAArk for developers to mark which TestNG cases run above SOFAArk and which TestNG cases just run normally. 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 above case is annotated with @TestNGTest. So when executing it, Ark Container will start first.