Skip to content

Commit

Permalink
Jetty 12 : QuickStart generation based on Path, usage based on Resour…
Browse files Browse the repository at this point in the history
…ce (#8614)

* QuickStart generation based on Path, usage based on Resource
* QuickStart usage in jetty-maven-plugin uses Path
* Cleanup MavenWebAppContext constructor exceptions
* Improving quickStart stream filtering and array conversion
* Creating WEB-INF, if missing, now more reliable
  • Loading branch information
joakime authored Sep 23, 2022
1 parent fe2bcf9 commit 144c78f
Show file tree
Hide file tree
Showing 23 changed files with 301 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import java.util.stream.Collectors;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.AbstractMojoExecutionException;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Parameter;
import org.eclipse.jetty.util.resource.Resource;

Expand Down Expand Up @@ -103,18 +105,25 @@ protected void verifyPomConfiguration() throws MojoExecutionException
}

@Override
protected void configureWebApp() throws Exception
protected void configureWebApp() throws AbstractMojoExecutionException
{
super.configureWebApp();
configureUnassembledWebApp();
try
{
configureUnassembledWebApp();
}
catch (IOException e)
{
throw new MojoFailureException("Unable to configure unassembled webapp", e);
}
}

/**
* Configure a webapp that has not been assembled into a war.
*
* @throws Exception
* @throws IOException
*/
protected void configureUnassembledWebApp() throws Exception
protected void configureUnassembledWebApp() throws IOException
{
//Set up the location of the webapp.
//There are 2 parts to this: setWar() and setBaseResource(). On standalone jetty,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.AbstractMojoExecutionException;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
Expand Down Expand Up @@ -781,7 +782,7 @@ protected String getProjectName()
* @throws Exception
*/
protected void configureWebApp()
throws Exception
throws AbstractMojoExecutionException
{
if (webApp == null)
webApp = new MavenWebAppContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
package org.eclipse.jetty.ee10.maven.plugin;

import java.io.File;
import java.io.IOException;

import org.apache.maven.plugin.AbstractMojoExecutionException;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
Expand All @@ -36,21 +39,30 @@ public class JettyEffectiveWebXml extends AbstractUnassembledWebAppMojo
protected File effectiveWebXml;

@Override
public void configureWebApp() throws Exception
public void configureWebApp() throws AbstractMojoExecutionException
{
//Use a nominated war file for which to generate the effective web.xml, or
//if that is not set, try to use the details of the current project's
//unassembled webapp
super.configureWebApp();
if (StringUtil.isBlank(webApp.getWar()))
try
{
if (StringUtil.isBlank(webApp.getWar()))
{
super.configureUnassembledWebApp();
}
}
catch (IOException e)
{
throw new MojoFailureException("Unable to configure unassembled webapp", e);
}
}

/**
* Override so we can call the parent's method in a different order.
*/
@Override
protected void configureUnassembledWebApp() throws Exception
protected void configureUnassembledWebApp()
{
}

Expand All @@ -76,7 +88,7 @@ private void generate() throws MojoExecutionException
{
try
{
QuickStartGenerator generator = new QuickStartGenerator(effectiveWebXml, webApp);
QuickStartGenerator generator = new QuickStartGenerator(effectiveWebXml.toPath(), webApp);
generator.generate();
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public void setStopKey(String stopKey)
this.stopKey = stopKey;
}

public void setWebApp(MavenWebAppContext app) throws Exception
public void setWebApp(MavenWebAppContext app)
{
webApp = app;
}
Expand Down Expand Up @@ -279,7 +279,7 @@ private void configure() throws Exception
Path qs = webApp.getTempDirectory().toPath().resolve("quickstart-web.xml");
if (Files.exists(qs) && Files.isRegularFile(qs))
{
webApp.setAttribute(QuickStartConfiguration.QUICKSTART_WEB_XML, webApp.getResourceFactory().newResource(qs));
webApp.setAttribute(QuickStartConfiguration.QUICKSTART_WEB_XML, qs);
webApp.addConfiguration(new MavenQuickStartConfiguration());
webApp.setAttribute(QuickStartConfiguration.MODE, Mode.QUICKSTART);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
Expand Down Expand Up @@ -51,10 +50,10 @@ public class JettyForkedChild extends ContainerLifeCycle

/**
* @param args arguments that were passed to main
* @throws Exception
* @throws IOException if unable to configure
*/
public JettyForkedChild(String[] args)
throws Exception
throws IOException
{
jetty = new JettyEmbedder();
configure(args);
Expand All @@ -64,10 +63,10 @@ public JettyForkedChild(String[] args)
* Based on the args passed to the program, configure jetty.
*
* @param args args that were passed to the program.
* @throws Exception
* @throws IOException if unable to load webprops
*/
public void configure(String[] args)
throws Exception
throws IOException
{
Map<String, String> jettyProperties = new HashMap<>();

Expand Down Expand Up @@ -171,10 +170,9 @@ public void onPathWatchEvents(List<PathWatchEvent> events)
* present.
*
* @return file contents as properties
* @throws FileNotFoundException
* @throws IOException
*/
private Properties loadWebAppProps() throws FileNotFoundException, IOException
private Properties loadWebAppProps() throws IOException
{
Properties props = new Properties();
if (Objects.nonNull(webAppPropsFile))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ public void doStart()
throws Exception
{
//Run the webapp to create the quickstart file and properties file
generator = new QuickStartGenerator(forkWebXml, webApp);
generator = new QuickStartGenerator(forkWebXml.toPath(), webApp);
generator.setContextXml(contextXml);
generator.setWebAppPropsFile(webAppPropsFile);
generator.setWebAppProps(webAppPropsFile.toPath());
generator.setServer(server);
generator.generate();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Date;
import java.util.Set;

import org.apache.maven.plugin.AbstractMojoExecutionException;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Execute;
import org.apache.maven.plugins.annotations.LifecyclePhase;
Expand Down Expand Up @@ -67,7 +68,7 @@ public class JettyRunWarMojo extends AbstractWebAppMojo
protected Path war;

@Override
public void configureWebApp() throws Exception
public void configureWebApp() throws AbstractMojoExecutionException
{
super.configureWebApp();
//if no war has been explicitly configured, use the one from the webapp project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.File;
import java.nio.file.Path;

import org.apache.maven.plugin.AbstractMojoExecutionException;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Expand Down Expand Up @@ -59,7 +60,7 @@ public class JettyStartWarMojo extends AbstractWebAppMojo
protected JettyHomeForker homeForker;

@Override
public void configureWebApp() throws Exception
public void configureWebApp() throws AbstractMojoExecutionException
{
super.configureWebApp();
//if a war has not been explicitly configured, use the one from the project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public class MavenWebAppContext extends WebAppContext
*/
private boolean _baseAppFirst = true;

public MavenWebAppContext() throws Exception
public MavenWebAppContext()
{
super();
// Turn off copyWebInf option as it is not applicable for plugin.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public OverlayManager(WarPluginInfo warPlugin)
this.warPlugin = warPlugin;
}

public void applyOverlays(MavenWebAppContext webApp)
throws Exception
public void applyOverlays(MavenWebAppContext webApp) throws IOException
{
List<Resource> resourceBases = new ArrayList<Resource>();

Expand Down Expand Up @@ -71,8 +70,7 @@ public void applyOverlays(MavenWebAppContext webApp)
* Generate an ordered list of overlays
*/
protected List<Overlay> getOverlays()
throws Exception
{
{
Set<Artifact> matchedWarArtifacts = new HashSet<Artifact>();
List<Overlay> overlays = new ArrayList<Overlay>();

Expand Down Expand Up @@ -127,7 +125,7 @@ protected List<Overlay> getOverlays()
*/
protected Resource unpackOverlay(Overlay overlay)
throws IOException
{
{
if (overlay.getResource() == null)
return null; //nothing to unpack

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

package org.eclipse.jetty.ee10.maven.plugin;

import java.io.File;
import java.nio.file.Path;

import org.eclipse.jetty.ee10.annotations.AnnotationConfiguration;
import org.eclipse.jetty.ee10.quickstart.QuickStartConfiguration;
Expand All @@ -30,9 +30,9 @@
*/
public class QuickStartGenerator
{
private File quickstartXml;
private MavenWebAppContext webApp;
private File webAppPropsFile;
private final Path quickstartXml;
private final MavenWebAppContext webApp;
private Path webAppProps;
private String contextXml;
private boolean prepared = false;
private Server server;
Expand All @@ -42,10 +42,10 @@ public class QuickStartGenerator
* @param quickstartXml the file to generate quickstart into
* @param webApp the webapp for which to generate quickstart
*/
public QuickStartGenerator(File quickstartXml, MavenWebAppContext webApp)
public QuickStartGenerator(Path quickstartXml, MavenWebAppContext webApp)
{
this.quickstartXml = quickstartXml;
this.webApp = webApp;
this.webApp = webApp == null ? new MavenWebAppContext() : webApp;
}

/**
Expand All @@ -59,7 +59,7 @@ public MavenWebAppContext getWebApp()
/**
* @return the quickstartXml
*/
public File getQuickstartXml()
public Path getQuickstartXml()
{
return quickstartXml;
}
Expand All @@ -80,17 +80,17 @@ public void setServer(Server server)
this.server = server;
}

public File getWebAppPropsFile()
public Path getWebAppProps()
{
return webAppPropsFile;
return webAppProps;
}

/**
* @param webAppPropsFile properties file describing the webapp
* @param webAppProps properties file describing the webapp
*/
public void setWebAppPropsFile(File webAppPropsFile)
public void setWebAppProps(Path webAppProps)
{
this.webAppPropsFile = webAppPropsFile;
this.webAppProps = webAppProps;
}

public String getContextXml()
Expand All @@ -108,19 +108,13 @@ public void setContextXml(String contextXml)

/**
* Configure the webapp in preparation for quickstart generation.
*
* @throws Exception
*/
private void prepareWebApp()
throws Exception
{
if (webApp == null)
webApp = new MavenWebAppContext();

//set the webapp up to do very little other than generate the quickstart-web.xml
webApp.addConfiguration(new MavenQuickStartConfiguration());
webApp.setAttribute(QuickStartConfiguration.MODE, Mode.GENERATE);
webApp.setAttribute(QuickStartConfiguration.QUICKSTART_WEB_XML, webApp.getResourceFactory().newResource(quickstartXml.toPath()));
webApp.setAttribute(QuickStartConfiguration.QUICKSTART_WEB_XML, quickstartXml);
webApp.setAttribute(QuickStartConfiguration.ORIGIN_ATTRIBUTE, "o");
webApp.setCopyWebDir(false);
webApp.setCopyWebInf(false);
Expand All @@ -132,8 +126,7 @@ private void prepareWebApp()
*
* @throws Exception
*/
public void generate()
throws Exception
public void generate() throws Exception
{
if (quickstartXml == null)
throw new IllegalStateException("No quickstart xml output file");
Expand Down Expand Up @@ -173,8 +166,8 @@ public void generate()
webApp.start(); //just enough to generate the quickstart

//save config of the webapp BEFORE we stop
if (webAppPropsFile != null)
WebAppPropertyConverter.toProperties(webApp, webAppPropsFile, contextXml);
if (webAppProps != null)
WebAppPropertyConverter.toProperties(webApp, webAppProps.toFile(), contextXml);
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ public static void configureDefaultConfigurationClasses(Server server)
* @param server the server to use
* @param contextHandlers the context handlers to include
* @param requestLog a request log to use
* @throws Exception
*/
public static void configureHandlers(Server server, List<ContextHandler> contextHandlers, RequestLog requestLog) throws Exception
public static void configureHandlers(Server server, List<ContextHandler> contextHandlers, RequestLog requestLog)
{
if (server == null)
throw new IllegalArgumentException("Server is null");
Expand Down Expand Up @@ -148,9 +147,8 @@ public static void configureLoginServices(Server server, List<LoginService> logi
* Add a WebAppContext to a Server
* @param server the server to use
* @param webapp the webapp to add
* @throws Exception
*/
public static void addWebApplication(Server server, WebAppContext webapp) throws Exception
public static void addWebApplication(Server server, WebAppContext webapp)
{
if (server == null)
throw new IllegalArgumentException("Server is null");
Expand Down
Loading

0 comments on commit 144c78f

Please sign in to comment.