可运行war包,命令行启动war包

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

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

内置了jetty作为启动容器,

启动类:

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:

<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>CompareLuncher</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  名字.war 即可启动

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值