2016-09-16 8 views
1

私はJavaでプラグイン設定を読み込む必要があります。私はaetherライブラリを使用して実行時の依存関係を取得し、時間依存関係などをコンパイルしましたが、petherファイルに基づいてプラグインの設定を読み込むためにaetherを使用できますか?aetherを使用してPOMからプラグイン設定を取得できますか?

私はこのメカニズム

から

some-Prop 8080 
some-prop-admin 8081 

を解決できるようにしたい、この

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

ような何か現​​在、私はこの

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

コンパイル時の依存関係を取得するために現在のコードを投稿できますか? Mavenプラグインまたは外部アプリケーションを作成していますか? – Tunaki

+0

これはJavaアプリケーションで、petherからaetherを使用してプラグインの設定を読みたい –

答えて

0
のようなコンパイルタイの依存関係を取得

もっと簡単な方法があるかどうかは分かりませんが、Aether APIを使用して興味のあるPOM案件を解決し、Model Builder APIを使用してMavenモデルを構築することができます。

まず、あなたのアーティファクトへの座標がPOMアーティファクトでない場合、同じGAVを持ち、"pom"の新しいDefaultArtifactを作成して、それらをPOMアーティファクトに変換する必要があります。成果物を解決するには、リポジトリシステムでresolveArtifactを呼び出して、getArtifact()で結果を取得します。

解決済みの成果物を取得したら、maven-model-builderを使用して、成果物のファイルからMavenモデルを構築できます。 ModelBuilderDefaultModelBuilderFactory.newInstance()ファクトリメソッドで作成できます。

親POMを持つ成果物については、モデルを構築する要求にModelResolverを設定する必要があります。しかし、DefaultModelResolverであるAetherで使用できる唯一の実装は、maven-aether-provider内のpackage-privateです。そのため、Reflection APIを使用して構築する必要があります。 MavenRepositorySystemUtils.newServiceLocator()で返されたserviceLocatorで取得された注入コンポーネントが必要です。これは、Aetherセッションが構築されたのと同じロケータである必要があります。デフォルトでは

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オブジェクトに含まれ、getChild(name)の助けを借りてナビゲートし、名前付き子XMLエレメントを取得し、getValue()を使用してXMLエレメントの値を取得できます。

+0

上記のコードをテストしようとしましたが、pomではなくjarを取得します –

+0

@user_mda JOMではなくPOMを取得するコードを追加しました。 'Artifact pomArtifact'と' buildingRequest.setPomFile(pomArtifact.getFile()); 'を見てください。 – Tunaki

+0

はい同じコードをコピーしましたが、pomArtifact.getFile()からjarが返されるため、解析に失敗します –

関連する問題