From 70568de177e4134e4715d7725281cc57c2c4bd17 Mon Sep 17 00:00:00 2001 From: jetoile Date: Wed, 27 Nov 2019 12:22:47 +0100 Subject: [PATCH] add extraClasspath --- .../hadoopunit/HadoopUnitRunnable.java | 45 +++++++++++++++++-- .../hadoopunit/HadoopStandaloneBootstrap.java | 36 ++++++++++++++- 2 files changed, 76 insertions(+), 5 deletions(-) diff --git a/hadoop-unit-maven-plugin/src/main/java/fr/jetoile/hadoopunit/HadoopUnitRunnable.java b/hadoop-unit-maven-plugin/src/main/java/fr/jetoile/hadoopunit/HadoopUnitRunnable.java index d1d6da85..924d1905 100644 --- a/hadoop-unit-maven-plugin/src/main/java/fr/jetoile/hadoopunit/HadoopUnitRunnable.java +++ b/hadoop-unit-maven-plugin/src/main/java/fr/jetoile/hadoopunit/HadoopUnitRunnable.java @@ -42,10 +42,7 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.*; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.stream.Collectors; @@ -87,6 +84,44 @@ public void serviceCreationFailed(Class type, Class impl, Throwable except return locator.getService(RepositorySystem.class); } + private List loadExtraClasspath(ComponentArtifact currentArtifact, RepositorySystem system, DefaultRepositorySystemSession session, DependencyFilter classpathFilter) { + Map properties = currentArtifact.getProperties(); + if (properties == null) { + return Collections.EMPTY_LIST; + } + String extraClasspathProperty = properties.get(currentArtifact.getComponentName().toLowerCase() + ".extraClasspath"); + if (extraClasspathProperty != null) { + log.info("Found extraClasspath for component " + currentArtifact.getComponentName() + " configuration. Is going to add them to classpath"); + String[] artifacts = extraClasspathProperty.split(","); + return Arrays.asList(artifacts).stream() + .map(a -> { + DefaultArtifact artifact = new DefaultArtifact(a.trim()); + + CollectRequest collectRequest = new CollectRequest(); + collectRequest.setRoot(new Dependency(artifact, JavaScopes.RUNTIME)); + collectRequest.setRepositories(remoteRepos); + + log.info("Resolving artifact for extra Classpath" + artifact + " from " + remoteRepos.stream().map(r -> r.getId() + "-" + r.getUrl()).collect(Collectors.joining(", "))); + + DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, classpathFilter); + + List artifactResults = null; + try { + artifactResults = system.resolveDependencies(session, dependencyRequest).getArtifactResults(); + } catch (DependencyResolutionException e) { + log.error("an error occured during the dependencies phase: " + e.getMessage()); + e.printStackTrace(); + } + + return artifactResults; + }) + .flatMap(x -> x.stream()) + .collect(Collectors.toList()); + } else { + return Collections.EMPTY_LIST; + } + } + @Override public void run() { log.info("is going to start hadoop unit"); @@ -116,6 +151,8 @@ public void run() { } List artifacts = new ArrayList<>(); + artifactResults.addAll(loadExtraClasspath(c, system, session, classpathFilter)); + artifactResults.stream().forEach(a -> artifacts.add(a.getArtifact().getFile()) ); diff --git a/hadoop-unit-standalone/src/main/java/fr/jetoile/hadoopunit/HadoopStandaloneBootstrap.java b/hadoop-unit-standalone/src/main/java/fr/jetoile/hadoopunit/HadoopStandaloneBootstrap.java index 45f6378d..0e70db10 100644 --- a/hadoop-unit-standalone/src/main/java/fr/jetoile/hadoopunit/HadoopStandaloneBootstrap.java +++ b/hadoop-unit-standalone/src/main/java/fr/jetoile/hadoopunit/HadoopStandaloneBootstrap.java @@ -330,6 +330,39 @@ private static void addModuleJarToCurrentClassloader(Map loadExtraClasspath(String currentArtifact, RepositorySystem system, DefaultRepositorySystemSession session, DependencyFilter classpathFilter) { + String extraClasspathProperty = hadoopUnitConfiguration.getString(currentArtifact.toLowerCase() + ".extraClasspath"); + if (extraClasspathProperty != null) { + LOGGER.info("Found extraClasspath for component {} configuration. Is going to add them to classpath", currentArtifact); + String[] artifacts = extraClasspathProperty.split(","); + return Arrays.asList(artifacts).stream() + .map(a -> { + DefaultArtifact artifact = new DefaultArtifact(a.trim()); + + CollectRequest collectRequest = new CollectRequest(); + collectRequest.setRoot(new Dependency(artifact, JavaScopes.RUNTIME)); + collectRequest.setRepositories(getRemoteRepositories()); + + LOGGER.info("Resolving artifact for extra Classpath {} from {}", artifact, getRemoteRepositories().stream().map(r -> r.getId() + "-" + r.getUrl()).collect(Collectors.joining(", "))); + + DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, classpathFilter); + + List artifactResults = null; + try { + artifactResults = system.resolveDependencies(session, dependencyRequest).getArtifactResults(); + } catch (DependencyResolutionException e) { + LOGGER.error("an error occured during the dependencies phase: " + e.getMessage()); + } + + return artifactResults; + }) + .flatMap(x -> x.stream()) + .collect(Collectors.toList()); + } else { + return Collections.EMPTY_LIST; + } + } + private static Map loadMavenDependencies(List componentsToStart) throws BootstrapException { Map componentsToStartWithDependencies = new HashMap<>(); @@ -357,8 +390,9 @@ private static Map loadMavenDependencies(List artifacts = new ArrayList<>(); + artifactResults.addAll(loadExtraClasspath(c, repositorySystem, session, classpathFilter)); + artifactResults.stream().forEach(a -> artifacts.add(a.getArtifact().getFile()) );