user3014595
user3014595

Reputation: 124

how to set jetty.home Embedded jetty with maven

I am using jetty-server:9.2.11 and I want to have a webapps directory for war deployment in my embedded jetty application like that we have when use standalone jetty server. I read some documentation and found "Like Jetty XML" programmatic configuration like below

    // Path to as-built jetty-distribution directory
    String jettyHomeBuild = "../../jetty-distribution/target/distribution";

    // Find jetty home and base directories
    String homePath = System.getProperty("jetty.home", jettyHomeBuild);
    File homeDir = new File(homePath);
    if (!homeDir.exists())
    {
        throw new FileNotFoundException(homeDir.getAbsolutePath());
    }
    String basePath = System.getProperty("jetty.base", homeDir + "/demo-base");
    File baseDir = new File(basePath);
    if(!baseDir.exists())
    {
        throw new FileNotFoundException(baseDir.getAbsolutePath());
    }

    // Configure jetty.home and jetty.base system properties
    String jetty_home = homeDir.getAbsolutePath();
    String jetty_base = baseDir.getAbsolutePath();
    System.setProperty("jetty.home", jetty_home);
    System.setProperty("jetty.base", jetty_base);







    // === jetty-deploy.xml ===
    DeploymentManager deployer = new DeploymentManager();
    deployer.setContexts(contexts);
    deployer.setContextAttribute(
            "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
            ".*/servlet-api-[^/]*\\.jar$");

    WebAppProvider webapp_provider = new WebAppProvider();
    webapp_provider.setMonitoredDirName(jetty_base + "/webapps");
    webapp_provider.setDefaultsDescriptor(jetty_home + "/etc/webdefault.xml");
    webapp_provider.setScanInterval(1);
    webapp_provider.setExtractWars(true);
    webapp_provider.setConfigurationManager(new PropertiesConfigurationManager());

    deployer.addAppProvider(webapp_provider);
    server.addBean(deployer);

But here uses "jetty-distribution" but I only have jetty related jar files in my lib directory that get copied as maven dependencies. I'm kind of stuck here to do my configuration to have "jetty.home" for my embedded jetty application because I'm not using any standard jetty distribution.I only have separate jars.

Upvotes: 2

Views: 2009

Answers (2)

user3014595
user3014595

Reputation: 124

with bit of research I filtered what I need for my purpose from jetty-like.xml and what is below is what we need to keep

    DeploymentManager deployer = new DeploymentManager();
    deployer.setContexts(contexts);
    deployer.setContextAttribute(
            "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
            ".*/servlet-api-[^/]*\\.jar$");

    WebAppProvider webapp_provider = new WebAppProvider();
    webapp_provider.setMonitoredDirName("webapps");
    webapp_provider.setScanInterval(1);
    webapp_provider.setExtractWars(true);
    webapp_provider.setConfigurationManager(new PropertiesConfigurationManager());

    deployer.addAppProvider(webapp_provider);
    server.addBean(deployer);

See here no need of jetty_base or jetty_home

webapp_provider.setMonitoredDirName("webapps");

Upvotes: 1

Joakim Erdfelt
Joakim Erdfelt

Reputation: 49472

jetty.home has very little meaning when using .

The LikeJettyXml is exactly as its name implies, its a java version of the jetty-distribution's etc/jetty-*.xml files, that's all it is and claims to be.

If you want to setup a webapps directory (aka a DeploymentManager with WebAppProvider) then do just that.

In order have some success with this, you'll need to understand quite a decent swathe of how Jetty operates and functions, with a focus on the DeploymentManager and WebAppContext.

While the LikeJettyXml shows you some of the features, it shows you it from the point of view of the jetty-distribution, not embedded-jetty.

For example:

  • the jetty-distribution has a etc/webdefault.xml that is being referenced by the setDefaultsDescriptor() call, but that's not required to be set, as the DeploymentManager can just use the same webdefault.xml that exists in the jar files instead.
  • the jetty-distribution has a jetty.base that can pre-build the ${jetty.base}/webapps directory when you enable the deploy module. That directory, the concept of a jetty.base, the idea and features of modules, and even the name webapps is all arbitrary, and doesn't need to exist that way for embedded-jetty. Make that directory anywhere you want it to be, name it whatever your heart desires.
  • scanInterval, extractWars, ConfigurationManager are all optional. in fact, the only configurations that are mandatory are the setMonitoredDirName and setContexts
  • setting the ContainerIncludeJarPattern isn't needed for most embedded-jetty setups (see other questions on stackoverflow for that that attribute does)
  • setting the system properties is also meaningless for embedded-jetty, nothing in embedded-jetty uses those properties (those properties exist solely for jetty-distribution, its start.jar, and the various ${jetty.home}/etc/*.xml files)

Hope this helps, and please take some time to really understand how deployments work in Jetty (the javadoc for DeploymentManager does a decent job explaining this).

Deployment can be big topic, so attempting to cover it all in a single stackoverflow answer is out of scope.

You'll want to understand:

  • The Jetty Component LifeCycle
  • The DeploymentManager role
  • The Deployment AppLifeCycle
  • The AppProvider role
  • The WebAppContext
  • The Server / WebAppContext Configuration classes
  • The WebAppContext Initialization LifeCycle

Upvotes: 1

Related Questions