gpt4 book ai didi

java - 从 Plexus 获取 Maven 实例

转载 作者:行者123 更新时间:2023-11-30 06:47:29 26 4
gpt4 key购买 nike

我目前正在尝试让 PlexusContainer 给我一个初始化的 Maven - 在 maven 或 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;
}

鉴于我在我的 pom.xml 中执行了标准的 plexus-component-metadata:process-classes 插件,如果它能像这样工作的话我会很高兴。

不幸的是,运行此代码会导致 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

...

为了补救缺失的绑定(bind)并避免我自己经历手动注册 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 实例?


附言为了完整起见,我目前使用以下依赖项:

[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

最佳答案

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

缺少的组件是 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 实例了。

关于java - 从 Plexus 获取 Maven 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45895098/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com