使用 Liberty Maven 插件安装 Liberty
Liberty Maven 插件 提供了一组管理和操作 Liberty 配置文件服务器的目标。也可以从 Liberty 存储库或另一个位置下载并安装 Liberty 运行时。这使得在持续集成或部署场景中结合使用 Liberty 和 Maven 变得很容易。
要使用 Liberty Maven 插件,需要为它配置 Liberty 配置文件安装信息。可为该插件配置一个 Liberty 配置文件实例的安装目录、一个打包的服务器归档文件(包含 Liberty 二进制文件的 .zip 文件),或者一个已作为 Maven 工件发布的打包服务器。
从 Liberty 存储库安装 Liberty 运行时
要从 Liberty 存储库安装 Liberty,需要版本号和相关的 Liberty 许可代码。许可代码位于相关许可文件中以 D/N:
开头的行上:
8.5.+
。许可代码e包含在 8.5.5 许可文件中。
要安装最新的 beta 版,可将 version 设置为 2014.+
。 许可代码 包含在 beta 许可文件。
接下来,通过以下方式配置 Liberty Maven 插件:
<plugin> <groupId>net.wasdev.wlp.maven.plugins</groupId> <artifactId>liberty-maven-plugin</artifactId> <configuration> <install> <licenseCode>license code</licenseCode> <version>version</version> </install> ... </configuration> </plugin>
从自定义位置安装 Liberty 运行时
要从其他某个位置安装 Liberty,比如从您组织内的 Web 服务器进行安装,需要为该插件配置一个 Liberty 许可代码以及要下载并安装 Liberty 运行时归档文件的位置。
要找到 Liberty 许可代码,请在 Liberty 运行时归档文件上运行以下命令:
java -jar wlp*runtime.jar –viewLicenseInfo
在输出中寻找以 D/N:
开头的行。
接下来,通过以下方式配置 Liberty Maven 插件:
<plugin> <groupId>net.wasdev.wlp.maven.plugins</groupId> <artifactId>liberty-maven-plugin</artifactId> <configuration> <install> <licenseCode>license code</licenseCode> <runtimeUrl>URL to Liberty runtime archive</runtimeUrl> </install> ... </configuration> </plugin>
Ferret 样本应用程序
Ferret 样本应用程序是一个简单的 Web 应用程序,其中显示了 HTTP 请求的信息,比如请求参数或发送的 HTTP 标头。我们扩展了该样本,以便使用 Liberty Maven 插件从 Liberty 存储库下载和安装 Liberty 运行时,并运行该应用程序。
配置 Liberty Maven 插件
为了针对 Ferret 样本来设置 Liberty Maven 插件,我们执行了以下更改:
我们为该应用程序创建了一个server.xml
配置文件,并将它放在 src/main/wlp
目录中。启用了应用程序所需的特性,并使用了可用于传递特定于环境的设置的变量。具体来讲,server.xml
使用 httpPort
变量来指定服务器监听的端口,并使用 appLocation
变量来指定要运行的应用程序的位置。
<server description="ferret server"> <featureManager> <feature>jsp-2.2</feature> <feature>appSecurity-1.0</feature> </featureManager> <httpEndpoint id="defaultHttpEndpoint" host="*" httpPort="${httpPort}" > <tcpOptions soReuseAddr="true"/> </httpEndpoint> <application name="ferret" context-root="/ferret" location="${appLocation}" type="war" /> </server>我们更新了
pom.xml
文件,以便定义 Liberty Maven 插件的 pluginManagement
部分。
<pluginManagement> <plugins> <plugin> <groupId>net.wasdev.wlp.maven.plugins</groupId> <artifactId>liberty-maven-plugin</artifactId> <version>1.1-SNAPSHOT</version> </plugin> </plugins> </pluginManagement>我们将一个
pluginRepository
条目添加到 pom.xml
文件。该存储库包含 Liberty Maven 插件。
<pluginRepository> <id>sonatype-nexus-snapshots</id> <name>Sonatype Nexus Snapshots</name> <url>https://oss.sonatype.org/content/repositories/snapshots/</url> <snapshots> <enabled>true</enabled> </snapshots> <releases> <enabled>false</enabled> </releases> </pluginRepository>我们将一个
wlp
Maven 配置文件添加到 pom.xml
文件。该配置文件仅在设置了 IBM_LIBERTY_LICENSE
环境属性时激活。这使您可以不必先下载和设置 Liberty 配置文件服务器就可以构建该样本。在 wlp 配置文件中,Liberty Maven 插件被配置为使用来自 server.xml
目录的 src/main/wlp
配置文件创建一个新的 Liberty 服务器。该插件还被配置为传递两个引导属性: httpPort
和 appLocation
属性被设置为端口 9080。 appLocation
属性被设置为 Maven 为构建应用程序创建的目标目录。这样,我们无需将任何文件复制到服务器目录就能运行该应用程序。
<profile> <id>wlp</id> <activation> <property> <name>env.IBM_LIBERTY_LICENSE</name> </property> </activation> <build> <plugins> <plugin> <groupId>net.wasdev.wlp.maven.plugins</groupId> <artifactId>liberty-maven-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>create-server</goal> </goals> </execution> </executions> <configuration> <install> <licenseCode>${env.IBM_LIBERTY_LICENSE}</licenseCode> </install> <configFile>src/main/wlp/server.xml</configFile> <bootstrapProperties> <httpPort>9080</httpPort> <appLocation>../../../../../${project.build.finalName}</appLocation> </bootstrapProperties> </configuration> </plugin> </plugins> </build> </profile>
构建和运行该应用程序
创建 server.xml
文件并更新 pom.xml
文件后,我们可以构建应用程序并在 Liberty 配置文件上运行它:
IBM_LIBERTY_LICENSE
环境变量设置正确的许可代码,以激活 wlp 配置文件:
$ export IBM_LIBERTY_LICENSE=license code像平常一样构建代码。设置
IBM_LIBERTY_LICENSE
环境变量后,Liberty Maven 插件会下载并安装 Liberty 运行时,并配置一个运行该应用程序的服务器:
$ mvn install运行包含该应用程序的 Liberty 服务器:
$ mvn liberty:run-server
就这么简单!服务器运行后,在 Web 浏览器中打开 http://localhost:9080/ferret ,以查看该应用程序的运行情况。
可以访问 GitHub 上的 Ferret 样本存储库 来获取此应用程序的完整源代码。另外,可以查阅 Liberty Maven 插件 文档 来获取关于此特性的更多信息。如果您使用了 Liberty Ant 任务, wlp:install-liberty
任务提供了等效的功能。
在 developerWorks 上的相关资源:
传统 WebSphere 或 Liberty:如何选择? 在 5 分钟内通过 10 个简单步骤集成 JRebel 与 Liberty 使用 WebSphere Developer Tools for Eclipse 在 Liberty 上创建一个 Hello World 应用程序
本文翻译自:Installing Liberty with the Liberty Maven plug-in(2017-01-05)
The post 使用 Liberty Maven 插件安装 Liberty appeared first on developerWorks Developer Center -- 中国(Beta).
文章来源:
Author:developerWorks中国
link:https://developer.ibm.com/cn/blog/2017/installing-liberty-liberty-maven-plug/