Skip to content

Commit

Permalink
Cleaned up implementation of package scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
timols committed Jul 23, 2013
1 parent 3088dff commit 2a6ade3
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 8 deletions.
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@

<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
<artifactId>maven-core</artifactId>
<version>3.0.4</version>
</dependency>

<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/github/timols/seeder/Seeder.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.github.timols.seeder;

import com.google.inject.*;
import com.github.timols.seeder.annotation.Step;
import com.github.timols.seeder.step.SeedStep;
import com.google.inject.*;
import org.reflections.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -33,9 +33,9 @@ public int compare(Class<? extends SeedStep> o1, Class<? extends SeedStep> o2) {
*
* @param stepPackageName The package that contains the SeedSteps
*/
public Seeder(String stepPackageName) {
public Seeder(String stepPackageName, Reflections reflections) {
this.stepPackageName = stepPackageName;
this.reflections = new Reflections(stepPackageName);
this.reflections = reflections;
this.injector = createInjector();
}

Expand Down Expand Up @@ -80,7 +80,6 @@ private List<Module> getModules() {
instances.add(module);
} catch (ReflectiveOperationException e) {
logger.error(e.getCause().getMessage(), e);

}
}
return instances;
Expand Down
63 changes: 62 additions & 1 deletion src/main/java/com/github/timols/seeder/plugins/SeederMojo.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
package com.github.timols.seeder.plugins;

import com.github.timols.seeder.Seeder;
import com.google.common.collect.Sets;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.StringUtils;
import org.reflections.Reflections;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.FilterBuilder;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;

/**
* Example pom.xml configuration:
Expand All @@ -31,7 +46,7 @@ public class SeederMojo extends AbstractMojo {

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
Seeder seeder = new Seeder(stepsPackage);
Seeder seeder = new Seeder(stepsPackage, getReflections(stepsPackage));
try {
seeder.seed();
} catch (Exception e) {
Expand All @@ -42,4 +57,50 @@ public void execute() throws MojoExecutionException, MojoFailureException {
public void setStepsPackage(String stepsPackage) {
this.stepsPackage = stepsPackage;
}

private MavenProject getProject() {
return (MavenProject) getPluginContext().get("project");
}

private Reflections getReflections(String stepPackageName) throws MojoExecutionException {
ConfigurationBuilder configuration = new ConfigurationBuilder()
.addUrls(Sets.newHashSet(buildOutputDirectoryUrl()))
.addClassLoader(buildProjectClassLoader());

if (StringUtils.isNotBlank(stepPackageName)) {
FilterBuilder filterBuilder = new FilterBuilder();
filterBuilder.include(FilterBuilder.prefix(stepPackageName));
configuration.filterInputsBy(filterBuilder);
}

return new Reflections(configuration);
}

private URLClassLoader buildProjectClassLoader() throws MojoExecutionException {
getLog().debug("Adding Artifacts to ClassLoader");
List<URL> urls = new ArrayList<>();

for (Artifact artifact : getProject().getArtifacts()) {
try {
urls.add((artifact).getFile().toURI().toURL());
} catch (MalformedURLException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}

urls.add(buildOutputDirectoryUrl());

getLog().debug("Artifact URLS:\n" + urls.toString().replace(",", "\n"));

return new URLClassLoader(urls.toArray(new URL[urls.size()]), getClass().getClassLoader());
}

private URL buildOutputDirectoryUrl() throws MojoExecutionException {
try {
File outputDirectoryFile = new File(getProject().getBuild().getOutputDirectory() + "/");
return outputDirectoryFile.toURI().toURL();
} catch (MalformedURLException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}
}
4 changes: 3 additions & 1 deletion src/test/java/com/github/timols/seeder/SeederTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.github.timols.seeder;

import org.junit.Test;
import org.reflections.Reflections;

import static org.junit.Assert.fail;

public class SeederTest {

@Test
public void testSeed() throws Exception {
Seeder seeder = new Seeder("com.example.seed");
String packageName = "com.example.seed";
Seeder seeder = new Seeder(packageName, new Reflections(packageName));
try {
seeder.seed();
} catch (Exception e) {
Expand Down

0 comments on commit 2a6ade3

Please sign in to comment.