java-school logo
Last Modified 2018.8.30

Jetty Maven Plugin

Jetty Maven Plugin을 사용하면 서버 설치없이 웹 앱을 테스트할 수 있다.

메이븐 아키타입을 생성한다.

C:\ Command Prompt
mvn archetype:generate -Dfilter=maven-archetype-webapp

생성한 메이븐 프로젝트를 Import - Import - Existing Maven Project 선택하여 이클립스에 임포트한다.

web.xml 파일을 열고 아래와 같이 수정한다.

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  version="3.1"
  metadata-complete="true">
  
</web-app>

pom.xml 파일을 아래와 같이 수정한다.

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>net.java_school</groupId>
	<artifactId>examples</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>examples</name>
	<url>http://maven.apache.org</url>

	<properties>
		<jdk.version>1.8</jdk.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<!-- Servlet JSP JSTL -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>javax.servlet.jsp-api</artifactId>
			<version>2.3.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
	</dependencies>

	<build>
		<finalName>examples</finalName>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-compiler-plugin</artifactId>
					<version>3.6.2</version>
					<configuration>
						<source>${jdk.version}</source>
						<target>${jdk.version}</target>
						<encoding>UTF-8</encoding>
					</configuration>
				</plugin>
				<plugin>
					<groupId>org.eclipse.jetty</groupId>
					<artifactId>jetty-maven-plugin</artifactId>
					<version>9.4.6.v20170531</version>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>

</project>

https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-maven-plugin 에서 가장 최신 jetty-maven-plugin 배포본을 선택하고 의존성이 아닌 플러그인에 추가한다. (위에서 강조한 부분 참조)

jetty를 실행한다.

C:\ Command Prompt
mvn jetty:run

http://localhost:8080을 방문하여 Hello World!를 보면 성공이다.

실행에 아무런 문제가 없으나 이클립스에서 보이는 에러는, Project Explorer 뷰에서 프로젝트 선택 - 마우스 오른쪽 버튼 클릭 - Properties - Project Facets을 선택해서 Java 버전을 1.8로, Dynamic Web Module 버전을 3.1으로 바꾸면, 사라진다. (윈도에서는 한 번에 하나씩만 바꿀 수 있다.)

참고 관련 글