簡體   English   中英

插件依賴的Maven依賴管理

[英]Maven dependency management for plugin dependencies

最近,我遇到了以下問題:

當我為我的項目設置依賴項管理時,我使用帶有依賴項的插件的 child-pom,我希望與我的依賴項管理中聲明的依賴項同步。

在根 pom 中,我在依賴管理中聲明:

<dependencyManagement>
    <dependencies>
      ...
        <dependency>
            <groupId>com.google.gwt</groupId>
            <artifactId>gwt-user</artifactId>
            <version>2.4.0</version>
        </dependency>
      ...
    <dependencies>
<dependencyManagement>

在子 pom 中,我有一個需要 gwt-user 的插件:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>gwt-maven-plugin</artifactId>
    <version>2.4.0</version>
    <dependencies>
        <dependency>
            <groupId>com.google.gwt</groupId>
            <artifactId>gwt-user</artifactId>
            <version>2.4.0</version>
        </dependency>
            ...
        </dependencies>
  ...
</plugin>

但是,如果我刪除 gwt-maven-plugin 中使用的依賴版本,則編譯失敗。

有沒有另一種方法來實現它?

PS:有一篇相關的帖子在 maven 和 maven 插件中選擇依賴版本沒有回答我的問題

根據以下鏈接,似乎不可能:

這是我找到的解決方法,我想與大家分享,以防其他人遇到同樣的問題:

在我的根 pom 中,我定義了一個屬性、一個依賴管理和一個插件管理:

<properties>
    <gwtVersion>2.4.0</gwtVersion>
    <gwtMavenPluginVersion>2.4.0</gwtMavenPluginVersion>
</properties>

<dependencyManagement>
   <dependencies>
    ...
    <dependency>
        <groupId>com.google.gwt</groupId>
        <artifactId>gwt-user</artifactId>
        <version>${gwtVersion}</version>
    </dependency>
    ...
   </dependencies>
</dependencyManagement>

<build>    
  <pluginManagement>
        <plugins>
            <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>gwt-maven-plugin</artifactId>
            <version>${gwtMavenPluginVersion}</version>
            <dependencies>
                <dependency>
                    <groupId>com.google.gwt</groupId>
                    <artifactId>gwt-user</artifactId>
                    <version>${gwtVersion}</version>
                </dependency>
                ...
            </dependencies>
            ...
        </plugins>
  ...
  </pluginManagement>
</build>

而在我的子 pom 中,使用插件管理提供的關系(參見Maven2 -插件管理和父子關系的問題),我只是聲明了插件依賴:

<plugin>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>gwt-maven-plugin</artifactId>
</plugin>

現在,如果我更改屬性中的版本,它會自動影響所有直接依賴項和插件依賴項。

為了讓父 POM 控制子使用的插件版本,您應該在父 POM 的<pluginManagement>部分聲明<plugin>

你定義com.google.gwt:gwt-user作為<dependency><dependencyManagement>部。

我不確定您是打算將gwt-user用作插件還是依賴項,但它應該在兩者中列為相同的實體,以便繼承工作。

另一種可能是導入父 POM 的所有依賴項:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>gwt-maven-plugin</artifactId>
    <version>2.4.0</version>
    <dependencies>
        <dependency>
             <groupId>${project.groupId}</groupId>
             <artifactId>${project.artifactId}</artifactId>
             <version>${project.version}</version>
        </dependency>
            ...
        </dependencies>
  ...
</plugin> 

不是最漂亮的解決方案,但工作:-)

在我的例子中,我使用了 jetty maven 插件,依賴於 hsqldb。 我從 sonatype 書中復制了一些示例行(我認為這是我從那里得到這些行的)以使用 jetty 插件,該插件將 groupId 指定為 hsqldb。 我正在使用 hsqldb 的 2.3.2 版。 在dependencyManagement 部分的父pom 和持久性模塊中,groupId 是org.hsqldb。 groupIds 不匹配是導致我收到錯誤的原因,因為在舊的 groupId 下沒有版本 2.3.2。 一旦我將 groupId 從 hsqldb 更改為 org.hsqldb 一切都開始工作了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM