2015-11-29 27 views
1

在我的項目我連着公地lang3對插件和commons-io的項目:如何掃描maven插件的依賴關係?

<build> 
    <plugins> 
     <plugin> 
      <groupId>@[email protected]</groupId> 
      <artifactId>@[email protected]</artifactId> 
      <version>@[email protected]</version> 
       (...) 
      <dependencies> 
       <dependency> 
        <groupId>org.apache.commons</groupId> 
        <artifactId>commons-lang3</artifactId> 
        <version>3.4</version> 
       </dependency> 
      </dependencies> 
     </plugin> 
    </plugins> 
</build> 
<dependencies> 
    <dependency> 
     <groupId>commons-io</groupId> 
     <artifactId>commons-io</artifactId> 
     <version>2.4</version> 
    </dependency> 
</dependencies> 

在我的自定義插件(我的魔力之內),我能找到的commons-io的

@Component 
private MavenProject project; 
(...) 
project.getDependecies(); // [{groupId=commons-io, artifactId=commons-io... 

如何找到commons-lang3?

+0

運行'mvn help:effective-pom' –

回答

0

使用Maven的API,從MavenProject你可以生成實例,並從中再配置的插件列表,從每個插件對象,你就可以擁有它依賴如下:

@Component 
private PluginDescriptor pluginDescriptor; 
(...) 

List<Plugin> plugins = project.getBuild().getPlugins(); 
for (Plugin p : plugins) { 
    if (p.getId().equals(pluginDescriptor.getId())) { 
     List<Dependency> pluginDependencies = p.getDependencies(); 
     // your logic here 
     break; 
    } 
} 

如果您真的要覆蓋所有的情況下,還可以掃描當前活動的配置文件配置的插件如下:

List<Profile> profiles = project.getActiveProfiles(); 
for (Profile p : profiles) { 
    // from personal experience, don't forget this check! 
    if (p.getBuild() != null) { 
     checkAsShownAbove(p.getBuild().getPlugins()); 
    } 
} 

希望有所幫助。

0

由於commons-lang3僅作爲特定插件的依賴項聲明,因此在編譯期間不可用。您必須將commons-lang3明確定義爲依賴項(如果它未包含爲傳遞依賴項),則類似於您定義commons-io的方式。

+0

但是,maven編譯器插件允許支持非標準的編譯器。這些編譯器被定義爲插件依賴性,它們是「工作」的。我想實現類似的東西。我會更新說明更清楚 – michaldo