Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Commit

Permalink
Handle to resolve and load project dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad Uzair Baig committed Jun 12, 2018
1 parent bca69d6 commit 48a119f
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 7 deletions.
12 changes: 6 additions & 6 deletions generator-plugin-it/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,6 @@
</execution>
</executions>
<dependencies>
<dependency>
<groupId>uk.gov.justice.maven.generator</groupId>
<artifactId>files-for-testing-io</artifactId>
<version>${project.version}</version>
<classifier>raml</classifier>
</dependency>
<dependency>
<groupId>uk.gov.justice.maven.generator</groupId>
<artifactId>raml-generator-for-testing</artifactId>
Expand Down Expand Up @@ -167,6 +161,12 @@
</plugins>
</build>
<dependencies>
<dependency>
<groupId>uk.gov.justice.maven.generator</groupId>
<artifactId>files-for-testing-io</artifactId>
<version>${project.version}</version>
<classifier>raml</classifier>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public void execute() throws MojoExecutionException {

try {
FileUtils.forceMkdir(outputDirectory);
new ProjectDependencyLoader(project).loadProjectDependencies();
new GenerateGoalProcessor(
new MojoGeneratorFactory(),
new FileTreeScannerFactory(),
Expand All @@ -92,5 +93,4 @@ private GenerateGoalConfig configuration(final List<Path> sourcePaths) {
sourcePaths);

}

}
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);
}
}
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));
}

}

0 comments on commit 48a119f

Please sign in to comment.