Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Automatically use jar resource listener in a multimodule Maven project #16738

Merged
merged 9 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions flow-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
<vaadin.reuseDevServer>false</vaadin.reuseDevServer>
<vaadin.devServerPort>0</vaadin.devServerPort>
<vaadin.frontend.hotdeploy>true</vaadin.frontend.hotdeploy>
<vaadin.frontend.hotdeploy.dependencies></vaadin.frontend.hotdeploy.dependencies>
</properties>

<dependencies>
Expand Down Expand Up @@ -229,7 +228,6 @@
<!-- Allow test clients not on localhost to connect to Vite-->
<vaadin.devmode.vite.options>${vaadin.devmode.vite.options}</vaadin.devmode.vite.options>
<vaadin.frontend.hotdeploy>${vaadin.frontend.hotdeploy}</vaadin.frontend.hotdeploy>
<vaadin.frontend.hotdeploy.dependencies>${vaadin.frontend.hotdeploy.dependencies}</vaadin.frontend.hotdeploy.dependencies>
</systemProperties>
</configuration>
</plugin>
Expand Down
1 change: 0 additions & 1 deletion flow-tests/test-live-reload-multimodule/ui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<packaging>war</packaging>
<properties>
<maven.deploy.skip>true</maven.deploy.skip>
<vaadin.frontend.hotdeploy.dependencies>../library</vaadin.frontend.hotdeploy.dependencies>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;

import com.vaadin.flow.server.InitParameters;
import com.vaadin.flow.server.VaadinContext;
Expand All @@ -44,16 +45,28 @@ public ExternalDependencyWatcher(VaadinContext context,
File jarFrontendResourcesFolder) {
ApplicationConfiguration config = ApplicationConfiguration.get(context);

String hotdeployDependencies = config.getStringProperty(
String hotdeployDependenciesProperty = config.getStringProperty(
InitParameters.FRONTEND_HOTDEPLOY_DEPENDENCIES, null);

List<String> hotdeployDependencyFolders = new ArrayList<>();
if (hotdeployDependencies != null) {
for (String folder : hotdeployDependencies.split(",")) {
if (hotdeployDependenciesProperty != null) {
for (String folder : hotdeployDependenciesProperty.split(",")) {
if (!folder.isBlank()) {
hotdeployDependencyFolders.add(folder.trim());
}
}
} else {
File parentPomFile = MavenUtils.getParentPomOfMultiModuleProject(
new File(config.getProjectFolder(), "pom.xml"));
if (parentPomFile != null) {
Document parentPom = MavenUtils.parsePomFile(parentPomFile);
if (parentPom != null) {
hotdeployDependencyFolders = MavenUtils
.getModuleFolders(parentPom).stream()
.map(folder -> ".." + File.separator + folder)
.toList();
}
}
}

for (String hotdeployDependencyFolder : hotdeployDependencyFolders) {
Expand All @@ -62,7 +75,8 @@ public ExternalDependencyWatcher(VaadinContext context,
Path metaInf = moduleFolder
.resolve(Path.of("src", "main", "resources", "META-INF"));
if (!watchDependencyFolder(metaInf.toFile(),
jarFrontendResourcesFolder)) {
jarFrontendResourcesFolder)
&& hotdeployDependenciesProperty != null) {
getLogger().warn("No folders to watch were found in "
+ metaInf.normalize().toAbsolutePath()
+ ". This should be the META-INF folder that contains either frontend or resources/frontend");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
import javax.xml.parsers.DocumentBuilderFactory;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;
import java.util.stream.Stream.Builder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

Expand Down Expand Up @@ -95,14 +97,18 @@ public static String getGroupId(Document pom) {
String groupId = getFirstElementTextByName(pom.getDocumentElement(),
"groupId");
if (groupId == null) {
groupId = findChild(pom.getDocumentElement(), "parent")
groupId = findParentTag(pom)
.map(parentNode -> getFirstElementTextByName(parentNode,
"groupId"))
.orElse(null);
}
return groupId;
}

private static Optional<Node> findParentTag(Document pom) {
return findChild(pom.getDocumentElement(), "parent");
}

/**
* Finds the artifact id for the given pom file.
*
Expand All @@ -115,6 +121,19 @@ public static String getArtifactId(Document pom) {
"artifactId");
}

private static String getParentArtifactId(Document pom) {
return findParentTag(pom)
.flatMap(parentNode -> findChild(parentNode, "artifactId"))
.map(artifactIdNode -> artifactIdNode.getTextContent())
.orElse(null);
}

private static Optional<String> getParentRelativePath(Document pom) {
return findParentTag(pom)
.flatMap(parentNode -> findChild(parentNode, "relativePath"))
.map(artifactIdNode -> artifactIdNode.getTextContent());
}

private static Optional<Node> findChild(Node node, String tagname) {
return findChildren(node, tagname).findFirst();
}
Expand All @@ -132,4 +151,79 @@ private static Stream<Node> findChildren(Node node, String tagname) {
return builder.build();
}

/**
* Gets the parent pom location for the given pom file, if the given pom
* file is part of a multi module project.
*
* @param pomFile
* the pom file
* @return the location of the parent pom file or {@code null} if the given
* pom file does not have a parent inside the same multi module
* project
*/
public static File getParentPomOfMultiModuleProject(File pomFile) {
Document pom = parsePomFile(pomFile);
if (pom == null) {
return null;
}
String parent = getParentArtifactId(pom);

File pomFolder = pomFile.getParentFile();
File parentPomFile = getParentRelativePath(pom)
.map(relativePath -> new File(pomFolder, relativePath))
.orElse(new File(pomFolder.getParentFile(), "pom.xml"));

Document parentFolderPom = parsePomFile(parentPomFile);
if (parentFolderPom == null) {
return null;
}
String parentFolderArtifactId = getArtifactId(parentFolderPom);

if (Objects.equals(parent, parentFolderArtifactId)) {
try {
return parentPomFile.getCanonicalFile();
} catch (IOException e) {
return parentPomFile;
}
}
return null;

}

/**
* Gets a list of the folders containing the sub modules for the given pom
* file.
*
* @param pom
* the pom file containing sub modules
* @return a list of folders for the sub modules
*/
public static List<String> getModuleFolders(Document pom) {
return findChild(pom.getDocumentElement(), "modules").stream()
.flatMap(node -> findChildren(node, "module"))
.map(moduleNode -> moduleNode.getTextContent())
.map(possiblyFilename -> removeAfter(possiblyFilename, "/"))
.toList();
}

/**
* Removes the part of the given string that comes after the (last) instance
* of the given delimiter.
*
* Returns the original string if it does not contain the delimiter.
*
* @param str
* the string to parse
* @param delimiter
* the delimiter to look for
* @return the modified string
*/
private static String removeAfter(String str, String delimiter) {
int i = str.lastIndexOf(delimiter);
if (i != -1) {
return str.substring(0, i);
}
return str;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.net.URL;
import java.nio.file.Path;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Expand All @@ -17,8 +18,16 @@ public class MavenUtilsTest {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

private File mavenFolder;

@Before
public void setupPoms() throws Exception {
URL mavenTestResourceDirectory = getClass().getResource("maven");
this.mavenFolder = Path.of(mavenTestResourceDirectory.toURI()).toFile();
}

@Test
public void basicInformationForStandalonePom() throws IOException {
public void basicInformationForStandalonePom() throws Exception {
File pomXml = getPomXml("pom-standalone.xml");
Document parse = MavenUtils.parsePomFile(pomXml);
Assert.assertEquals("this.group", MavenUtils.getGroupId(parse));
Expand All @@ -27,21 +36,56 @@ public void basicInformationForStandalonePom() throws IOException {
}

@Test
public void basicInformationForPomWithParent() throws IOException {
File pomXml = getPomXml("pom-with-parent.xml");
public void basicInformationForPomWithParent() throws Exception {
File pomXml = getPomXml("standard-multimodule/module1/pom.xml");
Document parse = MavenUtils.parsePomFile(pomXml);
Assert.assertEquals("this.group", MavenUtils.getGroupId(parse));
Assert.assertEquals("this-the-artifact",
MavenUtils.getArtifactId(parse));
}

@Test
public void detectsPomIsPartOfASimpleMultimoduleProject() throws Exception {
File parent = getPomXml("standard-multimodule/pom.xml");
Assert.assertEquals(parent, MavenUtils.getParentPomOfMultiModuleProject(
getPomXml("standard-multimodule/module1/pom.xml")));
}

@Test
public void detectsPomIsPartOfAComplexMultimoduleProject()
throws Exception {
File parent = getPomXml("complex-multimodule/pom-parent.xml");
File parentPomOfMultiModuleProject = MavenUtils
.getParentPomOfMultiModuleProject(getPomXml(
"complex-multimodule/module1/pom-with-parent.xml"));
Assert.assertEquals(parent, parentPomOfMultiModuleProject);
}

@Test
public void findsModulesInSimpleMultiModulePom() throws Exception {
File pomXml = getPomXml("standard-multimodule/pom.xml");
Document pom = MavenUtils.parsePomFile(pomXml);
Assert.assertEquals(List.of("module1", "module2"),
MavenUtils.getModuleFolders(pom));
}

@Test
public void findsModulesInComplexMultiModulePom() throws Exception {
File pomXml = getPomXml("complex-multimodule/pom-parent.xml");
Document pom = MavenUtils.parsePomFile(pomXml);
Assert.assertEquals(List.of("module1", "module2"),
MavenUtils.getModuleFolders(pom));
}

@Test
public void findsNoModulesInStandalonePom() throws Exception {
File pomXml = getPomXml("pom-standalone.xml");
Document pom = MavenUtils.parsePomFile(pomXml);
Assert.assertEquals(List.of(), MavenUtils.getModuleFolders(pom));
}

private File getPomXml(String filename) throws IOException {
File pomXml = temporaryFolder.newFile(filename);
FileUtils.write(pomXml,
IOUtils.toString(getClass().getResource("maven/" + filename),
StandardCharsets.UTF_8),
StandardCharsets.UTF_8);
return pomXml;
return new File(mavenFolder, filename);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>parent-artifact</artifactId>
<groupId>this.group</groupId>
<version>1.7.2-SNAPSHOT</version>
<relativePath>../pom-parent.xml</relativePath>
</parent>
<artifactId>this-the-artifact</artifactId>
<name>Something something</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>parent-artifact</artifactId>
<groupId>this.group</groupId>
<version>1.7.2-SNAPSHOT</version>
<relativePath>../pom-parent.xml</relativePath>
</parent>
<artifactId>another-artifact</artifactId>
<name>Something something</name>

<build>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>this.group</groupId>
<artifactId>parent-artifact</artifactId>
<version>1.7.2-SNAPSHOT</version>
<name>Something something parent</name>
<packaging>pom</packaging>
<modules>
<module>module1/pom-with-parent.xml</module>
<module>module2</module>
</modules>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,4 @@
<version>1.7.2-SNAPSHOT</version>
<name>Something something</name>

<build>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>parent-artifact</artifactId>
<groupId>this.group</groupId>
<version>1.7.2-SNAPSHOT</version>
</parent>
<artifactId>this-the-artifact</artifactId>
<name>Something something</name>

<build>
</build>

</project>
Loading