【问题标题】:Can I have maven artifact run maven plugin when it is installed?我可以在安装 maven artifact 时运行 maven 插件吗?
【发布时间】:2013-06-03 19:32:43
【问题描述】:

我创建了一个 Maven 插件(称为 unpackTemplates),它解压缩依赖项 jar 文件并将其中的资源文件(在本例中为模板)复制到项目中的特定位置。

现在,我将以下内容放入每个与模板有依赖关系的项目的 pom 文件中。它看起来像:

<project>
   <groupId>DuriansAreDope</groupId>
   <artifactId>DuriansAreDope</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <build>
      <plugin>
         <groupId>mycorp</groupId>
         <artifactId>unpackTemplates</artifactId>
         <version>1.0</version>
         <executions>
            <execution>
               <configuration>
                  <groupId>com.mycorp.lib</groupId>
                  <version>1.0</version>
                  <artifactId>Lib-With-Templates</artifactId>
               </configuration>
               <goals>
                  <goal>unpackTemplates</goal>
               </goals>
               <phase>generate-sources</phase>
            </execution>
         </executions>
      </plugin>
      <pluginManagement>....</pluginManagement>
   </build>
   <dependencies>
      <dependency>
         <groupId>com.mycorp.lib</groupId>
         <artifactId>Lib-With-Templates</artifactId>
         <version>1.0</version>
      </dependency>
   </dependencies>
</project>

上述项目 pom 为我们工作。它调用插件,插件完成它的工作。但是,我们希望避免将插件部分添加到每个项目的 pom 中。

将插件部分放在依赖项 pom.xml 中会更有意义。这样,除了像往常一样添加&lt;dependency&gt;标签之外,不需要修改项目pom。并且依赖项的插件在安装的任何地方都可以运行。

我看到 Gson 的 pom 文件中包含一个 &lt;build&gt;&lt;plugins&gt;...&lt;/plugins&gt;&lt;/build&gt; 部分。但是,当我将以下 pom 文件提供给我的依赖项时,该插件未运行(尽管已正确找到、下载、安装等依赖项)。

<project>
   <groupId>com.mycorp.lib</groupId>
   <artifactId>Lib-With-Templates</artifactId>
   <version>1.0</version>
   <build>
      <plugin>
         <groupId>mycorp</groupId>
         <artifactId>unpackTemplates</artifactId>
         <version>1.0</version>
         <executions>
            <execution>
               <configuration>
                  <groupId>com.mycorp.lib</groupId>
                  <version>1.0</version>
                  <artifactId>Lib-With-Templates</artifactId>
               </configuration>
               <goals>
                  <goal>unpackTemplates</goal>
               </goals>
               <phase>generate-sources</phase>
            </execution>
         </executions>
      </plugin>
      <pluginManagement>....</pluginManagement>
   </build>
</project>

任何想法我做错了什么,或者如果 Gson pom 只是完全在做其他事情?

(注意:&lt;configuration&gt; 中的 groupId/version/artifactIds 是必需的,因为它们是插件的(字符串)参数;大概如果我让 run-straight-from-dependency 方法起作用,我可以将它们重构掉,但是同样,它甚至没有运行带有参数的 kludgy 版本。)

【问题讨论】:

  • 除了你的插件你知道kind of plugin already exists?。可能是我疏忽了某些事情,但我不确定您的问题到底是什么?因为您写了It would make more sense to have the library know that the last step of its installation is to run the plugin.,但是您在谈论哪个库。也许你可以详细说明一下。
  • 谢谢@khmarbaise。我们找到了 maven“解包”插件,但没有找到您链接到的“远程资源”插件。从参数的结构来看,我会说它们都做类似的事情:将文件从依赖项复制到您的项目中。但是,我们需要对文件有更多的控制权:我们需要重命名它们并将它们放到特定的位置。
  • @khmarbaise:详细说明最后一点:我们不想将调用插件添加到项目中,而是将其添加到依赖项本身。我们希望这意味着插件可以在安装依赖项的任何地方运行,而项目不需要知道插件或如何调用它。

标签: maven maven-plugin m2eclipse m2e


【解决方案1】:

两点:

首先,我同意 khmarbaise 的观点,即您不需要自己的插件来完成这些任务。要解压到特定位置,您可以使用 dependency:unpack-dependencies 和 outputDirectory 参数。 如果您需要更多配置,您可以使用程序集插件来构建您的工件(您想要解包)。

对于第二点,在我看来,您希望在许多项目中使用您的 lib-with-templates 的内容。为什么不将插件和依赖项添加到您需要的每个 pom 中包含的父 pom 中?那么你不需要在“每个pom”中声明它。如果您在每个 pom 中都不需要它,您可以将其放入配置文件并为其选择适当的激活方式。

HTH!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 2012-02-24
    • 1970-01-01
    相关资源
    最近更新 更多