WAR and EAR file format

来源:互联网 发布:闲鱼.淘宝二手 编辑:程序博客网 时间:2024/06/05 00:31

看Jenkins的时候,安装运行Jenkins时用的是WAR包,所以我就看看什么是war包。

http://en.wikipedia.org/wiki/WAR_file_format_%28Sun%29


In computing, a WAR file (or Web applicationARchive) is aJAR file used to distribute a collection of JavaServer Pages, Java Servlets, Javaclasses, XML files, tag libraries and static Web pages (HTML and related files) that togetherconstitute a Web application.


所以Jenkins使用war非常合理,因为Jenkins是一个Web application。而There are special files and directories within a WAR file.


The /WEB-INF directory in the WAR file contains a file named web.xml which defines the structure of the web application. If the web application is only serving JSP files, the web.xml file is not strictly necessary. If the web application uses servlets, then the servlet container uses web.xml to ascertain to which servlet a URL request is to be routed. web.xml is also used to define context variables which can be referenced within the servlets and it is used to define environmental dependencies which the deployer is expected to set up. An example of this is a dependency on a mail session used to send email. The servlet container is responsible for providing this service.

因此web.xml非常重要,下面是例子:

 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE web-app
     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
     "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
 
 <web-app>
     <servlet>
         <servlet-name>HelloServlet</servlet-name>
         <servlet-class>mypackage.HelloServlet</servlet-class>
     </servlet>
 
     <servlet-mapping>
         <servlet-name>HelloServlet</servlet-name>
         <url-pattern>/HelloServlet</url-pattern>
     </servlet-mapping>
 <!--上面就是前面说的:the servlet container uses web.xml to ascertain to which servlet a URL request is to be routed

当用户在URL输入.../HelloServlet时,就知道请求的是servlet HelloServlet,同时背后应执行的java类是mypackage.HelloServlet-->
     <resource-ref>
         <description>
             Resource reference to a factory for javax.mail.Session
             instances that may be used for sending electronic mail messages,
             preconfigured to connect to the appropriate SMTP server.
         </description>
         <res-ref-name>mail/Session</res-ref-name>
         <res-type>javax.mail.Session</res-type>
         <res-auth>Container</res-auth>
     </resource-ref>
 </web-app>

The /WEB-INF/classes directory is on the ClassLoader's classpath. This is where .class files are loaded from when the web application is executing. Any JAR files placed in the /WEB-INF/lib directory will also be placed on the ClassLoader's classpath.

Advantages of WAR files:

  • security - no one can modify the deployment, once the war file is signed and deployed
  • easy development, testing and deployment
  • the version of the deployed application is easily identified
  • all J2EE containers support .WAR files

One disadvantage of web deployment using WAR files in very dynamic environments is that minor changes cannot be made during runtime. Any change whatsoever requires regenerating and redeploying the entire WAR file.


总之这个wiki写的很精华,值得一看。


再来看看EAR

http://en.wikipedia.org/wiki/EAR_%28file_format%29

EAR (Enterprise Archive) is a file format used byJava EE for packaging one or more modules into a single archive so that the deployment of the variousmodules onto an application server happens simultaneously and coherently. It also contains XML files calleddeployment descriptors which describe how to deploy the modules.

Ant or Maven can be used to build EAR files.

An EAR file is a standard JAR file (and therefore a Zip file) with a .ear extension, with one or more entries representing the modules of the application, and a metadata directory calledMETA-INF which contains one or more deployment descriptors.



原创粉丝点击