1 package org.apache.maven.plugins.site.run;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.doxia.siterenderer.DocumentRenderer;
23 import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
24 import org.apache.maven.plugin.MojoExecutionException;
25 import org.apache.maven.plugin.MojoFailureException;
26 import org.apache.maven.plugins.annotations.Mojo;
27 import org.apache.maven.plugins.annotations.Parameter;
28 import org.apache.maven.plugins.site.render.AbstractSiteRenderingMojo;
29 import org.apache.maven.reporting.exec.MavenReportExecution;
30 import org.codehaus.plexus.util.IOUtil;
31 import org.eclipse.jetty.server.Server;
32 import org.eclipse.jetty.webapp.WebAppContext;
33
34 import static org.apache.maven.shared.utils.logging.MessageUtils.buffer;
35
36 import java.io.File;
37 import java.io.FileOutputStream;
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.util.HashMap;
41 import java.util.List;
42 import java.util.Locale;
43 import java.util.Map;
44
45
46
47
48
49
50
51
52 @Mojo( name = "run", aggregator = true, requiresReports = true )
53 public class SiteRunMojo
54 extends AbstractSiteRenderingMojo
55 {
56
57
58
59 @Parameter( defaultValue = "${project.build.directory}/site-webapp" )
60 private File tempWebappDirectory;
61
62
63
64
65 @Parameter( property = "port", defaultValue = "8080" )
66 private int port;
67
68
69
70
71 public void execute()
72 throws MojoExecutionException, MojoFailureException
73 {
74 checkInputEncoding();
75
76 Server server = new Server( port );
77 server.setStopAtShutdown( true );
78
79 WebAppContext webapp = createWebApplication();
80 webapp.setServer( server );
81
82 server.setHandler( webapp );
83
84 getLog().info( buffer().a( "Starting Jetty on " ).strong( "http://localhost:" + port + "/" ).toString() );
85 try
86 {
87 server.start();
88 }
89 catch ( Exception e )
90 {
91 throw new MojoExecutionException( "Error executing Jetty: " + e.getMessage(), e );
92 }
93
94
95 try
96 {
97 server.getThreadPool().join();
98 }
99 catch ( InterruptedException e )
100 {
101 getLog().warn( "Jetty was interrupted", e );
102 }
103 }
104
105 private WebAppContext createWebApplication()
106 throws MojoExecutionException
107 {
108 File webXml = new File( tempWebappDirectory, "WEB-INF/web.xml" );
109 webXml.getParentFile().mkdirs();
110
111
112 try ( InputStream inStream = getClass().getResourceAsStream( "/run/web.xml" );
113 FileOutputStream outStream = new FileOutputStream( webXml ) )
114 {
115 IOUtil.copy( inStream, outStream );
116 }
117 catch ( IOException e )
118 {
119 throw new MojoExecutionException( "Unable to construct temporary webapp for running site", e );
120 }
121
122 WebAppContext webapp = new WebAppContext();
123 webapp.setContextPath( "/" );
124 webapp.setResourceBase( tempWebappDirectory.getAbsolutePath() );
125 webapp.setAttribute( DoxiaFilter.SITE_RENDERER_KEY, siteRenderer );
126 webapp.getInitParams().put( "org.mortbay.jetty.servlet.Default.useFileMappedBuffer", "false" );
127
128
129 project.getReporting().setOutputDirectory( tempWebappDirectory.getAbsolutePath() );
130 for ( MavenReportExecution mavenReportExecution : getReports() )
131 {
132 mavenReportExecution.getMavenReport().setReportOutputDirectory( tempWebappDirectory );
133 }
134
135 List<MavenReportExecution> reports = getReports();
136
137 List<Locale> localesList = getLocales();
138 webapp.setAttribute( DoxiaFilter.LOCALES_LIST_KEY, localesList );
139
140
141 Locale defaultLocale = localesList.get( 0 );
142
143 try
144 {
145 Map<String, DoxiaBean> i18nDoxiaContexts = new HashMap<>();
146
147 for ( Locale locale : localesList )
148 {
149 SiteRenderingContext i18nContext = createSiteRenderingContext( locale );
150 i18nContext.setInputEncoding( getInputEncoding() );
151 i18nContext.setOutputEncoding( getOutputEncoding() );
152
153 SiteRenderingContext i18nGeneratedSiteContext = createSiteRenderingContext( locale );
154 i18nGeneratedSiteContext.setInputEncoding( getInputEncoding() );
155 i18nGeneratedSiteContext.setOutputEncoding( getOutputEncoding() );
156 i18nGeneratedSiteContext.getSiteDirectories().clear();
157
158 Map<String, DocumentRenderer> i18nDocuments = locateDocuments( i18nContext, reports, locale );
159 DoxiaBean doxiaBean;
160 if ( defaultLocale.equals( locale ) )
161 {
162 i18nGeneratedSiteContext.addSiteDirectory( generatedSiteDirectory );
163 doxiaBean = new DoxiaBean( i18nContext, i18nDocuments, i18nGeneratedSiteContext );
164 }
165 else
166 {
167 i18nGeneratedSiteContext.addSiteDirectory( new File( generatedSiteDirectory,
168 locale.toString() ) );
169 doxiaBean = new DoxiaBean( i18nContext, i18nDocuments, i18nGeneratedSiteContext );
170 }
171
172 i18nDoxiaContexts.put( locale.toString(), doxiaBean );
173 if ( defaultLocale.equals( locale ) )
174 {
175 i18nDoxiaContexts.put( "default", doxiaBean );
176 }
177
178 if ( defaultLocale.equals( locale ) )
179 {
180 siteRenderer.copyResources( i18nContext, tempWebappDirectory );
181 }
182 else
183 {
184 siteRenderer.copyResources( i18nContext, new File( tempWebappDirectory, locale.toString() ) );
185 }
186 }
187
188 webapp.setAttribute( DoxiaFilter.I18N_DOXIA_CONTEXTS_KEY, i18nDoxiaContexts );
189 }
190 catch ( Exception e )
191 {
192 throw new MojoExecutionException( "Unable to set up webapp", e );
193 }
194 return webapp;
195 }
196
197 public void setTempWebappDirectory( File tempWebappDirectory )
198 {
199 this.tempWebappDirectory = tempWebappDirectory;
200 }
201
202 public void setPort( int port )
203 {
204 this.port = port;
205 }
206 }