added artifact import support

This commit is contained in:
2023-05-29 11:09:22 -04:00
parent d22f657f4e
commit 3c73bcb83d
3 changed files with 81 additions and 12 deletions

View File

@@ -36,21 +36,13 @@ public class AbstractBanConfiguration implements BanConfiguration {
private final Pattern artifactPattern = Pattern.compile("^([^:]+):([^:]+)(:([^:]+))?$");
private final Pattern notRegexPattern = Pattern.compile("^[A-Za-z0-9_\\.]*$");
private final List<ArtifactFilter> includeArtifacts = new LinkedList<>();
private final List<ArtifactFilter> excludeArtifacts = new LinkedList<>();
protected final List<ArtifactFilter> includeArtifacts = new LinkedList<>();
protected final List<ArtifactFilter> excludeArtifacts = new LinkedList<>();
public void init(Xpp3Dom rootDom) throws IOException, MojoFailureException {
if (rootDom == null)
return;
Xpp3Dom importDom = rootDom.getChild("import");
if (importDom != null) {
String url = StringUtils.trimToNull(importDom.getValue());
BanConfigurationDownloader downloader = new BanConfigurationDownloader(url);
this.includeArtifacts.addAll(downloader.getIncludeArtifacts());
this.excludeArtifacts.addAll(downloader.getExcludeArtifacts());
}
Xpp3Dom includesDom = rootDom.getChild("includes");
if (includesDom != null)
this.includeArtifacts.addAll(this.parseArtifacts(includesDom));

View File

@@ -15,14 +15,24 @@
package com.inteligr8.maven.ban;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoFailureException;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.impl.ArtifactResolver;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.ArtifactResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -30,7 +40,17 @@ public class BanConfigurationDownloader extends AbstractBanConfiguration {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
public BanConfigurationDownloader(String url) throws IOException, MojoFailureException {
private final ArtifactResolver artifactResolver;
private final MavenSession session;
public BanConfigurationDownloader(MavenSession session, ArtifactResolver artifactResolver) {
this.session = session;
this.artifactResolver = artifactResolver;
}
public BanConfigurationDownloader(MavenSession session, ArtifactResolver artifactResolver, String url) throws IOException, MojoFailureException {
this(session, artifactResolver);
try {
Xpp3Dom rootDom = this.load(new URL(url));
this.init(rootDom);
@@ -39,6 +59,47 @@ public class BanConfigurationDownloader extends AbstractBanConfiguration {
}
}
public BanConfigurationDownloader(MavenSession session, ArtifactResolver artifactResolver, File file) throws IOException, MojoFailureException {
this(session, artifactResolver);
try {
Xpp3Dom rootDom = this.load(file);
this.init(rootDom);
} catch (XmlPullParserException xppe) {
throw new MojoFailureException(xppe.getMessage(), xppe);
}
}
@Override
public void init(Xpp3Dom rootDom) throws IOException, MojoFailureException {
Xpp3Dom importDom = rootDom.getChild("import");
if (importDom != null) {
for (Xpp3Dom child : importDom.getChildren()) {
BanConfigurationDownloader downloader = null;
if (child.getName().equals("url")) {
String url = StringUtils.trimToNull(importDom.getValue());
downloader = new BanConfigurationDownloader(this.session, this.artifactResolver, url);
} else if (child.getName().equals("artifact")) {
Artifact artifact = new DefaultArtifact(child.getValue());
ArtifactRequest request = new ArtifactRequest(artifact, this.session.getCurrentProject().getRemoteProjectRepositories(), null);
try {
ArtifactResult result = this.artifactResolver.resolveArtifact(this.session.getRepositorySession(), request);
File file = result.getArtifact().getFile();
downloader = new BanConfigurationDownloader(this.session, this.artifactResolver, file);
} catch (ArtifactResolutionException are) {
this.logger.warn("The artifact could not be resolved; skipping: {}", child.getValue());
}
}
if (downloader != null) {
this.includeArtifacts.addAll(downloader.getIncludeArtifacts());
this.excludeArtifacts.addAll(downloader.getExcludeArtifacts());
}
}
}
}
private Xpp3Dom load(URL url) throws IOException, XmlPullParserException {
InputStream istream = url.openStream();
BufferedInputStream bistream = new BufferedInputStream(istream, 16384);
@@ -48,7 +109,17 @@ public class BanConfigurationDownloader extends AbstractBanConfiguration {
} finally {
bistream.close();
}
}
private Xpp3Dom load(File file) throws IOException, XmlPullParserException {
FileInputStream fistream = new FileInputStream(file);
BufferedInputStream bistream = new BufferedInputStream(fistream, 16384);
try {
this.logger.debug("Downloading configuration: {}", file);
return Xpp3DomBuilder.build(bistream, "utf-8");
} finally {
bistream.close();
}
}
}