`

可运行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 即可启动

分享到:
评论
1 楼 dotku 2016-06-22  
运行 mvn clean install 出现报错信息

> An Ant BuildException has occured

完整报错信息

> [ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.7:run (main-class-placement) on project pdfcompare: An Ant BuildException has occured: C:\www\dotku.github.io\tech\java-maven\demo-war-running\target\classes does not exist.

看看...?

相关推荐

    金蝶容器重新部署war包过程详细阐述

    4. 修改项目的部署方式:在文档中提到,原先的部署方式是直接启动war包,而现在采取的是通过外部解压缩war包内容,然后拷贝到applications目录下。这种方式可能是因为需要更灵活地管理项目文件,或者更细致地处理...

    springboot 打包war包

    服务器会自动解压WAR包,并启动其中的Spring Boot应用。注意,由于Spring Boot的特性,即使打包为WAR,它仍然可以作为一个独立的应用运行,无需额外的容器。 总结来说,通过Maven和Spring Boot,我们可以轻松地管理...

    weblogic部署测试war包

    测试WebLogic和Tomcat的WAR包部署能力可以帮助开发者了解服务器环境的兼容性,确保应用能在不同环境下正常运行。同时,这也有助于在生产环境中优化配置,提高服务的稳定性和效率。 总之,理解并熟练掌握WAR包在...

    jar/war包反编译工具

    "jar/war包反编译工具"是指能够将jar或war文件中的字节码转换回可读的源代码的软件。这在调试、学习开源项目、逆向工程或检查版权问题时非常有用。其中,jd-gui.exe是一个常用的Java反编译器,它提供了一个图形用户...

    spring boot项目打包成war在tomcat运行的全步骤

    在开发基于Spring Boot的应用时,通常默认的打包方式是生成可独立运行的JAR文件,因为Spring Boot内置了一个轻量级的Tomcat服务器。然而,有些场景下可能需要将Spring Boot应用部署到传统的Tomcat服务器上,这时就...

    war包解压工具

    6. 重新打包:最后,你可以使用7-Zip或其他工具(如`jar`命令行工具)将修改后的文件重新打包成`WAR`格式。 除了7-Zip,还有其他解压缩工具,如WinRAR、Zip、Unzip等,也支持`WAR`文件的操作。但需要注意的是,不是...

    Jenkins的war包

    Jenkins的war包是Jenkins项目的核心组成部分,它是一个可执行的Java Web应用程序,通常用于在服务器上部署和运行Jenkins服务。Jenkins作为一个开源软件项目,它的目标是提供一个灵活且用户友好的持续集成(CI)平台...

    dubbox2.8.4 admin monitor war包和部署

    将war包复制到服务器的`webapps`目录下,Tomcat会自动解压并启动应用。 4. **配置应用服务器**:可能需要在服务器的`server.xml`配置文件中添加Dubbo的相关配置,例如设置Zookeeper地址,以便admin模块能正确地发现...

    jenkins直接启动war包

    本文将详细介绍如何在Windows和Linux系统上通过直接启动WAR包来运行Jenkins,并提供查询管理员密码的方法。 ### Jenkins WAR包启动 1. **下载Jenkins WAR文件**: 首先,你需要从Jenkins官方网站...

    jenkins war包

    - 启动服务器,它会自动解压WAR包并开始运行Jenkins服务。 - 访问服务器的默认端口(通常是8080),按照向导完成初始设置,如创建管理员密码、选择需要安装的插件等。 4. **Jenkins WAR包的优势** - 易于部署:...

    iServer7C正式版war包在linux上的部署

    - 使用命令行工具对war包进行解压缩,例如通过`chmod 755 smiserver_700_10804_1790_chs_war.zip`设置权限,然后使用`unzip smiserver_700_10804_1790_chs_war.zip`解压缩war包。 **步骤2:解压并配置文件** - 解...

    shell脚本发布jar包服务,war包服务

    这篇博客文章标题为“shell脚本发布jar包服务,war包服务”,意味着它将介绍如何使用shell脚本来部署和管理Java应用程序。以下是基于这个主题的详细知识点: 1. **Shell脚本**: - Shell脚本是一种用bash或其他...

    金蝶容器Apusic中重新部署war包

    只需停止新应用,删除新部署的WAR包,然后将备份的WAR包恢复到部署目录,并启动应用。 以上就是在金蝶容器Apusic中重新部署WAR包的详细步骤。理解并熟练掌握这些步骤,对于确保企业应用的稳定性和高效运维至关重要...

    solor检索war包

    【标题】"solor检索war包"涉及到的核心技术是Apache Solr,这是一个开源的企业级全文搜索引擎,被广泛用于构建高效、可扩展的搜索解决方案。在本场景中,"war包"指的是Web应用程序归档文件,它是Java Web应用的标准...

    axis2的war包、项目实例

    Axis2的WAR包是预编译的Web应用,可以直接部署到支持Servlet容器(如Tomcat、Jetty)中运行。 2. **Axis2 WAR包的内容** Axis2的WAR包通常包含以下组件: - **必备的JAR文件**:这些文件提供了Axis2的核心功能,...

    打包war

    【标题】:“打包war” 在IT行业中,"打包war"是指...8. **部署**:将打包好的WAR文件上传至Web服务器并启动应用的流程。 这篇博客文章可能详细地介绍了以上各个知识点,帮助读者掌握Java Web应用的打包和部署技巧。

    axis2-1.6.2的war包与bin包以及使用文档

    2. 对于war包,将其复制到你的Web服务器的webapps目录下,服务器会自动解压并启动Axis2实例。 3. 对于bin包,进入bin目录,根据提供的脚本启动和管理Axis2服务。 4. 查阅文档,了解如何编写Web服务代码,以及如何...

    war包添加配置文件设置.docx

    这个过程涉及到Spring Boot的自定义环境处理器(EnvironmentPostProcessor)和WAR包的构建配置。以下是如何实现这一目标的详细步骤: 1. **创建自定义环境处理器**: 在项目中的`config`包下,创建名为`...

Global site tag (gtag.js) - Google Analytics