5

我有以下涉及 plexus 容器的简化示例:

package de.vogel612.depanalyzer;

import de.vogel612.depanalyzer.dependency.MavenResolutionTaskReal;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
import org.apache.maven.artifact.repository.layout.FlatRepositoryLayout;
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
import org.codehaus.plexus.DefaultPlexusContainer;
import org.codehaus.plexus.PlexusContainer;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.connector.basic.BasicRepositoryConnectorFactory;
import org.eclipse.aether.impl.RemoteRepositoryManager;
import org.eclipse.aether.spi.connector.RepositoryConnectorFactory;
import org.eclipse.aether.spi.connector.transport.TransporterFactory;
import org.eclipse.aether.spi.locator.ServiceLocator;
import org.eclipse.aether.transport.file.FileTransporterFactory;
import org.eclipse.aether.transport.http.HttpTransporterFactory;

public class MCVE {

    private static final ServiceLocator serviceLocator = MavenRepositorySystemUtils.newServiceLocator()
            .addService(RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class)
            .addService(TransporterFactory.class, FileTransporterFactory.class)
            .addService(TransporterFactory.class, HttpTransporterFactory.class);


    private static final PriorityBlockingQueue<ResolutionTask> taskQueue = new PriorityBlockingQueue<>();
    private static final ExecutorService consumer = Executors.newSingleThreadExector();
    // actually initialized in main, since number of threads is configurable
    private static ExecutorService workers = Executors.newFixedThreadPool(threads, runnable -> {
            Thread result = new Thread(runnable, "WorkerQueue Thread");
            result.setDaemon(true);
            return result;
        });

    public static void main(String[] args) throws Exception {

        PlexusContainer container = new DefaultPlexusContainer();
        // bind eclipse aether RepositorySystem
        container.addComponent(serviceLocator.getService(RepositorySystem.class), RepositorySystem.class, "default");
        container.addComponent(serviceLocator.getService(RemoteRepositoryManager.class), RemoteRepositoryManager.class, "default");

        container.addComponent(new DefaultRepositoryLayout(), ArtifactRepositoryLayout.class, "default");
        container.addComponent(new FlatRepositoryLayout(), ArtifactRepositoryLayout.class, "flat");

        taskQueue.put(container.lookup(MavenResolutionTask.class));
        // ... 
    }
}

请注意最后两container.addComponent行。现在,当我在以下设置中使用容器时,我观察到了一些非常奇怪的行为:

public class ResolutionTask extends CompletableFuture<List<SomeType>> implements Runnable { 
    // actually contains an override for run, which does error handling
}

public class MavenResolutionTask extends ResolutionTask {
    @Requires
    Maven mavenInstance;

    @Override
    public void run() {
        // use the maven instance to compute some stuff:
        complete(Collections.singletonList());
    }
}

此代码工作正常并按预期执行:

List<DependencyResult> results = new ArrayList<>();
while (!taskQueue.isEmpty()) {
    ResolutionTask currentTask = taskQueue.poll();
    workers.execute(currentTask);
    results.addAll(currentTask.join());
}
results.forEach(System.out::println);

一旦这个简单的工作队列处理程序按如下方式移出主线程,我就会收到一个异常:

consumer.execute(() -> {
    List<DependencyResult> results = new ArrayList<>();
    while (!taskQueue.isEmpty()) {
        ResolutionTask currentTask = taskQueue.poll();
        workers.execute(currentTask);
        results.addAll(currentTask.join());
    }
    results.forEach(System.out::println);
});

Exception in thread "pool-1-thread-1" java.util.concurrent.CompletionException: java.lang.RuntimeException: Exception occurred during maven invocation
    at java.util.concurrent.CompletableFuture.reportJoin(CompletableFuture.java:375)
    at java.util.concurrent.CompletableFuture.join(CompletableFuture.java:1934)
    at de.vogel612.depanalyzer.Main.lambda$main$1(Main.java:109)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.RuntimeException: Exception occurred during maven invocation
    at de.vogel612.depanalyzer.dependency.MavenResolutionTask.runImpl(MavenResolutionTask.java:35)
    at de.vogel612.depanalyzer.dependency.ResolutionTask.run(ResolutionTask.java:27)
    ... 3 more
    Suppressed: org.apache.maven.InternalErrorException: Internal error: java.lang.NullPointerException
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:164)
        at de.vogel612.depanalyzer.dependency.MavenResolutionTask.runImpl(MavenResolutionTask.java:32)
        ... 4 more
    Caused by: java.lang.NullPointerException
        at org.apache.maven.RepositoryUtils.getLayout(RepositoryUtils.java:217)
        at org.apache.maven.RepositoryUtils.toRepo(RepositoryUtils.java:201)
        at org.apache.maven.RepositoryUtils.toRepos(RepositoryUtils.java:191)
        at org.apache.maven.project.DefaultProjectBuilder$InternalConfig.<init>(DefaultProjectBuilder.java:684)
        at org.apache.maven.project.DefaultProjectBuilder.build(DefaultProjectBuilder.java:340)
        at org.apache.maven.DefaultMaven.collectProjects(DefaultMaven.java:637)
        at org.apache.maven.DefaultMaven.getProjectsForMavenReactor(DefaultMaven.java:586)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:229)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:152)
        ... 5 more

这个异常的“根本原因”指向一个实际上没有帮助的地方。当 maven 尝试初始化远程存储库时,它访问 a Map<String, ArtifactRepositoryLayout>,它应该包含条目{"default", new DefaultRepositoryLayout()},{"flat", new FlatRepositoryLayout()},但

这个 Map 可以在maven-compat 的 LegacyRepositorySystem中找到,但是在访问 taskQueue 时它没有正确初始化consumer。取出方程式
时,代码确实会产生正确的输出......consumer

为什么会这样?

4

0 回答 0