This article is the 21st day article of Java EE Advent Calendar 2016. Yesterday was emag @ github's WildFly Swarm 3 selections #javaee.
Java EE applications are generally distributed using EAR / WAR files, but application servers must be installed and configured to execute them. It is convenient to be able to distribute it as a JAR file that can be executed with java -jar
. IBM's Java EE application server WebSphere Liberty also provides the ability to package executable JAR files.
WebSphere Liberty can be downloaded and used free of charge for development purposes. Download runtime from WASDev download site (https://developer.ibm.com/wasdev/downloads/) (https://developer.ibm.com/wasdev/downloads/liberty-profile-using-non- Let's do eclipse-environments /). Java EE 7 Web Profile compatible version Full Platform compatible version and Java SDK compatible version are available, so select the one you need and download it. All you have to do is unzip the downloaded ZIP file.
$ unzip wlp-webProfile7-16.0.0.4.zip
You can create a default configuration file using the server command under the wlp / bin directory. Here, we are creating the name server1. If you omit the server name, a server named defaultServer is created.
$ cd wlp/bin
$ ./server create server1
A configuration file called server.xml has been created in the wlp / usr / servers / server1 directory. The default configuration file varies depending on the environment, but here it is rewritten as follows.
server.xml
<?xml version="1.0" encoding="UTF-8"?>
<server description="new server">
<!-- Enable features -->
<featureManager>
<feature>jsp-2.3</feature>
</featureManager>
<httpEndpoint id="defaultHttpEndpoint"
httpPort="9080"
httpsPort="9443" />
</server>
In Liberty, the provided functionality is implemented in a separate component called feature. Only the features specified in the configuration file will be loaded into memory and initialized. The features available in the current environment are listed with the productInfo featureInfo
command. The missing features can be downloaded and installed by executing a command such as ʻinstall Utility install adminCenter-1.0`.
Full-featured features such as webProfile-7.0 and javaee-7.0 are also defined, but if possible, if you specify only the features of the functions you want to use, the startup time will be shorter and the JAR file created after that will also be smaller.
Here, only the functions of JSP 2.3 (and Servlet 3.1, which is the premise of it) are enabled [^ 1]. [^ 1]: If you downloaded and installed the MicroProfile package, the JSP 2.3 feature is not included. In that case, please install additional features with ʻinstall Utility install jsp-2.3`.
Of course, you can use the separately developed WAR file and EAR file, but here let's create the application directly on the disk.
Create a directory called test.war in wlp / usr / servers / server1 / dropsins. This alone will be recognized by Liberty as a web application. Create a file called index.jsp in it.
index.jsp
<html>
<head><title>Hello Liberty!</title></head>
<body>
<h1>Hello Liberty!</h1>
It is <%= new java.util.Date() %> now.
</body>
</html>
For more practical server.xml configuration and application installation method, see [Liberty profile] in the here document. Development and configuration by](http://public.dhe.ibm.com/software/dw/jp/websphere/was/was855liberty_ws/WASV855_UPDATE_03.pdf) etc.
Run the server command under the wlp / bin directory.
$ ./server run server1
Java HotSpot(TM) 64-Bit Server VM version 1.8.0 (ja_JP)So, server1(WebSphere Application Server 16.0.0.4/wlp-1.0.14.cl160320160831-1555)Is running
[AUDIT ] CWWKE0001I:Server server1 has started.
[AUDIT ] CWWKZ0058I:I'm monitoring the dropins of my application.
[AUDIT ] CWWKT0016I:Web application available(default_host): http://localhost:9080/test/
[AUDIT ] CWWKZ0001I:Application test is 0.It started in 231 seconds.
[AUDIT ] CWWKF0012I:The server has installed the following features:[jsp-2.3, servlet-3.1, el-3.0]。
[AUDIT ] CWWKF0011I:Server server1 is ready for Smarter Planet.
If you access http: // localhost: 9080 / test / with a browser, the created JSP will be executed. You can stop the server with Ctrl + C.
The Liberty execution environment + server configuration + application created in this way can be packaged together in one JAR file.
$ ./server package server1 --include=minify,runnable --archive=/tmp/server1.jar
Server server1 is being packaged.
You are querying the contents on server server1.
Java HotSpot(TM) 64-Bit Server VM version 1.8.0 (ja_JP)So, server1(WebSphere Application Server 16.0.0.4/wlp-1.0.14.cl160320160831-1555)Is running
[AUDIT ] CWWKE0001I:Server server1 has started.
[AUDIT ] CWWKF0026I:Server server1 is ready to create a smaller package.
[AUDIT ] CWWKE0036I: 1.Server server1 stopped after 439 seconds.
Creating an archive for server server1.
Server server1 package/tmp/server1.Completed with jar.
Let's take a look at the completed JAR file.
$ ls -l /tmp/server1.jar
-rw-r----- 1 takakiyo wheel 27055142 12 21 17:16 /tmp/server1.jar
The size is 27 MB. Let's do this.
$ java -jar /tmp/server1.jar
file/Users/takakiyo/wlpExtract/server1_15934526082127/Extracted to wlp.
All product files have been successfully extracted.
Java HotSpot(TM) 64-Bit Server VM version 1.8.0 (ja_JP)So, server1(WebSphere Application Server 16.0.0.4/wlp-1.0.14.cl160320160831-1555)Is running
[AUDIT ] CWWKE0001I:Server server1 has started.
[AUDIT ] CWWKZ0058I:I'm monitoring the dropins of my application.
[AUDIT ] CWWKT0016I:Web application available(default_host): http://localhost:9080/test/
[AUDIT ] CWWKZ0001I:Application test is 0.It started in 163 seconds.
[AUDIT ] CWWKF0012I:The server has installed the following features:[jsp-2.3, servlet-3.1, el-3.0]。
[AUDIT ] CWWKF0011I:Server server1 is ready for Smarter Planet.
Somehow it is expanded to a temporary directory, but thanks to that, ClassLoader # getResourceAsStream () etc. also work without problems. The contents of the temporary directory are automatically deleted when the server is stopped.
It would be convenient to be able to operate according to the environment without modifying the created package. Here, we will show you how to refer to environment variables in the configuration file.
Modify the LISTEN port number in server.xml to $ {env. Environment variable name}
as follows.
<httpEndpoint id="defaultHttpEndpoint"
httpPort="${env.WLP_HTTP_PORT}"
httpsPort="9443" />
Let's recreate the package and execute it as follows.
$ export WLP_HTTP_PORT=8880; java -jar /tmp/server1.jar
If you access it with a browser, you will see that the port number has changed.
WebSphere Liberty also allows Java EE applications to be distributed as One JAR files, making it easier to distribute web applications.
Recommended Posts