从Plexus获取Maven实例

时间:2017-08-26 11:58:47

标签: java maven dependency-injection plexus

我正在尝试让PlexusContainer在maven或eclipse执行上下文之外给我一个初始化的Maven - 实例。

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

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;
}

鉴于我在我的pom.xml中执行了标准plexus-component-metadata:process-classes插件,如果这样做的话,我会很喜欢。

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

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>

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

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

[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

1 个答案:

答案 0 :(得分:4)

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

缺少的组件是org.apache.maven.repository.RepositorySystem而不是org.eclipse.aether.RepositorySystem。虽然前者已被包括在内,但后者只出现在org.apache.maven:maven-compat中。

将其添加到依赖项(并包括它的组件定义)可以解决此处的紧急问题。中间结果当前为NoClassDefFoundError 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-instance。