diff --git a/jetty-core/jetty-deploy/src/main/java/org/eclipse/jetty/deploy/providers/ContextProvider.java b/jetty-core/jetty-deploy/src/main/java/org/eclipse/jetty/deploy/providers/ContextProvider.java index 4f19a9cf523..bebd2ab3641 100644 --- a/jetty-core/jetty-deploy/src/main/java/org/eclipse/jetty/deploy/providers/ContextProvider.java +++ b/jetty-core/jetty-deploy/src/main/java/org/eclipse/jetty/deploy/providers/ContextProvider.java @@ -501,7 +501,7 @@ public class ContextProvider extends ScanningAppProvider !Files.exists(path.getParent().resolve(basename + ".WAR")); } - // deploy if there is not a .war for of the same basename + // deploy if it is a .war and there is not a .xml for of the same basename if (FileID.isWebArchive(path)) { // if a .xml file exists for it @@ -519,4 +519,11 @@ public class ContextProvider extends ScanningAppProvider if (isDeployable(path)) super.pathAdded(path); } + + @Override + protected void pathChanged(Path path) throws Exception + { + if (isDeployable(path)) + super.pathChanged(path); + } } diff --git a/jetty-core/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/providers/ContextProviderRuntimeUpdatesTest.java b/jetty-core/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/providers/ContextProviderRuntimeUpdatesTest.java index 2c7561fe2c3..977db71608e 100644 --- a/jetty-core/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/providers/ContextProviderRuntimeUpdatesTest.java +++ b/jetty-core/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/providers/ContextProviderRuntimeUpdatesTest.java @@ -16,12 +16,14 @@ package org.eclipse.jetty.deploy.providers; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.attribute.FileTime; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import org.eclipse.jetty.deploy.AppProvider; import org.eclipse.jetty.deploy.DeploymentManager; import org.eclipse.jetty.deploy.test.XmlConfiguredJetty; +import org.eclipse.jetty.server.handler.ContextHandler; import org.eclipse.jetty.toolchain.test.FS; import org.eclipse.jetty.toolchain.test.jupiter.WorkDir; import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension; @@ -34,6 +36,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertSame; /** * Similar in scope to {@link ContextProviderStartupTest}, except is concerned with the modification of existing @@ -56,6 +62,11 @@ public class ContextProviderRuntimeUpdatesTest FS.ensureDirExists(resourceBase); jetty.setProperty("test.bar.resourceBase", resourceBase.toUri().toASCIIString()); + Path tmpBase = jetty.getJettyBasePath().resolve("tmp"); + FS.ensureDirExists(tmpBase); + jetty.setProperty("test.tmpBase", tmpBase.toFile().getAbsolutePath()); + System.err.println(tmpBase.toFile().getAbsolutePath()); + Files.writeString(resourceBase.resolve("text.txt"), "This is the resourceBase text"); Path resourceBaseAlt = jetty.getJettyBasePath().resolve("resourceBase-alt"); @@ -110,6 +121,42 @@ public class ContextProviderRuntimeUpdatesTest await().atMost(5, TimeUnit.SECONDS).until(() -> _scans.get() > scan); } + /** + * Test that if a war file has a context xml sibling, it will only be redeployed when the + * context xml changes, not the war. + */ + @Test + public void testSelectiveDeploy(WorkDir workDir) throws Exception + { + Path testdir = workDir.getEmptyPathDir(); + createJettyBase(testdir); + startJetty(); + + Path webappsDir = jetty.getJettyBasePath().resolve("webapps"); + Files.createFile(webappsDir.resolve("simple.war")); + jetty.copyWebapp("simple.xml", "simple.xml"); + waitForDirectoryScan(); + jetty.assertContextHandlerExists("/simple"); + ContextHandler contextHandler = jetty.getContextHandler("/simple"); + assertNotNull(contextHandler); + assertEquals(jetty.getJettyBasePath().resolve("tmp").toFile().getAbsolutePath(), contextHandler.getTempDirectory().getAbsolutePath()); + + //touch the context xml and check the context handler was redeployed + jetty.copyWebapp("simple.xml", "simple.xml"); + waitForDirectoryScan(); + ContextHandler contextHandler2 = jetty.getContextHandler("/simple"); + assertNotNull(contextHandler2); + assertNotSame(contextHandler, contextHandler2); + + //touch the war file and check the context handler was NOT redeployed + Thread.sleep(1000L); //ensure at least a millisecond has passed + Files.setLastModifiedTime(webappsDir.resolve("simple.war"), FileTime.fromMillis(System.currentTimeMillis())); + waitForDirectoryScan(); + ContextHandler contextHandler3 = jetty.getContextHandler("/simple"); + assertNotNull(contextHandler3); + assertSame(contextHandler2, contextHandler3); + } + /** * Simple webapp deployment after startup of server. * diff --git a/jetty-core/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/test/XmlConfiguredJetty.java b/jetty-core/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/test/XmlConfiguredJetty.java index 50d88bcc2aa..2ee6ce882ed 100644 --- a/jetty-core/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/test/XmlConfiguredJetty.java +++ b/jetty-core/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/test/XmlConfiguredJetty.java @@ -216,6 +216,23 @@ public class XmlConfiguredJetty } } + public ContextHandler getContextHandler(String contextPath) + { + ContextHandler contextHandler = null; + for (Handler handler : _contexts.getHandlers()) + { + if (handler instanceof ContextHandler ch) + { + if (ch.getContextPath().equals(contextPath)) + { + contextHandler = ch; + break; + } + } + } + return contextHandler; + } + private void copyFile(String type, Path srcFile, Path destFile) throws IOException { assertThat(srcFile, PathMatchers.isRegularFile()); diff --git a/jetty-core/jetty-deploy/src/test/resources/webapps/simple.xml b/jetty-core/jetty-deploy/src/test/resources/webapps/simple.xml new file mode 100644 index 00000000000..a83ffeafa26 --- /dev/null +++ b/jetty-core/jetty-deploy/src/test/resources/webapps/simple.xml @@ -0,0 +1,10 @@ + + + + /simple + + + + + +