1 //
2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
8 //
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
11 //
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
14 //
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
17 //
18
19 package org.eclipse.jetty.maven.plugin;
20
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.apache.maven.plugin.MojoExecutionException;
26 import org.apache.maven.plugin.MojoFailureException;
27 import org.eclipse.jetty.util.Scanner;
28
29 /**
30 * <p>
31 * This goal is used to assemble your webapp into a war and automatically deploy it to Jetty.
32 * </p>
33 * <p>
34 * Once invoked, the plugin runs continuously and can be configured to scan for changes in the project and to the
35 * war file and automatically perform a hot redeploy when necessary.
36 * </p>
37 * <p>
38 * You may also specify the location of a jetty.xml file whose contents will be applied before any plugin configuration.
39 * This can be used, for example, to deploy a static webapp that is not part of your maven build.
40 * </p>
41 *
42 * @goal run-war
43 * @requiresDependencyResolution compile+runtime
44 * @execute phase="package"
45 * @description Runs jetty on a war file
46 *
47 */
48 public class JettyRunWarMojo extends AbstractJettyMojo
49 {
50
51 /**
52 * The location of the war file.
53 * @parameter expression="${project.build.directory}/${project.build.finalName}.war"
54 * @required
55 */
56 private File war;
57
58
59 /**
60 * @see org.apache.maven.plugin.Mojo#execute()
61 */
62 public void execute() throws MojoExecutionException, MojoFailureException
63 {
64 super.execute();
65 }
66
67
68 @Override
69 public void finishConfigurationBeforeStart() throws Exception
70 {
71 server.setStopAtShutdown(true); //as we will normally be stopped with a cntrl-c, ensure server stopped
72 super.finishConfigurationBeforeStart();
73 }
74
75
76
77 public void configureWebApplication () throws Exception
78 {
79 super.configureWebApplication();
80
81 webApp.setWar(war.getCanonicalPath());
82 }
83
84
85
86
87 /**
88 * @see AbstractJettyMojo#checkPomConfiguration()
89 */
90 public void checkPomConfiguration() throws MojoExecutionException
91 {
92 return;
93 }
94
95
96
97
98 /**
99 * @see AbstractJettyMojo#configureScanner()
100 */
101 public void configureScanner() throws MojoExecutionException
102 {
103 scanList = new ArrayList();
104 scanList.add(project.getFile());
105 scanList.add(war);
106
107 scannerListeners = new ArrayList();
108 scannerListeners.add(new Scanner.BulkListener()
109 {
110 public void filesChanged(List changes)
111 {
112 try
113 {
114 boolean reconfigure = changes.contains(project.getFile().getCanonicalPath());
115 restartWebApp(reconfigure);
116 }
117 catch (Exception e)
118 {
119 getLog().error("Error reconfiguring/restarting webapp after change in watched files",e);
120 }
121 }
122 });
123 }
124
125
126
127
128 /**
129 * @see org.eclipse.jetty.maven.plugin.AbstractJettyMojo#restartWebApp(boolean)
130 */
131 public void restartWebApp(boolean reconfigureScanner) throws Exception
132 {
133 getLog().info("Restarting webapp ...");
134 getLog().debug("Stopping webapp ...");
135 webApp.stop();
136 getLog().debug("Reconfiguring webapp ...");
137
138 checkPomConfiguration();
139
140 // check if we need to reconfigure the scanner,
141 // which is if the pom changes
142 if (reconfigureScanner)
143 {
144 getLog().info("Reconfiguring scanner after change to pom.xml ...");
145 scanList.clear();
146 scanList.add(project.getFile());
147 scanList.add(war);
148 scanner.setScanDirs(scanList);
149 }
150
151 getLog().debug("Restarting webapp ...");
152 webApp.start();
153 getLog().info("Restart completed.");
154 }
155
156 }