|
1 | 1 | # 使用 GraalVM 构建 tio-boot-native 程序
|
2 | 2 |
|
3 |
| -介绍如何使用 TIO-Boot 和 GraalVM 设置项目。这对希望构建快速高效的 Java 服务器应用程序的开发人员特别有用。我们将从安装必要的依赖开始,然后构建一个可以独立于 JVM 运行的本地二进制镜像。 |
| 3 | +介绍如何使用 GraalVM 构建 tio-boot-native 程序。这对希望构建快速高效的 Java 服务器应用程序的开发人员特别有用。我们将从安装必要的依赖开始,然后构建一个可以独立于 JVM 运行的本地二进制镜像。 |
4 | 4 |
|
5 | 5 | ## 安装 tio 依赖项
|
6 | 6 |
|
@@ -378,3 +378,188 @@ public class HelloApp {
|
378 | 378 | }
|
379 | 379 | }
|
380 | 380 | ```
|
| 381 | + |
| 382 | +5. **pom.xml** |
| 383 | + |
| 384 | +```xml |
| 385 | +<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 386 | + <modelVersion>4.0.0</modelVersion> |
| 387 | + <groupId>com.litongjava</groupId> |
| 388 | + <artifactId>tio-boot-http-request-handler-demo</artifactId> |
| 389 | + <version>1.0.0</version> |
| 390 | + <properties> |
| 391 | + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
| 392 | + <java.version>1.8</java.version> |
| 393 | + <maven.compiler.source>${java.version}</maven.compiler.source> |
| 394 | + <maven.compiler.target>${java.version}</maven.compiler.target> |
| 395 | + <graalvm.version>23.1.1</graalvm.version> |
| 396 | + <tio-boot.version>1.4.3</tio-boot.version> |
| 397 | + <lombok-version>1.18.30</lombok-version> |
| 398 | + <hotswap-classloader.version>1.2.2</hotswap-classloader.version> |
| 399 | + <final.name>web-hello</final.name> |
| 400 | + <main.class>com.litongjava.tio.web.hello.HelloApp</main.class> |
| 401 | + </properties> |
| 402 | + <dependencies> |
| 403 | + <dependency> |
| 404 | + <groupId>com.litongjava</groupId> |
| 405 | + <artifactId>tio-boot</artifactId> |
| 406 | + <version>${tio-boot.version}</version> |
| 407 | + </dependency> |
| 408 | + |
| 409 | + <dependency> |
| 410 | + <groupId>org.projectlombok</groupId> |
| 411 | + <artifactId>lombok</artifactId> |
| 412 | + <version>${lombok-version}</version> |
| 413 | + <optional>true</optional> |
| 414 | + <scope>provided</scope> |
| 415 | + </dependency> |
| 416 | + |
| 417 | + <dependency> |
| 418 | + <groupId>junit</groupId> |
| 419 | + <artifactId>junit</artifactId> |
| 420 | + <version>4.12</version> |
| 421 | + <scope>test</scope> |
| 422 | + </dependency> |
| 423 | + </dependencies> |
| 424 | + <profiles> |
| 425 | + <!-- 开发环境 --> |
| 426 | + <profile> |
| 427 | + <id>development</id> |
| 428 | + <activation> |
| 429 | + <activeByDefault>true</activeByDefault> |
| 430 | + </activation> |
| 431 | + <dependencies> |
| 432 | + <dependency> |
| 433 | + <groupId>ch.qos.logback</groupId> |
| 434 | + <artifactId>logback-classic</artifactId> |
| 435 | + <version>1.2.3</version> |
| 436 | + </dependency> |
| 437 | + </dependencies> |
| 438 | + </profile> |
| 439 | + |
| 440 | + <!-- 生产环境 --> |
| 441 | + <profile> |
| 442 | + <id>production</id> |
| 443 | + <dependencies> |
| 444 | + <dependency> |
| 445 | + <groupId>ch.qos.logback</groupId> |
| 446 | + <artifactId>logback-classic</artifactId> |
| 447 | + <version>1.2.3</version> |
| 448 | + </dependency> |
| 449 | + </dependencies> |
| 450 | + <build> |
| 451 | + <plugins> |
| 452 | + <plugin> |
| 453 | + <groupId>org.springframework.boot</groupId> |
| 454 | + <artifactId>spring-boot-maven-plugin</artifactId> |
| 455 | + <version>2.7.4</version> |
| 456 | + <configuration> |
| 457 | + <mainClass>${main.class}</mainClass> |
| 458 | + <excludeGroupIds>org.projectlombok</excludeGroupIds> |
| 459 | + </configuration> |
| 460 | + <!-- 设置执行目标 --> |
| 461 | + <executions> |
| 462 | + <execution> |
| 463 | + <goals> |
| 464 | + <goal>repackage</goal> |
| 465 | + </goals> |
| 466 | + </execution> |
| 467 | + </executions> |
| 468 | + </plugin> |
| 469 | + </plugins> |
| 470 | + </build> |
| 471 | + </profile> |
| 472 | + <!-- assembly --> |
| 473 | + <profile> |
| 474 | + <id>assembly</id> |
| 475 | + <dependencies> |
| 476 | + <dependency> |
| 477 | + <groupId>ch.qos.logback</groupId> |
| 478 | + <artifactId>logback-classic</artifactId> |
| 479 | + <version>1.2.3</version> |
| 480 | + </dependency> |
| 481 | + </dependencies> |
| 482 | + <build> |
| 483 | + <plugins> |
| 484 | + <plugin> |
| 485 | + <groupId>org.apache.maven.plugins</groupId> |
| 486 | + <artifactId>maven-jar-plugin</artifactId> |
| 487 | + <version>3.2.0</version> |
| 488 | + </plugin> |
| 489 | + <plugin> |
| 490 | + <groupId>org.apache.maven.plugins</groupId> |
| 491 | + <artifactId>maven-assembly-plugin</artifactId> |
| 492 | + <version>3.1.1</version> |
| 493 | + <configuration> |
| 494 | + <archive> |
| 495 | + <manifest> |
| 496 | + <mainClass>${main.class}</mainClass> |
| 497 | + </manifest> |
| 498 | + </archive> |
| 499 | + <descriptorRefs> |
| 500 | + <descriptorRef>jar-with-dependencies</descriptorRef> |
| 501 | + </descriptorRefs> |
| 502 | + <appendAssemblyId>false</appendAssemblyId> |
| 503 | + </configuration> |
| 504 | + <executions> |
| 505 | + <execution> |
| 506 | + <id>make-assembly</id> |
| 507 | + <phase>package</phase> |
| 508 | + <goals> |
| 509 | + <goal>single</goal> |
| 510 | + </goals> |
| 511 | + </execution> |
| 512 | + </executions> |
| 513 | + </plugin> |
| 514 | + </plugins> |
| 515 | + </build> |
| 516 | + </profile> |
| 517 | + <profile> |
| 518 | + <id>native</id> |
| 519 | + <dependencies> |
| 520 | + <!-- GraalVM 环境使用 jdk log --> |
| 521 | + <dependency> |
| 522 | + <groupId>org.slf4j</groupId> |
| 523 | + <artifactId>slf4j-jdk14</artifactId> |
| 524 | + <version>1.7.31</version> |
| 525 | + </dependency> |
| 526 | + <!-- GraalVM --> |
| 527 | + <dependency> |
| 528 | + <groupId>org.graalvm.sdk</groupId> |
| 529 | + <artifactId>graal-sdk</artifactId> |
| 530 | + <version>${graalvm.version}</version> |
| 531 | + <scope>provided</scope> |
| 532 | + </dependency> |
| 533 | + </dependencies> |
| 534 | + <build> |
| 535 | + <finalName>${final.name}</finalName> |
| 536 | + <plugins> |
| 537 | + <plugin> |
| 538 | + <groupId>org.graalvm.nativeimage</groupId> |
| 539 | + <artifactId>native-image-maven-plugin</artifactId> |
| 540 | + <version>21.2.0</version> |
| 541 | + <executions> |
| 542 | + <execution> |
| 543 | + <goals> |
| 544 | + <goal>native-image</goal> |
| 545 | + </goals> |
| 546 | + <phase>package</phase> |
| 547 | + </execution> |
| 548 | + </executions> |
| 549 | + <configuration> |
| 550 | + <skip>false</skip> |
| 551 | + <imageName>${final.name}</imageName> |
| 552 | + <mainClass>${main.class}</mainClass> |
| 553 | + <buildArgs> |
| 554 | + -H:+RemoveSaturatedTypeFlows |
| 555 | + --allow-incomplete-classpath |
| 556 | + --no-fallback |
| 557 | + </buildArgs> |
| 558 | + </configuration> |
| 559 | + </plugin> |
| 560 | + </plugins> |
| 561 | + </build> |
| 562 | + </profile> |
| 563 | + </profiles> |
| 564 | +</project> |
| 565 | +``` |
0 commit comments