diff --git a/src/main/java/org/codehaus/mojo/xml/AbstractXmlMojo.java b/src/main/java/org/codehaus/mojo/xml/AbstractXmlMojo.java index 31d46aa..50bb7b0 100644 --- a/src/main/java/org/codehaus/mojo/xml/AbstractXmlMojo.java +++ b/src/main/java/org/codehaus/mojo/xml/AbstractXmlMojo.java @@ -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; @@ -45,6 +47,7 @@ * Abstract base class for the plugins Mojo's. */ public abstract class AbstractXmlMojo extends AbstractMojo { + /** * The Maven Project. */ @@ -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 pluginDependencies; + /** * An XML catalog file, or URL, which is being used to resolve entities. See * Catalog files. @@ -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 @@ -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. */ @@ -151,6 +174,11 @@ protected void setCatalogs(List pCatalogFiles, List 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); @@ -347,4 +375,19 @@ void checkCatalogHandling() throws MojoFailureException { protected CatalogHandling getCatalogHandling() { return catalogHandling; } + + private ClassLoader getClassLoader() { + if (classLoader == null) { + List 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; + } }