搜索

25.3. 编译

download PDF

Csimple 语言被解析为常规 Java 源代码,并与所有其他源代码一起编译,或者在通过 camel-csimple-joor 模块 bootstrap 期间编译一次。

编译简单的方法有两种

  • 在构建时使用 camel-csimple-maven-plugin 生成源代码。
  • 使用 camel-csimple-joor,它在 Camel bootstrap 过程中执行运行时内存编译。

25.3.1. 使用 camel-csimple-maven-plugin

camel-csimple-maven-plugin Maven 插件用于发现来自源代码的所有 csimple 脚本,然后在 src/generated/java 文件夹中自动生成源代码,然后编译到所有其他源。

maven 插件将对 .java.xml 文件(Java 和 XML DSL)进行源代码扫描。扫描程序限制检测某些代码模式,如果它们以异常/方式使用,则可能会错过发现一些简单脚本。

使用 camel-csimple-joor 的运行时编译没有这个限制。

好处是所有 csimple 脚本都使用常规 Java 编译器编译,因此所有内容都作为应用程序 JAR 文件中的 .class 文件包含在框中,在运行时不需要额外的依赖项。

要使用 camel-csimple-maven-plugin,您需要将其添加到 pom.xml 文件中,如下所示:

<plugins>
    <!-- generate source code for csimple languages -->
    <plugin>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-csimple-maven-plugin</artifactId>
        <version>${camel.version}</version>
        <executions>
            <execution>
                <id>generate</id>
                <goals>
                    <goal>generate</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    <!-- include source code generated to maven sources paths -->
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>3.1.0</version>
        <executions>
            <execution>
                <phase>generate-sources</phase>
                <goals>
                    <goal>add-source</goal>
                    <goal>add-resource</goal>
                </goals>
                <configuration>
                    <sources>
                        <source>src/generated/java</source>
                    </sources>
                    <resources>
                        <resource>
                            <directory>src/generated/resources</directory>
                        </resource>
                    </resources>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

然后,您还必须添加 build-helper-maven-plugin Maven 插件,将 src/generated 包含在 Java 编译器的源文件夹列表中,以确保生成的源代码已编译并包含在应用 JAR 文件中。

请参阅 Camel 示例(使用 maven 插件的 camel-example-csimple 示例 )。

25.3.2. 使用 camel-csimple-joor

jOOR 库与 Java 编译器集成,并执行 Java 代码运行时编译。

使用 camel-simple-joor 时支持的运行时适用于 Java 独立、Spring Boot、Camel Quarkus 和其他微服务运行时。在 OSGi、Camel Karaf 或任何类型的 Java Application Server 运行时中不支持它。

joor 不支持使用 fat jar 打包(https://github.com/jOOQ/jOOR/issues/69)的运行时编译,它可用于 exploded 类路径。

要使用 camel-simple-joor,只需将其作为依赖项添加到 classpath 中:

<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-csimple-joor</artifactId>
  <version>{CamelSBProjectVersion}</version>
</dependency>

不需要将 Maven 插件添加到 pom.xml 文件中。

请参阅 camel-example-csimple-joor 示例(Camel Examples),它使用 jOOR 编译器。

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.