3

我目前正在尝试让 PlexusContainer 在MavenMaven 或 Eclipse 执行上下文之外给我一个初始化的实例。

为此,我有以下代码(简化):

PlexusContainer container = null;
try {
    container = new DefaultPlexusContainer();
    container.lookup(MavenResolutionTask.class).run();
    container.dispose();
} catch (Exception e) {
    e.printStackTrace();
}

// --- MavenResolutionTask.java:

@Component(role = MavenResolutionTask.class)
// ResolutionTask is an abstract class 
// implementing Runnable and extending CompletableFuture
public class MavenResolutionTask extends ResolutionTask {

    @Requirement
    private Maven maven;
}

鉴于我plexus-component-metadata:process-classes在我的 pom.xml 中有标准插件执行,如果它像这样工作,我会喜欢的。

不幸的是,运行此代码会导致 LookupException,原因如下(为简洁起见,省略了堆栈跟踪):

WARN Sisu - Error injecting: org.apache.maven.project.DefaultProjectBuildingHelper
com.google.inject.ProvisionException: Guice provision errors:

1) No implementation for org.apache.maven.repository.RepositorySystem was bound.
  while locating org.apache.maven.project.DefaultProjectBuildingHelper

WARN Sisu - Error injecting: org.apache.maven.project.DefaultProjectBuilder
com.google.inject.ProvisionException: Guice provision errors:

1) No implementation for org.apache.maven.repository.RepositorySystem was bound.
  while locating org.apache.maven.project.DefaultProjectBuildingHelper
  at ClassRealm[plexus.core, parent: null]
  at ClassRealm[plexus.core, parent: null]
  while locating org.apache.maven.project.ProjectBuildingHelper
  while locating org.apache.maven.project.DefaultProjectBuilder

...

为了修复丢失的绑定并避免自己不得不手动注册 maven 的完整依赖关系图的麻烦,我尝试将包含在我的依赖项中的组件描述符与为我的应用程序生成的组件描述符合并。

为此,我将所有依赖target/dependency项复制到并运行了一些 shell 脚本,以获取包含components.xml.
我将这些依赖项传递给执行,dependency:unpack然后将提取的位置传递给元数据合并器,如下所示:

<plugin>
    <groupId>org.codehaus.plexus</groupId>
    <artifactId>plexus-component-metadata</artifactId>
    <version>1.7.1</version>
    <executions>
        <execution>
            <id>process-classes</id>
            <goals>
                <goal>generate-metadata</goal>
            </goals>
        </execution>
        <execution>
            <id>process-test-classes</id>
            <goals>
                <goal>generate-test-metadata</goal>
            </goals>
        </execution>
        <execution>
            <id>merge-descriptors</id>
            <phase>process-classes</phase>
            <goals>
                <goal>merge-metadata</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <output>${plexus.outputFile}</output>
        <descriptors>
            <descriptor>${plexus.outputFile}</descriptor>
            <!--./aether-transport-wagon-1.1.0.jar.d/META-INF/plexus/components.xml-->
            <descriptor>${project.build.directory}/extracted-sources/aether-transport-wagon/META-INF/plexus/components.xml</descriptor>
            <!--./maven-aether-provider-3.1.0.jar.d/META-INF/plexus/components.xml-->
            <descriptor>${project.build.directory}/extracted-sources/maven-aether-provider/META-INF/plexus/components.xml</descriptor>
            <!--./maven-artifact-manager-2.2.1.jar.d/META-INF/plexus/components.xml-->
            <descriptor>${project.build.directory}/extracted-sources/maven-artifact-manager/META-INF/plexus/components.xml</descriptor>
            <!--./maven-core-3.1.1.jar.d/META-INF/plexus/components.xml-->
            <descriptor>${project.build.directory}/extracted-sources/maven-core/META-INF/plexus/components.xml</descriptor>
            <!--./maven-model-builder-3.1.0.jar.d/META-INF/plexus/components.xml-->
            <descriptor>${project.build.directory}/extracted-sources/maven-model-builder/META-INF/plexus/components.xml</descriptor>
            <!--./maven-plugin-registry-2.2.1.jar.d/META-INF/plexus/components.xml-->
            <descriptor>${project.build.directory}/extracted-sources/maven-plugin-registry/META-INF/plexus/components.xml</descriptor>
            <!--./maven-profile-2.2.1.jar.d/META-INF/plexus/components.xml-->
            <descriptor>${project.build.directory}/extracted-sources/maven-profile/META-INF/plexus/components.xml</descriptor>
            <!--./maven-project-2.2.1.jar.d/META-INF/plexus/components.xml-->
            <descriptor>${project.build.directory}/extracted-sources/maven-project/META-INF/plexus/components.xml</descriptor>
            <!--./maven-settings-builder-3.1.1.jar.d/META-INF/plexus/components.xml-->
            <descriptor>${project.build.directory}/extracted-sources/maven-settings-builder/META-INF/plexus/components.xml</descriptor>
            <!--./org.eclipse.sisu.plexus-0.0.0.M2a.jar.d/META-INF/plexus/components.xml-->
            <descriptor>${project.build.directory}/extracted-sources/sisu-plexus/META-INF/plexus/components.xml</descriptor>
            <!--./plexus-cipher-1.4.jar.d/META-INF/plexus/components.xml-->
            <descriptor>${project.build.directory}/extracted-sources/plexus-cipher/META-INF/plexus/components.xml</descriptor>
            <!--./plexus-interactivity-api-1.0-alpha-6.jar.d/META-INF/plexus/components.xml-->
            <descriptor>${project.build.directory}/extracted-sources/plexus-interactivity-api/META-INF/plexus/components.xml</descriptor>
            <!--./plexus-sec-dispatcher-1.3.jar.d/META-INF/plexus/components.xml-->
            <descriptor>${project.build.directory}/extracted-sources/plexus-sec-dispatcher/META-INF/plexus/components.xml</descriptor>
            <!--./wagon-ssh-1.0.jar.d/META-INF/plexus/components.xml-->
            <descriptor>${project.build.directory}/extracted-sources/wagon-ssh/META-INF/plexus/components.xml</descriptor>
            <!--./wagon-ssh-common-1.0.jar.d/META-INF/plexus/components.xml-->
            <descriptor>${project.build.directory}/extracted-sources/wagon-ssh-common/META-INF/plexus/components.xml</descriptor>
        </descriptors>
    </configuration>
</plugin>

不幸的是,错误消息仍然存在所以我想我的问题是:如何轻松地Maven从 Plexus 获取实例?


PS为了完整起见,我目前正在使用以下依赖项:

[INFO] com.github.vogel612:dependency-analyzer:jar:0.0.1
[INFO] +- org.projectlombok:lombok:jar:1.16.16:compile
[INFO] +- commons-cli:commons-cli:jar:1.4:compile
[INFO] +- org.slf4j:slf4j-simple:jar:1.7.16:compile
[INFO] |  \- org.slf4j:slf4j-api:jar:1.7.16:compile
[INFO] +- org.codehaus.plexus:plexus-utils:jar:3.0.24:compile
[INFO] +- org.eclipse.aether:aether-api:jar:1.1.0:compile
[INFO] +- org.eclipse.aether:aether-util:jar:1.1.0:compile
[INFO] +- org.eclipse.aether:aether-impl:jar:1.1.0:compile
[INFO] |  \- org.eclipse.aether:aether-spi:jar:1.1.0:compile
[INFO] +- org.eclipse.aether:aether-connector-basic:jar:1.1.0:compile
[INFO] +- org.eclipse.aether:aether-transport-file:jar:1.1.0:compile
[INFO] +- org.eclipse.aether:aether-transport-http:jar:1.1.0:compile
[INFO] |  +- org.apache.httpcomponents:httpclient:jar:4.3.5:compile
[INFO] |  |  +- org.apache.httpcomponents:httpcore:jar:4.3.2:compile
[INFO] |  |  \- commons-codec:commons-codec:jar:1.6:compile
[INFO] |  \- org.slf4j:jcl-over-slf4j:jar:1.6.2:compile
[INFO] +- org.eclipse.aether:aether-transport-wagon:jar:1.1.0:compile
[INFO] |  \- org.apache.maven.wagon:wagon-provider-api:jar:1.0:compile
[INFO] +- org.apache.maven:maven-aether-provider:jar:3.1.0:compile
[INFO] |  +- org.apache.maven:maven-model:jar:3.1.0:compile
[INFO] |  +- org.apache.maven:maven-model-builder:jar:3.1.0:compile
[INFO] |  +- org.apache.maven:maven-repository-metadata:jar:3.1.0:compile
[INFO] |  +- org.eclipse.sisu:org.eclipse.sisu.plexus:jar:0.0.0.M2a:compile
[INFO] |  |  +- javax.enterprise:cdi-api:jar:1.0:compile
[INFO] |  |  |  +- javax.annotation:jsr250-api:jar:1.0:compile
[INFO] |  |  |  \- javax.inject:javax.inject:jar:1:compile
[INFO] |  |  +- com.google.guava:guava:jar:10.0.1:compile
[INFO] |  |  |  \- com.google.code.findbugs:jsr305:jar:1.3.9:compile
[INFO] |  |  +- org.sonatype.sisu:sisu-guice:jar:no_aop:3.1.0:compile
[INFO] |  |  |  \- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  |  \- org.eclipse.sisu:org.eclipse.sisu.inject:jar:0.0.0.M2a:compile
[INFO] |  |     \- asm:asm:jar:3.3.1:compile
[INFO] |  \- org.codehaus.plexus:plexus-component-annotations:jar:1.5.5:compile
[INFO] +- org.apache.maven.wagon:wagon-ssh:jar:1.0:compile
[INFO] |  +- com.jcraft:jsch:jar:0.1.44-1:compile
[INFO] |  \- org.apache.maven.wagon:wagon-ssh-common:jar:1.0:compile
[INFO] |     \- org.codehaus.plexus:plexus-interactivity-api:jar:1.0-alpha-6:compile
[INFO] +- org.apache.maven:maven-core:jar:3.1.1:compile
[INFO] |  +- org.apache.maven:maven-settings:jar:3.1.1:compile
[INFO] |  +- org.apache.maven:maven-settings-builder:jar:3.1.1:compile
[INFO] |  +- org.apache.maven:maven-artifact:jar:3.1.1:compile
[INFO] |  +- org.apache.maven:maven-plugin-api:jar:3.1.1:compile
[INFO] |  +- org.codehaus.plexus:plexus-interpolation:jar:1.19:compile
[INFO] |  +- org.codehaus.plexus:plexus-classworlds:jar:2.5.1:compile
[INFO] |  \- org.sonatype.plexus:plexus-sec-dispatcher:jar:1.3:compile
[INFO] |     \- org.sonatype.plexus:plexus-cipher:jar:1.4:compile
[INFO] \- org.apache.maven:maven-project:jar:2.2.1:compile
[INFO]    +- org.apache.maven:maven-profile:jar:2.2.1:compile
[INFO]    +- org.apache.maven:maven-artifact-manager:jar:2.2.1:compile
[INFO]    |  \- backport-util-concurrent:backport-util-concurrent:jar:3.1:compile
[INFO]    +- org.apache.maven:maven-plugin-registry:jar:2.2.1:compile
[INFO]    \- org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9-stable-1:compile
[INFO]       +- junit:junit:jar:3.8.1:compile
[INFO]       \- classworlds:classworlds:jar:1.1-alpha-2:compile
4

1 回答 1

5

开始仔细阅读错误消息可能会有所帮助:

缺少的组件 isorg.apache.maven.repository.RepositorySystem和 not org.eclipse.aether.RepositorySystem。虽然前者已包含在内,但后者仅存在于org.apache.maven:maven-compat.

将它添加到依赖项(并包括它的组件定义)可以解决这里的直接问题。中间结果当前是NoClassDefFoundErrorfor Lorg/apache/maven/artifact/transform/ArtifactTransformationManager,可以通过实际使用正确版本的依赖项来解决。

作为最后一步,现在需要注册一个实例org.eclipse.aether.RepositorySystem和一个实例,这可以使用以下代码来完成:org.eclipse.aether.impl.RemoteRepositoryManager

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

 // ...
container.addComponent(serviceLocator.lookup(RepositorySystem.class)
    , RepositorySystem.class
    , "default");
container.addComponent(serviceLocator.lookup(RemoteRepositoryManager.class)
    , RemoteRepositoryManager.class
    , "default");

现在我们终于可以拥有一个 Maven 实例了。

于 2017-08-26T16:12:37.290 回答