Skip to content

Commit

Permalink
Add support for classpath lookups.
Browse files Browse the repository at this point in the history
Closes #114.

Signed-off-by: Łukasz Dywicki <[email protected]>
  • Loading branch information
splatch committed Mar 18, 2024
1 parent 06b2e6b commit 40610ac
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/main/java/org/codehaus/mojo/xml/AbstractXmlMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
Expand All @@ -45,6 +47,7 @@
* Abstract base class for the plugins Mojo's.
*/
public abstract class AbstractXmlMojo extends AbstractMojo {

/**
* The Maven Project.
*/
Expand All @@ -71,6 +74,9 @@ public abstract class AbstractXmlMojo extends AbstractMojo {
@Parameter(property = "xml.skip", defaultValue = "false")
private boolean skip;

@Parameter(readonly = true, defaultValue = "${plugin.artifacts}")
private List<Artifact> pluginDependencies;

/**
* An XML catalog file, or URL, which is being used to resolve entities. See
* <a href="examples/catalog.html">Catalog files</a>.
Expand All @@ -80,6 +86,11 @@ public abstract class AbstractXmlMojo extends AbstractMojo {
@Parameter
private String[] catalogs;

/**
* Class loader which wraps resources available to the plugin.
*/
private ClassLoader classLoader;

public enum CatalogHandling {
/**
* Unmatched entities are resolved through URI resolution
Expand Down Expand Up @@ -138,6 +149,18 @@ protected File asAbsoluteFile(File f) {
return new File(getBasedir(), f.getPath());
}

/**
* Attempt to convert given uri into a classpath resource.
*
* Resources are looked up using plugin classpath.
*
* @param catalog Catalog location.
* @return URL of resource or null if not found.
*/
private URL asClasspath(String catalog) {
return getClassLoader().getResource(catalog);
}

/**
* Returns the plugins catalog files.
*/
Expand All @@ -151,6 +174,11 @@ protected void setCatalogs(List<File> pCatalogFiles, List<URL> pCatalogUrls) thr
URL url = new URL(catalogs[i]);
pCatalogUrls.add(url);
} catch (MalformedURLException e) {
URL classpath = asClasspath(catalogs[i]);
if (classpath != null) {
pCatalogUrls.add(classpath);
continue;
}
File absoluteCatalog = asAbsoluteFile(new File(catalogs[i]));
if (!absoluteCatalog.exists() || !absoluteCatalog.isFile()) {
throw new MojoExecutionException("That catalog does not exist:" + absoluteCatalog.getPath(), e);
Expand Down Expand Up @@ -347,4 +375,19 @@ void checkCatalogHandling() throws MojoFailureException {
protected CatalogHandling getCatalogHandling() {
return catalogHandling;
}

private ClassLoader getClassLoader() {
if (classLoader == null) {
List<URL> urls = new ArrayList<>();
for (Artifact artifact : pluginDependencies) {
try {
urls.add(artifact.getFile().toURI().toURL());
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
classLoader = new URLClassLoader(urls.toArray(new URL[0]));
}
return classLoader;
}
}

0 comments on commit 40610ac

Please sign in to comment.