简体   繁体   English

安装了Maven构件后可以运行Maven插件吗?

[英]Can I have maven artifact run maven plugin when it is installed?

I have created a Maven plugin (called unpackTemplates) that unpacks a dependency jar file and copies resource files (in this case, templates) from it into a specific location in a project. 我创建了一个Maven插件(称为unpackTemplates),该插件将依赖项jar文件解压缩,并将资源文件(在本例中为模板)从中复制到项目中的特定位置。

Right now, I put the following into the pom file of every project that has a dependency with templates. 现在,我将以下内容放到每个具有模板依赖项的项目的pom文件中。 It looks like: 看起来像:

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

The above project pom works for us. 上面的pom项目对我们有用。 It calls the plugin and the plugin does it's job. 它调用插件,插件完成它的工作。 However, we'd like to avoid adding the plugin section to the pom of every project. 但是,我们希望避免将插件部分添加到每个项目的pom中。

It would make more sense to put that plugin section in the dependencies pom. 将插件部分放在依赖项pom中会更有意义。 This way the project pom does not need to be modified beyond adding the <dependency> tags as usual. 这样,除了像往常一样添加<dependency>标记外,无需修改项目pom。 And the dependency has it's plugin run wherever it is installed. 依赖关系使其插件可以在任何安装位置运行。

I've seen that the pom file for Gson contains a <build><plugins>...</plugins></build> section in it. 我已经看到Gson的pom文件中包含一个<build><plugins>...</plugins></build>部分。 When I give my dependencies the following pom files, however, the plugin is not run (although the dependency is found, downloaded, installed, etc correctly). 但是,当我将以下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>

Any ideas what I'm doing wrong, or if the Gson pom is simply doing something else entirely? 有什么想法我做错了,还是Gson pom只是在完全做其他事情?

(NB: The groupId/version/artifactIds in <configuration> are necessary because they are (string) parameters to the plugin; presumably if I got the run-straight-from-dependency approach working I could refactor them away, but again, it's not even running the kludgy version with parameters.) (注意: <configuration>中的groupId / version / artifactIds是必需的,因为它们是插件的(字符串)参数;想必如果我能运行从依赖直接运行的方法,我可以将其重构掉,但是再次)甚至没有运行带有参数的kludgy版本。)

two points: 两点:

First I agree with khmarbaise in that you don't need a plugin of your own for those tasks. 首先,我同意khmarbaise,因为您不需要自己的插件即可执行这些任务。 To unpack to a specific location you can use dependency:unpack-dependencies and outputDirectory parameter. 要解压到特定位置,可以使用dependency:unpack-dependencies和outputDirectory参数。 If you need more configuration you can use the assembly plugin to structure your artifact (which you want to unpack). 如果需要更多配置,则可以使用Assembly插件来构造您的工件(要解压缩)。

For the second point it seems to me that you want to use the contents of your lib-with-templates in many projects. 对于第二点,在我看来,您想在许多项目中使用lib-with-templates的内容。 Why don't you add the plugin and dependency to a parent pom which you include in every pom where you need it? 为什么不将插件和依赖项添加到您需要的每个pom中包含的父pom中呢? Then you don't need to declare it in "every pom". 然后,您无需在“每个pom”中声明它。 If you don't really need it in every pom you can put it in a profile and choose a proper activation for it. 如果您并不是真的需要每个pom,都可以将其放在配置文件中并为其选择适当的激活方式。

HTH! HTH!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以为Maven工件提供4部分版本吗? - Can I have a 4 part version for a maven artifact? 我可以从docker运行maven工件吗? - Can I run maven artifact from docker maven-install-plugin:我可以定义自定义包装类型,但是在repo中将工件安装为jar吗? - maven-install-plugin: Can i define a custom packaging type but get the artifact installed as jar in the repo? 如果我已经安装了maven eclipse插件,可以安装maven吗? - Is it ok to Install maven if I already have installed maven eclipse plugin? 我在使用Maven jar插件创建第二个jar工件时遇到问题 - I have issues to create a second jar artifact with Maven jar plugin 作为Maven工件,我可以拥有什么?如何使用它? - What can I have as a Maven artifact and how can I use it? 使用maven程序集插件,如何将工件复制到目录? - With the maven assembly plugin, how can I copy an artifact to a directory? Maven依赖插件-使用依赖解压缩时如何确保工件存在 - Maven dependency plugin - How can I ensure that an artifact is present when using dependency-unpack 如何使用Artifactory插件和Credentials插件将Jenven的Maven工件部署到Artifactory时解决401失败? - How can I resolve a 401 failure when deploying a Maven artifact from Jenkins to Artifactory using the Artifactory Plugin and the Credentials Plugin? 如何使maven-metadata.xml与maven部署的工件具有相同的时间戳? - How can I make maven-metadata.xml have the same timestamp as the artifact wen deployed with maven?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM