我需要在Java中读取插件配置。我使用了aether库来获取运行时依赖性,编译时依赖性等。但是我可以使用aether来读取基于pom文件的插件配置吗?
像这样的东西
<properties>
<servicePort>8080</servicePort>
<adminPort>8081</adminPort>
</properties>
<build>
<plugins>
<plugin>
<groupId>com.company.group</groupId>
<artifactId>my-plugin</artifactId>
<version>0.1-SNAPSHOT</version>
<configuration>
<myConfig>
<somePropName>someProp</somePropName>
<portMappings>
<someProp>${servicePort}</someProp>
<someProp-admin>${adminPort}</someProp-admin>
</portMappings>
</myConfig>
</configuration>
</plugin>
</plugins>
</build>
我希望能够解决
some-Prop 8080
some-prop-admin 8081
来自这个机制
目前我获取像这样
的编译绑定依赖关系Dependency dependency = new Dependency(new DefaultArtifact(
coordinate), COMPILE);
CollectRequest collectRequest = new CollectRequest();
collectRequest.setRoot(dependency);
collectRequest.addRepository(this.aetherSession
.getRemoteRepository());
DependencyNode node = this.aetherSession
.getRepoSystem()
.collectDependencies(this.aetherSession.getRepoSession(),
collectRequest).getRoot();
DependencyRequest dependencyRequest = new DependencyRequest();
dependencyRequest.setRoot(node);
result = this.aetherSession
.getRepoSystem()
.resolveDependencies(this.aetherSession.getRepoSession(),
dependencyRequest).getArtifactResults();
FinalResult.addAll(result);
答案 0 :(得分:0)
我不知道是否有更简单的方法,但您可以使用Aether API来解析您感兴趣的POM工件,然后使用Model Builder API从中构建Maven模型。
首先,如果工件的坐标不是POM工件,则需要通过创建具有相同GAV的新DefaultArtifact
将其转换为POM工件,但类型为{{1 }}。要解析工件,您可以在存储库系统上调用resolveArtifact
,并使用getArtifact()
检索结果。
获得已解析的工件后,可以使用maven-model-builder
从工件的文件构建Maven模型。可以使用DefaultModelBuilderFactory.newInstance()
工厂方法创建"pom"
。
对于具有父POM的工件,需要在构建模型的请求上设置ModelResolver
。但是,使用Aether的唯一可用实现是ModelBuilder
,在DefaultModelResolver
内是包私有的,因此我们需要使用Reflection API来构造它。它需要注入使用maven-aether-provider
返回的serviceLocator
检索的组件。这需要与构建Aether会话的定位器相同。
MavenRepositorySystemUtils.newServiceLocator()
默认情况下,模型构建器未配置为处理插件,因此我们需要调用DefaultArtifact artifact = new DefaultArtifact(coordinate);
Artifact pomArtifact = new DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), "pom", artifact.getVersion());
ArtifactRequest request = new ArtifactRequest(pomArtifact, Arrays.asList(aetherSession.getRemoteRepository()), null);
pomArtifact = aetherSession.getRepoSystem().resolveArtifact(session, request).getArtifact();
ModelBuilder modelBuilder = new DefaultModelBuilderFactory().newInstance();
ModelBuildingRequest buildingRequest = new DefaultModelBuildingRequest();
buildingRequest.setPomFile(pomArtifact.getFile());
buildingRequest.setProcessPlugins(true);
buildingRequest.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
Constructor<?> constr = Class.forName("org.apache.maven.repository.internal.DefaultModelResolver").getConstructors()[0];
constr.setAccessible(true);
ModelResolver modelResolver = (ModelResolver) constr.newInstance(session, null, null,
serviceLocator.getService(ArtifactResolver.class),
serviceLocator.getService(VersionRangeResolver.class),
serviceLocator.getService(RemoteRepositoryManager.class), request.getRepositories());
buildingRequest.setModelResolver(modelResolver);
Model model = modelBuilder.build(buildingRequest).getEffectiveModel();
Xpp3Dom pluginConfiguration = (Xpp3Dom) model.getBuild().getPluginsAsMap().get("com.company.group:my-plugin").getConfiguration();
Xpp3Dom myConfig = pluginConfiguration.getChild("myConfig");
System.out.println(myConfig.getChild("somePropName").getValue()); // prints "someProp"
。获得有效模型后,配置将包含在setProcessPlugin(true)
对象中,您可以在Xpp3Dom
的帮助下导航,以获取指定的子XML元素,并getChild(name)
获取XML元素的值。