我写了一个自定义的Maven插件报告输出约弹簧MVC类的一些基本信息。 在我的内部测试中,我可以看到像这样的代码:
public Set<Class<?>> findControllerClasses(File buildOutputDir) throws IOException, ClassNotFoundException {
Collection<URL> urls = ClasspathHelper.forJavaClassPath();
if (buildOutputDir != null) {
urls.add(buildOutputDir.toURI().toURL());
}
Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(urls));
Set<Class<?>> types = reflections.getTypesAnnotatedWith(Controller.class);
return types;
}
以及在注释类拉动工程。 然而,当我使用报告插件在另一个项目,注释类没有回升。
有人可以阐明如何访问编译的类用于报告一些轻? 或者这是否是甚至可能吗?
编辑:使用的答案,部分解决了添加的maven-集结classpath以插件运行的类路径
然而,这只是加载类,如果他们有runtimeClasspathElements VAR的行家之外没有依赖关系。 有没有办法到classrealm太合并这些类?
也许使用
/**
* The classpath elements of the project.
*
* @parameter expression="${project.runtimeClasspathElements}"
* @required
* @readonly
*/
private List<String> classpathElements;
同
private ClassLoader getProjectClassLoader()
throws DependencyResolutionRequiredException, MalformedURLException
{
List<String> classPath = new ArrayList<String>();
classPath.addAll( classpathElements );
classPath.add( project.getBuild().getOutputDirectory() );
URL[] urls = new URL[classPath.size()];
int i = 0;
for ( String entry : classPath )
{
getLog().debug( "use classPath entry " + entry );
urls[i] = new File( entry ).toURI().toURL();
i++; // Important
}
return new URLClassLoader( urls );
}
好。 扩大对上述评论的答案,完整的解决方案是使用配置者,考虑到这两个运行时类路径,并从劲歌依赖的URL。 下面的代码显示
/**
*
* @plexus.component
* role="org.codehaus.plexus.component.configurator.ComponentConfigurator"
* role-hint="include-project-dependencies"
* @plexus.requirement role=
* "org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup"
* role-hint="default"
*
*/
public class ClassRealmConfigurator extends AbstractComponentConfigurator {
private final static Logger logger = Logger.getLogger(ClassRealmConfigurator.class.getName());
public void configureComponent(Object component, PlexusConfiguration configuration, ExpressionEvaluator expressionEvaluator, ClassRealm containerRealm, ConfigurationListener listener) throws ComponentConfigurationException {
addProjectDependenciesToClassRealm(expressionEvaluator, containerRealm);
converterLookup.registerConverter(new ClassRealmConverter(containerRealm));
ObjectWithFieldsConverter converter = new ObjectWithFieldsConverter();
converter.processConfiguration(converterLookup, component, containerRealm.getClassLoader(), configuration, expressionEvaluator, listener);
}
private void addProjectDependenciesToClassRealm(ExpressionEvaluator expressionEvaluator, ClassRealm containerRealm) throws ComponentConfigurationException {
Set<String> runtimeClasspathElements = new HashSet<String>();
try {
runtimeClasspathElements.addAll((List<String>) expressionEvaluator.evaluate("${project.runtimeClasspathElements}"));
} catch (ExpressionEvaluationException e) {
throw new ComponentConfigurationException("There was a problem evaluating: ${project.runtimeClasspathElements}", e);
}
Collection<URL> urls = buildURLs(runtimeClasspathElements);
urls.addAll(buildAritfactDependencies(expressionEvaluator));
for (URL url : urls) {
containerRealm.addConstituent(url);
}
}
private Collection<URL> buildAritfactDependencies(ExpressionEvaluator expressionEvaluator) throws ComponentConfigurationException {
MavenProject project;
try {
project = (MavenProject) expressionEvaluator.evaluate("${project}");
} catch (ExpressionEvaluationException e1) {
throw new ComponentConfigurationException("There was a problem evaluating: ${project}", e1);
}
Collection<URL> urls = new ArrayList<URL>();
for (Object a : project.getArtifacts()) {
try {
urls.add(((Artifact) a).getFile().toURI().toURL());
} catch (MalformedURLException e) {
throw new ComponentConfigurationException("Unable to resolve artifact dependency: " + a, e);
}
}
return urls;
}
private Collection<URL> buildURLs(Set<String> runtimeClasspathElements) throws ComponentConfigurationException {
List<URL> urls = new ArrayList<URL>(runtimeClasspathElements.size());
for (String element : runtimeClasspathElements) {
try {
final URL url = new File(element).toURI().toURL();
urls.add(url);
} catch (MalformedURLException e) {
throw new ComponentConfigurationException("Unable to access project dependency: " + element, e);
}
}
return urls;
}
}