1. 程式人生 > >行war包,命令列啟動war包

行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. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>ssss</groupId>  
  5.     <artifactId>pdfcompare</artifactId>  
  6.     <packaging>war</packaging>  
  7.     <version>0.0.1-SNAPSHOT</version>  
  8.     <name>pdfcompare Maven Webapp</name>  
  9.     <url>http://maven.apache.org</url>  
  10.     <dependencies>  
  11.         <dependency>  
  12.             <groupId>junit</groupId>  
  13.             <artifactId>junit</artifactId>  
  14.             <version>4.8.1</version>  
  15.             <scope>test</scope>  
  16.         </dependency>  
  17.         <dependency>  
  18.             <groupId>org.apache.pdfbox</groupId>  
  19.             <artifactId>pdfbox</artifactId>  
  20.             <version>1.8.2</version>  
  21.         </dependency>  
  22.         <!-- <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-kernel</artifactId>   
  23.             <version>1.6.2</version> </dependency> -->  
  24.   
  25.         <dependency>  
  26.             <groupId>commons-fileupload</groupId>  
  27.             <artifactId>commons-fileupload</artifactId>  
  28.             <version>1.3</version>  
  29.         </dependency>  
  30.         <!-- Jetty -->  
  31.   
  32.         <dependency>  
  33.             <groupId>org.eclipse.jetty</groupId>  
  34.             <artifactId>jetty-server</artifactId>  
  35.             <version>${jettyVersion}</version>  
  36.             <scope>provided</scope>  
  37.         </dependency>  
  38.         <dependency>  
  39.             <groupId>org.eclipse.jetty</groupId>  
  40.             <artifactId>jetty-webapp</artifactId>  
  41.             <version>${jettyVersion}</version>  
  42.             <scope>provided</scope>  
  43.         </dependency>  
  44.         <!-- the dependency can be commented if no jsp -->  
  45.         <dependency>  
  46.             <groupId>org.eclipse.jetty.orbit</groupId>  
  47.             <artifactId>org.apache.jasper.glassfish</artifactId>  
  48.             <version>2.2.2.v201112011158</version>  
  49.             <scope>provided</scope>  
  50.         </dependency>  
  51.   
  52.         <dependency>  
  53.             <groupId>org.eclipse.jetty.orbit</groupId>  
  54.             <artifactId>javax.el</artifactId>  
  55.             <version>2.2.0.v201108011116</version>  
  56.             <scope>provided</scope>  
  57.         </dependency>  
  58.     </dependencies>  
  59.     <build>  
  60.         <plugins>  
  61.             <plugin>  
  62.                 <groupId>org.apache.maven.plugins</groupId>  
  63.                 <artifactId>maven-war-plugin</artifactId>  
  64.                 <version>2.3</version>  
  65.                 <configuration>  
  66.                     <archive>  
  67.                         <manifest>  
  68.                             <mainClass><span style="color: #ff0000;">CompareLuncher</span></mainClass>  
  69.                         </manifest>  
  70.                     </archive>  
  71.                 </configuration>  
  72.             </plugin>  
  73.             <plugin>  
  74.                 <groupId>org.apache.maven.plugins</groupId>  
  75.                 <artifactId>maven-antrun-plugin</artifactId>  
  76.                 <version>1.7</version>  
  77.                 <executions>  
  78.                     <execution>  
  79.                         <id>main-class-placement</id>  
  80.                         <phase>prepare-package</phase>  
  81.                         <configuration>  
  82.                             <target>  
  83.                                 <move todir="${project.build.directory}/${project.artifactId}/">  
  84.                                     <fileset dir="${project.build.directory}/classes/">  
  85.                                         <include name="CompareLuncher.class" />  
  86.                                     </fileset>  
  87.                                 </move>  
  88.                             </target>  
  89.                         </configuration>  
  90.                         <goals>  
  91.                             <goal>run</goal>  
  92.                         </goals>  
  93.                     </execution>  
  94.                 </executions>  
  95.             </plugin>  
  96.             <plugin>  
  97.                 <groupId>org.apache.maven.plugins</groupId>  
  98.                 <artifactId>maven-dependency-plugin</artifactId>  
  99.                 <version>2.5.1</version>  
  100.                 <executions>  
  101.                     <execution>  
  102.                         <id>jetty-classpath</id>  
  103.                         <phase>prepare-package</phase>  
  104.                         <goals>  
  105.                             <goal>unpack-dependencies</goal>  
  106.                         </goals>  
  107.                         <configuration>  
  108.                             <includeGroupIds>org.eclipse.jetty, org.eclipse.jetty.orbit</includeGroupIds>  
  109.                             <includeScope>provided</includeScope>  
  110.                             <!-- remove some files in order to decrease size -->  
  111.                             <excludes>*, about_files/*, META-INF/*</excludes>  
  112.                             <outputDirectory>  
  113.                                 ${project.build.directory}/${project.artifactId}  
  114.                             </outputDirectory>  
  115.                         </configuration>  
  116.                     </execution>  
  117.                 </executions>  
  118.             </plugin>  
  119.         </plugins>  
  120.         <finalName>pdfcompare</finalName>  
  121.     </build>  
  122.     <properties>  
  123.         <!-- <jettyVersion>7.3.0.v20110203</jettyVersion> -->  
  124.         <jettyVersion>8.1.7.v20120910</jettyVersion>  
  125.     </properties>  
  126. </project>  

 最後 在 workspace/專案名的根資料夾下執行:mvn clean install,再到target資料夾下找到 專案名稱.war

用 java -jar