在 上一篇文章 中,我描述了如何使用 OpenLiberty 和 maven 启动服务器,无论是作为独立的,还是作为 maven 的一部分构建,以及如何创建 fatjar 包。
在这篇文章中,我将研究如何使用 Wildfly swarm 来做到这一点。我仍在尝试让 MicroProfile 在 Wildfly full 上运行,所以目前,这个示例与 OpenLiberty 示例的工作方式不同。
我正在使用相同的示例项目,使用更多的 Maven 配置文件来运行不同的部署选项。
(参见 https://github.com/phillip-kruger/javaee-servers-parent)
我想包括一些 MicroProfile 功能,所以这是一个“每日报价”应用程序,而不是基本的“Hello world”。我的应用程序使用工厂来加载报价提供程序(目前只有一个)。当前提供者从 forismatic.com 获取报价。我使用 MicroProfile Configuration API 来配置 URL 和要加载的提供程序等内容。我使用 MicroProfile Fault Tolerance API 来确保在提供程序源不可用时我们能够生存。
您可以在此处获取完整的示例项目:https://github.com/phillip-kruger/quote-service
您可以使用 wildfly-swarm-plugin 运行 (mvn wildfly-swarm:run
) wildfly swarm 实例作为构建的一部分。该插件将执行“分数检测”,这意味着它将查看您需要的应用程序服务器的哪些部分,并且只创建包含这些分数的部署。因此,您仍然可以在依赖项中包含伞形 API,并针对这些 API 进行编码,但在部署时,您将获得正确的大小分布。凉爽的 !
<dependencies> <!-- Java EE --> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>${java-ee.version}</version> <scope>provided</scope> </dependency> <!-- MicroProfile --> <dependency> <groupId>org.eclipse.microprofile</groupId> <artifactId>microprofile</artifactId> <version>${microProfile.version}</version> <type>pom</type> <scope>provided</scope> </dependency> </dependencies>
在包含引用 webjars 的 HTML 文件时,我总是使用过滤,但似乎插件在应用过滤器之前使用了原始源文件,所以我不得不找到一个替代方案。
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.0.0</version> <configuration> <webResources> <resource> <directory>${basedir}/src/main/webapp</directory> <filtering>true</filtering> <includes> <include>**/*.css</include> <include>**/*.jsp</include> </includes> </resource> </webResources> </configuration> </plugin>
在此示例中,我使用 Semantic UI 构建一个显示当天报价的网页:
我使用 HTML 中 CSS 和 JS 版本的 maven 属性,并且在构建时需要将它们替换为真实值:
<link rel="stylesheet" type="text/css" href="webjars/semantic-ui/${semantic-ui.version}/dist/semantic.min.css"> <script type="text/javascript" src="webjars/jquery/${jquery.version}/dist/jquery.min.js" /> <script type="text/javascript" src="webjars/semantic-ui/${semantic-ui.version}/dist/semantic.min.js"></script>
作为替代方案,我使用包目标,然后使用 exec-maven-plugin
来运行 jar。
这也允许我为任何额外的配置传递一个 standalone.xml
:
<plugin> <groupId>org.wildfly.swarm</groupId> <artifactId>wildfly-swarm-plugin</artifactId> <executions> <execution> <id>1</id> <phase>pre-integration-test</phase> <goals> <goal>package</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.6.0</version> <executions> <execution> <id>1</id> <phase>post-integration-test</phase> <goals> <goal>exec</goal> </goals> </execution> </executions> <configuration> <executable>java</executable> <arguments> <argument>-jar</argument> <argument>${project.build.directory}${file.separator}${project.artifactId}-swarm.jar</argument> <argument>-c</argument> <argument>${project.build.directory}${file.separator}standalone.xml</argument> </arguments> </configuration> </plugin>
在我的示例中,standalone.xml
仅包含日志记录配置,但您现在可以包含任何其他配置。
<server xmlns="urn:jboss:domain:4.0"> <profile> <subsystem xmlns="urn:jboss:domain:logging:3.0"> <periodic-rotating-file-handler name="FILE" autoflush="true"> <file path="${wildfly-swarm.logfile}"/> <suffix value=".yyyy-MM-dd"/> <append value="true"/> </periodic-rotating-file-handler> <root-logger> <level name="INFO"/> <handlers> <handler name="FILE"/> </handlers> </root-logger> <logger category="${log.name}"> <level name="${log.level}"/> </logger> </subsystem> </profile> </server>
所以在 qoute-service
示例中,您可以这样做(与 OpenLiberty 示例相同):
mvn clean install -P wildfly-swarm-fatjar
Wildfly swarm 允许您创建空心罐。 (参见这篇文章)也就是说,一个没有应用程序的fatjar,只有应用程序服务器部分。然后,您可以将该应用程序作为命令行输入提供:
java -jar myapp-hollow-swarm.jar myapp.war
因此,如果我们能找到重新加载应用程序部分的方法,我们就可以拥有与完整应用程序(热部署)相同的开发模型。
Wildfly swarm 有一个称为部署扫描器 的部分,您可以将其包含在您的发行版中(fat 或 hollow)。
分数检测不会自动检测到这一点(因为代码中没有对此的引用)。幸运的是,您可以在 Maven 中定义其他分数:
<plugin> <groupId>org.wildfly.swarm</groupId> <artifactId>wildfly-swarm-plugin</artifactId> <executions> <execution> <phase>pre-integration-test</phase> <goals> <goal>package</goal> </goals> </execution> </executions> <configuration> <hollow>true</hollow> <additionalFractions>scanner</additionalFractions> </configuration> </plugin>
为了使这个扫描仪部分起作用,请将其添加到您的 standalone.xml
<subsystem xmlns="urn:jboss:domain:deployment-scanner:2.0"> <deployment-scanner scan-enabled="true" scan-interval="5000" path="/tmp/quote-service/wildfly-swarm/deployments" name="quote-service" auto-deploy-xml="false"/> </subsystem>
如果您现在将应用程序的更新版本移动到定义的路径,您就可以进行热部署。
在引用示例中,这意味着您可以:
您还可以创建一个 fatjar:
构建和启动一个 fatjar 大约需要 10 秒。进行热部署大约需要 2.7 秒。这节省了大量时间,使更改之间的周转时间更快。
标签2: Java教程地址:https://www.cundage.com/article/jcg-hollowjars-deployment-scanner-wildfly-swarm-cool.html