This repository has been archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle to resolve and load project dependencies
- Loading branch information
Muhammad Uzair Baig
committed
Jun 12, 2018
1 parent
bca69d6
commit 48a119f
Showing
4 changed files
with
125 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...ava/uk/gov/justice/maven/generator/io/files/parser/generator/ProjectDependencyLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package uk.gov.justice.maven.generator.io.files.parser.generator; | ||
|
||
import static com.google.common.collect.Iterables.concat; | ||
import static com.google.common.collect.Iterables.unmodifiableIterable; | ||
import static com.google.common.collect.Lists.newArrayList; | ||
import static com.google.common.collect.Sets.newHashSet; | ||
import static java.lang.Thread.currentThread; | ||
import static org.slf4j.LoggerFactory.getLogger; | ||
|
||
import java.io.File; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import org.apache.maven.artifact.DependencyResolutionRequiredException; | ||
import org.apache.maven.project.MavenProject; | ||
import org.slf4j.Logger; | ||
|
||
public class ProjectDependencyLoader { | ||
|
||
private static final Logger LOGGER = getLogger(ProjectDependencyLoader.class); | ||
|
||
private final MavenProject project; | ||
|
||
public ProjectDependencyLoader(final MavenProject project) { | ||
this.project = project; | ||
} | ||
|
||
public void loadProjectDependencies() throws DependencyResolutionRequiredException, MalformedURLException { | ||
|
||
final Set<URL> dependenciesUrl = newHashSet(); | ||
|
||
final Iterable<String> combinedDependencies = unmodifiableIterable( | ||
concat(project.getRuntimeClasspathElements(), project.getCompileClasspathElements())); | ||
|
||
final List<String> dependencies = newArrayList(combinedDependencies); | ||
|
||
for (final String dependency : dependencies) { | ||
final URL dependencyUrl = new File(dependency).toURI().toURL(); | ||
if (dependencyUrl.toString().endsWith(".jar")) { | ||
LOGGER.info("Resource URL: {}", dependencyUrl.toString()); | ||
dependenciesUrl.add(dependencyUrl); | ||
} | ||
} | ||
|
||
final ClassLoader contextClassLoader = URLClassLoader.newInstance( | ||
dependenciesUrl.toArray(new URL[0]), | ||
currentThread().getContextClassLoader()); | ||
|
||
currentThread().setContextClassLoader(contextClassLoader); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...uk/gov/justice/maven/generator/io/files/parser/generator/ProjectDependencyLoaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package uk.gov.justice.maven.generator.io.files.parser.generator; | ||
|
||
import static com.google.common.collect.Lists.newArrayList; | ||
import static java.lang.Thread.currentThread; | ||
import static org.hamcrest.CoreMatchers.endsWith; | ||
import static org.hamcrest.core.Is.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URLClassLoader; | ||
import java.util.List; | ||
|
||
import org.apache.maven.artifact.DependencyResolutionRequiredException; | ||
import org.apache.maven.project.MavenProject; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class ProjectDependencyLoaderTest { | ||
|
||
@Mock | ||
private MavenProject project; | ||
|
||
@InjectMocks | ||
private ProjectDependencyLoader projectDependencyLoader; | ||
|
||
@Test | ||
public void shouldLoadProjectDependenciesSuccessfully() throws DependencyResolutionRequiredException, MalformedURLException { | ||
|
||
final List compileTimeClasspathElements = newArrayList( | ||
"example.jar"); | ||
|
||
when(project.getRuntimeClasspathElements()).thenReturn(newArrayList()); | ||
when(project.getCompileClasspathElements()).thenReturn(compileTimeClasspathElements); | ||
|
||
projectDependencyLoader.loadProjectDependencies(); | ||
|
||
final URLClassLoader parent = (URLClassLoader)currentThread().getContextClassLoader(); | ||
|
||
assertThat(parent.getURLs().length, is(1)); | ||
assertThat(parent.getURLs()[0].toString(), endsWith("generator-maven-plugin/generator-plugin/example.jar")); | ||
} | ||
|
||
@Test | ||
public void shouldNotFailedWhenProjectDependenciesJarNotFound() throws DependencyResolutionRequiredException, MalformedURLException { | ||
|
||
final List compileTimeClasspathElements = newArrayList( | ||
"fileNotFoundInClassPath"); | ||
|
||
when(project.getRuntimeClasspathElements()).thenReturn(newArrayList()); | ||
when(project.getCompileClasspathElements()).thenReturn(compileTimeClasspathElements); | ||
|
||
projectDependencyLoader.loadProjectDependencies(); | ||
|
||
final URLClassLoader parent = (URLClassLoader)currentThread().getContextClassLoader(); | ||
|
||
assertThat(parent.getURLs().length, is(0)); | ||
} | ||
|
||
} |