行war包,命令列啟動war包
阿新 • • 發佈:2019-05-19
如果你用過hudson,肯定對它的啟動方式印象深刻,它既可以用 java -jar *.war來啟動,也可以放到web容器中啟動。
這次在專案中也用到了這種方式,在這裡總結一下,
內建了jetty作為啟動容器,
啟動類:
Java程式碼
- import java.io.File;
- import java.net.URL;
- import java.security.ProtectionDomain;
- import org.eclipse.jetty.server.Server;
- import org.eclipse.jetty.webapp.WebAppContext;
- public class CompareLuncher {
- public static void main(String[] args) throws Exception {
- String currentPath=new File("").getAbsolutePath();
- //如果沒有work目錄,則建立,jetty預設解壓路徑
- File work=new File(currentPath+"\\work");
- if(!work.exists()){
- work.mkdir();
- }
- Server server =null;
- Integer port=8090;
- server=new Server(port);
- ProtectionDomain domain = CompareLuncher.class.getProtectionDomain();
- URL location = domain.getCodeSource().getLocation();
- WebAppContext webapp = new WebAppContext();
- webapp.setContextPath("/");
- webapp.setWar(location.toExternalForm());
- server.setHandler(webapp);
- server.start();
- server.join();
- //啟動部署包的時候可以用這個
- // // Server server = new Server(8080);
- // // WebAppContext context = new WebAppContext();
- // // context.setContextPath("/compare");
- // // context.setWar("F:/compare.war");
- // // server.setHandler(context);
- // // server.start();
- // // server.join();
- //eclipse 測試的時候啟用如下程式碼,debug模式下可以直接看到修改效果
- // Server server = new Server(8090);
- ////
- // ResourceHandler resourceHandler = new ResourceHandler();
- // resourceHandler.setDirectoriesListed(true);
- //
- // server.setSendServerVersion(true);
- // server.setStopAtShutdown(true);
- // server.setHandler(getWebAppContext());
- // server.start();
- // server.join();
- }
- private static WebAppContext getWebAppContext() {
- String path = CompareLuncher.class.getResource("/").getFile()
- .replaceAll("/target/(.*)", "")
- + "/src/main/webapp";
- // System.out.println(path);
- String path2 = new File("").getAbsolutePath() + "\\src\\main\\webapp";
- // System.out.println();
- return new WebAppContext(path2, "/");
- }
- }
pom:
Xml程式碼
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>ssss</groupId>
- <artifactId>pdfcompare</artifactId>
- <packaging>war</packaging>
- <version>0.0.1-SNAPSHOT</version>
- <name>pdfcompare Maven Webapp</name>
- <url>http://maven.apache.org</url>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.8.1</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.apache.pdfbox</groupId>
- <artifactId>pdfbox</artifactId>
- <version>1.8.2</version>
- </dependency>
- <!-- <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-kernel</artifactId>
- <version>1.6.2</version> </dependency> -->
- <dependency>
- <groupId>commons-fileupload</groupId>
- <artifactId>commons-fileupload</artifactId>
- <version>1.3</version>
- </dependency>
- <!-- Jetty -->
- <dependency>
- <groupId>org.eclipse.jetty</groupId>
- <artifactId>jetty-server</artifactId>
- <version>${jettyVersion}</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>org.eclipse.jetty</groupId>
- <artifactId>jetty-webapp</artifactId>
- <version>${jettyVersion}</version>
- <scope>provided</scope>
- </dependency>
- <!-- the dependency can be commented if no jsp -->
- <dependency>
- <groupId>org.eclipse.jetty.orbit</groupId>
- <artifactId>org.apache.jasper.glassfish</artifactId>
- <version>2.2.2.v201112011158</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>org.eclipse.jetty.orbit</groupId>
- <artifactId>javax.el</artifactId>
- <version>2.2.0.v201108011116</version>
- <scope>provided</scope>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-war-plugin</artifactId>
- <version>2.3</version>
- <configuration>
- <archive>
- <manifest>
- <mainClass><span style="color: #ff0000;">CompareLuncher</span></mainClass>
- </manifest>
- </archive>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-antrun-plugin</artifactId>
- <version>1.7</version>
- <executions>
- <execution>
- <id>main-class-placement</id>
- <phase>prepare-package</phase>
- <configuration>
- <target>
- <move todir="${project.build.directory}/${project.artifactId}/">
- <fileset dir="${project.build.directory}/classes/">
- <include name="CompareLuncher.class" />
- </fileset>
- </move>
- </target>
- </configuration>
- <goals>
- <goal>run</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-dependency-plugin</artifactId>
- <version>2.5.1</version>
- <executions>
- <execution>
- <id>jetty-classpath</id>
- <phase>prepare-package</phase>
- <goals>
- <goal>unpack-dependencies</goal>
- </goals>
- <configuration>
- <includeGroupIds>org.eclipse.jetty, org.eclipse.jetty.orbit</includeGroupIds>
- <includeScope>provided</includeScope>
- <!-- remove some files in order to decrease size -->
- <excludes>*, about_files/*, META-INF/*</excludes>
- <outputDirectory>
- ${project.build.directory}/${project.artifactId}
- </outputDirectory>
- </configuration>
- </execution>
- </executions>
- </plugin>
- </plugins>
- <finalName>pdfcompare</finalName>
- </build>
- <properties>
- <!-- <jettyVersion>7.3.0.v20110203</jettyVersion> -->
- <jettyVersion>8.1.7.v20120910</jettyVersion>
- </properties>
- </project>
最後 在 workspace/專案名的根資料夾下執行:mvn clean install,再到target資料夾下找到 專案名稱.war
用 java -jar