java -jar *.war 可运行war包方法

如果你用过hudson,肯定对它的启动方式印象深刻,它既可以用 java -jar *.war来启动,也可以放到web容器中启动。

这次在项目中也用到了这种方式,在这里总结一下,

内置了jetty作为启动容器,

启动类:

Java代码 

 收藏代码

  1. import java.io.File;  
  2. import java.net.URL;  
  3. import java.security.ProtectionDomain;  
  4.   
  5. import org.eclipse.jetty.server.Server;  
  6. import org.eclipse.jetty.webapp.WebAppContext;  
  7.   
  8. public class CompareLuncher {  
  9.     public static void main(String[] args) throws Exception {  
  10.         String currentPath=new File("").getAbsolutePath();  
  11.         //如果没有work目录,则创建,jetty默认解压路径  
  12.         File work=new File(currentPath+"\\work");  
  13.         if(!work.exists()){  
  14.             work.mkdir();  
  15.         }  
  16.         Server server =null;  
  17.         Integer port=8090;  
  18.         server=new Server(port);  
  19.         ProtectionDomain domain = CompareLuncher.class.getProtectionDomain();  
  20.         URL location = domain.getCodeSource().getLocation();  
  21.         WebAppContext webapp = new WebAppContext();  
  22.         webapp.setContextPath("/");  
  23.         webapp.setWar(location.toExternalForm());  
  24.         server.setHandler(webapp);  
  25.         server.start();  
  26.         server.join();  
  27.           
  28.         //启动部署包的时候可以用这个  
  29.         // // Server server = new Server(8080);  
  30.         // // WebAppContext context = new WebAppContext();  
  31.         // // context.setContextPath("/compare");  
  32.         // // context.setWar("F:/compare.war");  
  33.         // // server.setHandler(context);  
  34.         // // server.start();  
  35.         // // server.join();  
  36.   
  37.         //eclipse 测试的时候启用如下代码,debug模式下可以直接看到修改效果  
  38. //       Server server = new Server(8090);  
  39. ////           
  40. //       ResourceHandler resourceHandler = new ResourceHandler();    
  41. //       resourceHandler.setDirectoriesListed(true);    
  42. //            
  43. //       server.setSendServerVersion(true);  
  44. //       server.setStopAtShutdown(true);  
  45. //       server.setHandler(getWebAppContext());  
  46. //       server.start();  
  47. //       server.join();  
  48.   
  49.     }  
  50.   
  51.     private static WebAppContext getWebAppContext() {  
  52.   
  53.         String path = CompareLuncher.class.getResource("/").getFile()  
  54.                 .replaceAll("/target/(.*)", "")  
  55.                 + "/src/main/webapp";  
  56. //      System.out.println(path);  
  57.         String path2 = new File("").getAbsolutePath() + "\\src\\main\\webapp";  
  58.         // System.out.println();  
  59.   
  60.         return new WebAppContext(path2, "/");  
  61.     }  
  62. }  

 

pom:

Xml代码 

 收藏代码

  1.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  2.     4.0.0  
  3.     ssss  
  4.     pdfcompare  
  5.     war  
  6.     0.0.1-SNAPSHOT  
  7.     pdfcompare Maven Webapp  
  8.     http://maven.apache.org  
  9.       
  10.           
  11.             junit  
  12.             junit  
  13.             4.8.1  
  14.             test  
  15.           
  16.           
  17.             org.apache.pdfbox  
  18.             pdfbox  
  19.             1.8.2  
  20.           
  21.           
  22.   
  23.           
  24.             commons-fileupload  
  25.             commons-fileupload  
  26.             1.3  
  27.           
  28.           
  29.   
  30.           
  31.             org.eclipse.jetty  
  32.             jetty-server  
  33.             ${jettyVersion}  
  34.             provided  
  35.           
  36.           
  37.             org.eclipse.jetty  
  38.             jetty-webapp  
  39.             ${jettyVersion}  
  40.             provided  
  41.           
  42.           
  43.           
  44.             org.eclipse.jetty.orbit  
  45.             org.apache.jasper.glassfish  
  46.             2.2.2.v201112011158  
  47.             provided  
  48.           
  49.   
  50.           
  51.             org.eclipse.jetty.orbit  
  52.             javax.el  
  53.             2.2.0.v201108011116  
  54.             provided  
  55.           
  56.       
  57.       
  58.           
  59.               
  60.                 org.apache.maven.plugins  
  61.                 maven-war-plugin  
  62.                 2.3  
  63.                   
  64.                       
  65.                           
  66.                             CompareLuncher  
  67.                           
  68.                       
  69.                   
  70.               
  71.               
  72.                 org.apache.maven.plugins  
  73.                 maven-antrun-plugin  
  74.                 1.7  
  75.                   
  76.                       
  77.                         main-class-placement  
  78.                         prepare-package  
  79.                           
  80.                               
  81.                                   
  82.                                       
  83.                                           
  84.                                       
  85.                                   
  86.                               
  87.                           
  88.                           
  89.                             run  
  90.                           
  91.                       
  92.                   
  93.               
  94.               
  95.                 org.apache.maven.plugins  
  96.                 maven-dependency-plugin  
  97.                 2.5.1  
  98.                   
  99.                       
  100.                         jetty-classpath  
  101.                         prepare-package  
  102.                           
  103.                             unpack-dependencies  
  104.                           
  105.                           
  106.                             org.eclipse.jetty, org.eclipse.jetty.orbit  
  107.                             provided  
  108.                               
  109.                             *, about_files/*, META-INF/*  
  110.                               
  111.                                 ${project.build.directory}/${project.artifactId}  
  112.                               
  113.                           
  114.                       
  115.                   
  116.               
  117.           
  118.         pdfcompare  
  119.       
  120.       
  121.           
  122.         8.1.7.v20120910  
  123.       
  124.   

 最后 在 workspace/项目名的根文件夹下执行:mvn clean install,再到target文件夹下找到 项目名称.war

用 java -jar  名字.war 即可启动

你可能感兴趣的:(运维)