Apache Maven 3 : Plugins (Jetty) - 2020
bogotobogo.com site search:
Jetty
Continuing from Apache Maven 3 - Plugins (compiler).
In this chapter, we'll be focused on plugin features of Maven again. This time we'll try the Jetty plugin which extends the War plugin to add tasks which allow us to deploy our web application to a Jetty web container embedded in the build.
Let's start from our base directory:
k@laptop:~/java/apache-maven-3.2.5/bin/myapp/mywebapp$ ls pom.xml src target
To use Jetty, we need to let Maven know by putting the plugin into our pom.xml within build tag:
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.k.bogotobogo</groupId> <artifactId>mywebapp</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>mywebapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.4</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.10</version> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> <connectors> <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector"> <port>8008</port> <maxIdleTime>60000</maxIdleTime> </connector> </connectors> </configuration> </plugin> </plugins> </build> </project>
mvn jetty:run
Then start Jetty:
k@laptop:~/java/apache-maven-3.2.5/bin/myapp/mywebapp$ ls pom.xml src target k@laptop:~/java/apache-maven-3.2.5/bin/myapp/mywebapp$ mvn jetty:run [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building mywebapp 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] >>> maven-jetty-plugin:6.1.10:run (default-cli) @ mywebapp >>> [INFO] [INFO] --- maven-resources-plugin:2.3:resources (default-resources) @ mywebapp --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /home/k/java/apache-maven-3.2.5/bin/myapp/mywebapp/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:2.0.2:compile (default-compile) @ mywebapp --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.3:testResources (default-testResources) @ mywebapp --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /home/k/java/apache-maven-3.2.5/bin/myapp/mywebapp/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:2.0.2:testCompile (default-testCompile) @ mywebapp --- [INFO] No sources to compile [INFO] [INFO] <<< maven-jetty-plugin:6.1.10:run (default-cli) @ mywebapp <<< [INFO] [INFO] --- maven-jetty-plugin:6.1.10:run (default-cli) @ mywebapp --- [INFO] Configuring Jetty for project: mywebapp [INFO] Webapp source directory = /home/k/java/apache-maven-3.2.5/bin/myapp/mywebapp/src/main/webapp [INFO] web.xml file = /home/k/java/apache-maven-3.2.5/bin/myapp/mywebapp/src/main/webapp/WEB-INF/web.xml [INFO] Classes = /home/k/java/apache-maven-3.2.5/bin/myapp/mywebapp/target/classes 2015-02-01 18:27:42.765::INFO: Logging to STDERR via org.mortbay.log.StdErrLog [INFO] Context path = /mywebapp [INFO] Tmp directory = determined at runtime [INFO] Web defaults = org/mortbay/jetty/webapp/webdefault.xml [INFO] Web overrides = none [INFO] Webapp directory = /home/k/java/apache-maven-3.2.5/bin/myapp/mywebapp/src/main/webapp [INFO] Starting jetty 6.1.10 ... 2015-02-01 18:27:42.092::INFO: jetty-6.1.10 2015-02-01 18:27:43.425::INFO: No Transaction manager found - if your webapp requires one, please configure one. 2015-02-01 18:27:43.047::INFO: Started SelectChannelConnector@0.0.0.0:8008 [INFO] Started Jetty Server [INFO] Starting scanner at interval of 10 seconds.
Deploy
Open a browser, and type in localhost:8008:
Note that jetty does not have Welcome page. So, this time, we need to append mywebapp to the previous url:
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization