war 패 키 지 를 실행 할 수 있 습 니 다. 명령 행 에서 war 패 키 지 를 시작 합 니 다.
8276 단어 java -jar *.war
이번에 도 프로젝트 에 이런 방식 을 사 용 했 습 니 다. 여기 서 정리 해 보 겠 습 니 다.
시동 용기 로 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
자바 로. - jar. 이름. war 시작 가능