How to package Ark packages
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>For more information, see Ark.
Procedure
Create a Spring Boot Web project.
Download a standard Spring Boot Web project from the Spring official website.
Introduce sample-ark-plugin.
Add the Ark Plugin dependency generated by packaging another sample project to the main
pom.xmlof 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>Configure the packaging plug-in.
Configure the Maven plug-in sofa-ark-maven-plugin in the main
pom.xmlof 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
Arkpackage. 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 themvn packagecommand.arkClassifier: specifies theclassifiervalue contained in the Maven coordinates of the published Ark package. This parameter is empty by default.
ImportantThe default value of the
arkClassifierconfiguration items is null. If noclassifieris 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.Package, install, and publish Ark packages.
Similar to common engineering operations, you can use
mvn package,mvn install, andmvn deploycommands to complete the installation and release of plug-in packages.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 -jarto 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
mainmethod, as follows:public class Application{ public static void main(String[] args){ SofaArkBootstrap.launch(args); ... } }
Run the test case.
SOFAArk provides two implementation classes for
org.junit.runner.Runner:ArkJUnit4RunnerandArkBootRunner. The two are used to integrate the JUnit4 test framework and Spring Test, respectively. For the TestNG test framework, an annotation@TestNGOnArkis provided. For any TestNG test case, only the test case with@TestNGOnArkwill run on Ark Container, otherwise it will be the same as the general example.ArkJUnit4Runner
ArkJUnit4RunnersimilarJUnit4, you can use the annotationArkJUnit4Runnerto 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); } }ArkJUnit4RunnerandJUnit4use are basically identical,JUnit4the other features of the test framework are fully compatible.ArkBootRunner
ArkBootRunnersimilarSpringRunner. For more information about how to useSpringRunner, 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())); } }ArkBootRunnerandSpringRunneruse are basically the same.TestNGOnArk
The annotation
@TestNGOnArkis 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.