2

spring-mvcクラスに関するいくつかの基本情報を出力するカスタムMavenレポートプラグインを作成しました。私の内部テストでは、次のようなコードを見ることができます:

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-build-classpathを追加

ただし、これは、mavenのruntimeClasspathElements変数の外部に依存関係がない場合にのみクラスをロードします。これらのクラスをクラスレルムにマージする方法もありますか?

4

2 に答える 2

1

多分使用

/**
 * 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 );
}
于 2012-07-05T15:41:15.403 に答える
1

Ok。上記のコメントの答えを拡張すると、完全な解決策は、ランタイムクラスパスとpoms依存関係からのURLの両方を考慮に入れるConfigurerを使用することです。以下に示すコード

/**
 * 
 * @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;
    }

}
于 2012-07-06T10:35:28.620 に答える