Skip to content

Commit

Permalink
Add source type of entries for non-filtered web resources dirs
Browse files Browse the repository at this point in the history
Signed-off-by: Scott Kurz <[email protected]>
  • Loading branch information
scottkurz committed May 4, 2021
1 parent f3c5367 commit c2579ce
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,46 +140,50 @@ protected void installLooseConfigWar(MavenProject proj, LooseConfigData config,
setLooseProjectRootForContainer(proj, config);
}

LooseWarApplication looseWar = new LooseWarApplication(proj, config, log);

if(exploded) {

runExplodedMojo();

Path webAppDir = MavenProjectUtil.getWebAppDirectory(proj);
//TODO - does order matter?

/*
* TODO - out of date !
* <archive>
<dir sourceOnDisk="C:\app\target\myapp-1.0" targetInArchive="/"/>
<file sourceOnDisk="C:\app\target\tmp\META-INF\MANIFEST.MF" targetInArchive="/META-INF/MANIFEST.MF"/>
</archive>
*/
LooseWarApplication looseWar = new LooseWarApplication(proj, config);
looseWar.addOutputDir(looseWar.getDocumentRoot(), webAppDir.toFile(), "/");
File manifestFile = MavenProjectUtil.getManifestFile(proj, "maven-war-plugin");
looseWar.addManifestFile(manifestFile);

// If I'm filtering web.xml, etc., I want to monitor from the exploded dir, not via a source dir
if (!looseWar.isFilteringDeploymentDescriptors()) {
looseWar.addSourceDir();
}

// Add source paths for non-filtered web resources.
// We'll already have the runtime application monitor watching for file changes, and we
// don't want to set up the more expensive dev mode type of watching.
looseWar.addNonFilteredWebResourcesConfigurationPaths();

// Don't especially need to run it exactly here, but in debugger we can see what we have
runExplodedMojo();
looseWar.addOutputDir(looseWar.getDocumentRoot(), looseWar.getWebAppDirectory(), "/");

} else {
LooseWarApplication looseWar = new LooseWarApplication(proj, config);

looseWar.addSourceDir(proj);
looseWar.addSourceDir();
looseWar.addOutputDir(looseWar.getDocumentRoot(), new File(proj.getBuild().getOutputDirectory()),
"/WEB-INF/classes");

// retrieve the directories defined as resources in the maven war plugin
Map<Path,String> webResources = MavenProjectUtil.getWebResourcesConfigurationPaths(proj, log);
if (webResources != null) {
for (Path directory : webResources.keySet()) {
String targetPath = webResources.get(directory)==null ? "/" : "/"+webResources.get(directory);
looseWar.addOutputDir(looseWar.getDocumentRoot(), directory.toFile(), targetPath);
}
}
looseWar.addAllWebResourcesConfigurationPaths();

// retrieves dependent library jar files
addEmbeddedLib(looseWar.getDocumentRoot(), proj, looseWar, "/WEB-INF/lib/");

// add Manifest file
File manifestFile = MavenProjectUtil.getManifestFile(proj, "maven-war-plugin");
looseWar.addManifestFile(manifestFile);
}

// add Manifest file
File manifestFile = MavenProjectUtil.getManifestFile(proj, "maven-war-plugin");
looseWar.addManifestFile(manifestFile);
}

// install ear project artifact using loose application configuration file
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
package io.openliberty.tools.maven.applications;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.w3c.dom.DOMException;

import io.openliberty.tools.common.plugins.config.LooseApplication;
import io.openliberty.tools.common.plugins.config.LooseConfigData;
Expand All @@ -11,14 +21,195 @@
public class LooseWarApplication extends LooseApplication {

protected final MavenProject project;
protected final Log log;

public LooseWarApplication(MavenProject project, LooseConfigData config) {
public LooseWarApplication(MavenProject project, LooseConfigData config, Log log) {
super(project.getBuild().getDirectory(), config);
this.project = project;
this.log = log;
}

public void addSourceDir(MavenProject proj) throws Exception {
Path warSourceDir = MavenProjectUtil.getWarSourceDirectory(proj);
public void addSourceDir() throws Exception {
Path warSourceDir = getWarSourceDirectory();
config.addDir(warSourceDir.toFile(), "/");
}

public Path getWarSourceDirectory() {
return getWarSourceDirectory(project);
}

private static Path getWarSourceDirectory(MavenProject project) {
Path baseDir = Paths.get(project.getBasedir().getAbsolutePath());
String warSourceDir = MavenProjectUtil.getPluginConfiguration(project, "org.apache.maven.plugins", "maven-war-plugin", "warSourceDirectory");
if (warSourceDir == null) {
warSourceDir = "src/main/webapp";
}
// Use java.nio Paths to fix issue with absolute paths on Windows
return baseDir.resolve(warSourceDir);
}

public Path getWebAppDirectory(MavenProject project) {
Xpp3Dom dom = project.getGoalConfiguration("org.apache.maven.plugins", "maven-war-plugin", null, null);
String webAppDirStr = null;
if (dom != null) {
Xpp3Dom webAppDirConfig = dom.getChild("webappDirectory");
if (webAppDirConfig != null) {
webAppDirStr = webAppDirConfig.getValue();
}
}

if (webAppDirStr != null) {
return Paths.get(webAppDirStr);
} else {
// Match plugin default (we could get the default programmatically via webAppDirConfig.getAttribute("default-value") but don't
return Paths.get(project.getBuild().getDirectory(), project.getBuild().getFinalName());
}
}

public static List<Path> getDynamicWebSourceDirectories(MavenProject project) {

List<Path> retVal = new ArrayList<Path>();

Path baseDirPath = Paths.get(project.getBasedir().getAbsolutePath());

for (Xpp3Dom resource : getWebResourcesConfigurations(project)) {
Xpp3Dom dir = resource.getChild("directory");
Xpp3Dom filtering = resource.getChild("filtering");
if (dir != null && filtering != null) {
boolean filtered = Boolean.parseBoolean(filtering.getValue());
if (filtered) {
retVal.add(baseDirPath.resolve(dir.getValue()));
}
}
}

// Now add warSourceDir
if (isFilteringDeploymentDescriptors(project)) {
retVal.add(getWarSourceDirectory(project));
}

return retVal;
}


private static boolean isFilteringDeploymentDescriptors(MavenProject project) {
Boolean retVal = false;
Xpp3Dom dom = project.getGoalConfiguration("org.apache.maven.plugins", "maven-war-plugin", null, null);
if (dom != null) {
Xpp3Dom fdd = dom.getChild("filteringDeploymentDescriptors");
if (fdd != null) {
retVal = Boolean.parseBoolean(fdd.getValue());
}
}
return retVal;
}

public boolean isFilteringDeploymentDescriptors() {
return isFilteringDeploymentDescriptors(project);
}


/**
* Get directory and targetPath configuration values from the Maven WAR plugin
* @param proj the Maven project
* @return ALLOWS DUPS
* @return a Map of source and target directories corresponding to the configuration keys
* or null if the war plugin or its webResources or resource elements are not used.
* The map will be empty if the directory element is not used. The value field of the
* map is null when the targetPath element is not used.
*/
private static List<Xpp3Dom> getWebResourcesConfigurations(MavenProject project) {
List<Xpp3Dom> retVal = new ArrayList<Xpp3Dom>();
Xpp3Dom dom = project.getGoalConfiguration("org.apache.maven.plugins", "maven-war-plugin", null, null);
if (dom != null) {
Xpp3Dom web = dom.getChild("webResources");
if (web != null) {
Xpp3Dom resources[] = web.getChildren("resource");
if (resources != null) {
for (int i = 0; i < resources.length; i++) {
Xpp3Dom dir = resources[i].getChild("directory");
// put dir in List
if (dir != null) {
retVal.add(resources[i]);
}
}
}
}
}
return retVal;
}

private void addWebResourcesConfigurationPaths(boolean onlyUnfiltered) throws DOMException, IOException {
Set<Path> handled = new HashSet<Path>();

Path baseDirPath = Paths.get(project.getBasedir().getAbsolutePath());

for (Xpp3Dom resource : getWebResourcesConfigurations(project)) {
Xpp3Dom dir = resource.getChild("directory");
Xpp3Dom target = resource.getChild("targetPath");
Xpp3Dom filtering = resource.getChild("filtering");
Path resolvedDir = baseDirPath.resolve(dir.getValue());
if (handled.contains(resolvedDir)) {
log.warn("Ignoring webResources dir: " + dir.getValue() + ", already have entry for path: " + resolvedDir);
} else {
if (onlyUnfiltered) {
if (filtering != null && Boolean.parseBoolean(filtering.getValue())) {
continue;
} else {
String targetPath = "/";
if (target != null) {
targetPath = "/" + target.getValue();
}
addOutputDir(getDocumentRoot(), resolvedDir.toFile(), targetPath);
handled.add(resolvedDir);
}
} else {
String targetPath = "/";
if (target != null) {
targetPath = "/" + target.getValue();
}
addOutputDir(getDocumentRoot(), resolvedDir.toFile(), targetPath);
handled.add(resolvedDir);
}
/**
* String targetPath = webResources.get(directory)==null ? "/" : "/"+webResources.get(directory);
looseWar.addOutputDir(looseWar.getDocumentRoot(), directory.toFile(), targetPath);
*/
}
}
}

/*
* @return
* @throws IOException
* @throws DOMException
*/

public void addAllWebResourcesConfigurationPaths() throws DOMException, IOException {
addWebResourcesConfigurationPaths(false);
}

public void addNonFilteredWebResourcesConfigurationPaths() throws DOMException, IOException {
addWebResourcesConfigurationPaths(true);
}

public Path getWebAppDirectory() {
Xpp3Dom dom = project.getGoalConfiguration("org.apache.maven.plugins", "maven-war-plugin", null, null);
String webAppDirStr = null;
if (dom != null) {
Xpp3Dom webAppDirConfig = dom.getChild("webappDirectory");
if (webAppDirConfig != null) {
webAppDirStr = webAppDirConfig.getValue();
}
}

if (webAppDirStr != null) {
return Paths.get(webAppDirStr);
} else {
// Match plugin default (we could get the default programmatically via webAppDirConfig.getAttribute("default-value") but don't
return Paths.get(project.getBuild().getDirectory(), project.getBuild().getFinalName());
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.twdata.maven.mojoexecutor.MojoExecutor.Element;

import io.openliberty.tools.ant.ServerTask;
import io.openliberty.tools.common.plugins.config.LooseConfigData;
import io.openliberty.tools.common.plugins.util.DevUtil;
import io.openliberty.tools.common.plugins.util.DevUtilConfig;
import io.openliberty.tools.common.plugins.util.JavaCompilerOptions;
Expand All @@ -67,6 +68,7 @@
import io.openliberty.tools.common.plugins.util.ServerStatusUtil;
import io.openliberty.tools.maven.BasicSupport;
import io.openliberty.tools.maven.applications.DeployMojoSupport;
import io.openliberty.tools.maven.applications.LooseWarApplication;
import io.openliberty.tools.maven.utils.ExecuteMojoUtil;
import io.openliberty.tools.maven.utils.MavenProjectUtil;

Expand Down Expand Up @@ -842,7 +844,7 @@ protected void doExecute() throws Exception {
resourceDirs.add(defaultResourceDir);
}

List<Path> webResourceDirs = MavenProjectUtil.getMonitoredWebSourceDirectories(project);
List<Path> webResourceDirs = LooseWarApplication.getDynamicWebSourceDirectories(project);

JavaCompilerOptions compilerOptions = getMavenCompilerOptions();

Expand Down
Loading

0 comments on commit c2579ce

Please sign in to comment.