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

Add support for classpath lookups. #115

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
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;
}
}