Maven和配置文件:在两个不同的配置文件中使用相同的插件,并且两者同时处于活动状态 [英] Maven and Profiles: Using the same plugin in two different profiles and have both active at the same time

查看:253
本文介绍了Maven和配置文件:在两个不同的配置文件中使用相同的插件,并且两者同时处于活动状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用 frontend-maven-plugin 在我们的版本中使用grunt和bower .使用Frontend Maven插件,我可以在本地安装NPM,使用Bower下载Java库,并运行Grunt来优化和混淆我的代码.

We use the frontend-maven-plugin for using grunt and bower in our builds. With the Frontend Maven Plugin, I can install NPM locally, use Bower to download Java libraries, and run Grunt to optimize and obfuscate my code.

对此进行一些简化:

<plugin>
  <groupId>com.github.eirslett</groupId>
  <artifactId>frontend-maven-plugin</artifactId>
  <version>0.0.24</version>
  <executions>
    <execution>
      <id>install node and npm</id>
      <goals> <goal>install-node-and-npm</goal> </goals>
      ...
    </execution>
    <execution>
      <id>npm-install</id>
      <goals> <goal>npm</goal> </goals>
      ...
    </execution>
    <execution>
      <id>bower-install</id>
      <goals> <goal>bower</goal> </goals>
      ...
    </execution>
    <execution>
      <id>grunt-build</id>
      <goals> <goal>grunt</goal> </goals>
      ...
    </execution>
  </executions>
</plugin>

请注意,最后一次执行是grunt-build,这是将JavaScript文件连接在一起,进行优化(返回,注释和其他内容已删除)并进行模糊处理的地方.

Note that the last execution is grunt-build which is where the JavaScript files are concatenated together, optimized (returns, comments, and other things removed) and obfuscated.

这对于发行版非常有效.但是,开发人员希望在不对JavaScript文件进行串联,优化和混淆的情况下进行部署.这将帮助他们进行调试.为此,我们只需要从此插件的配置中删除grunt-build执行部分.

This works well for releases. However, developers would like to deploy the war without the JavaScript files being concatenated, optimized, and obfuscated. This will help them with debugging. To do that, we merely have to remove the grunt-build execution section from this plugin's configuration.

我想使用个人资料来执行此操作.我可以有一个名为development的配置文件,该文件允许开发人员在没有最后一节的情况下进行构建.我可以简单地复制并粘贴pom.xml文件的这一部分,删除最后执行的代码,然后将其放入单独的配置文件中.全部完成.

I'd like to use profiles to do this. I could have a profile called development that allows developers to do a build without that last section. I could simply copy and paste this section of the pom.xml file, remove that last execution, and put it into a separate profile. All done.

但是,有一种古老的编程格言是:不要重复自己.我会在pom.xml中复制大约50行代码.

However, there's the old programming adage of don't repeat yourself. I'd be duplicating about 50 lines of code in my pom.xml.

我想做的是有一种方法来执行前三个执行,并且只有在这不是开发版本时才执行第四个.在这方面,我还会有其他一些缺点.例如,我将不得不复制JavaScript本身,而不是笨拙的结果.但这很容易做到,而且不会重复代码.

What I'd like to do is have a way to execute the first three executions, and do the fourth only if this isn't a development build. I would have a few other nips and tucks in this as well. For example, I would have to copy in the JavaScripts themselves rather than the grunted results. But, that's easy enough to do and doesn't duplicate code.

这将导致重复的代码,因为我必须使用两种配置来定义frontend-maven-plugin.一次用于development配置文件,一次用于标准发行版.据我所知,我不能说运行frontend-maven-plugin的此配置,如果这不是开发构建,请运行frontend-maven-plugin的此实例,该实例仅能完成艰苦的工作.

This would cause duplicate code because I'd have to define the frontend-maven-pluginwith two configurations. Once for the development profile and once for the standard release build. As far as I know, I can't say, run this configuration of the frontend-maven-plugin, and if this isn't a development build, run this instance of the frontend-maven-plugin which will only do the grunt stuff.

是否可以在pom.xm中两次定义相同的插件,并使Maven以正确的顺序运行两个实例?

Is there a way to define the same plugin twice in the pom.xm, and have Maven run both instances in the right order?

推荐答案

让我从

我的要点是,如果只是跳一下配置文件作为每种条件情况的解决方案,那么在恐怖的恐怖片中,构建将像水蛇一样长出第二个头,您将为此感到后悔.

My takeaway was that if one just jumps on profiles as the solution to every conditional situation, the build will grow a second head like a hydra in a bad horror flick and you'll really regret it.

重点是,概要文件通常是正确使用Maven的权宜之计.除非您知道自己正在做什么,否则应该始终将它们视为万不得已".

The point is that profiles are often an expedient substitute for using Maven correctly. They should always be considered "last resort" unless you know what you are getting yourself into.

在我的情况下,[...].侧面轮廓为此非常有用,但是我可以清楚地看到现在的九头蛇适合放在野兽上,除非绝对必要,否则不打算对它们做更多的事情.

In my situation, [...]. Profiles work great for that, but I can see exactly where the hydra head fits on the beast now and don't intend on doing anything more with them unless absolutely necessary.

我在个人资料方面有丰富的经验.我赞成这种观点.

I have made my experiences with profiles. I second this view.

从概念上讲,您有两个项目具有大多数共同点.这就是Maven具有继承性的POM层次结构发挥作用的地方:

From a conceptual point of view you have two projects that have most of their things in common. This is where Maven's POM hierarchy with its inheritance comes into play:

product ... parent POM
  +- pom.xml ... contains build steps apart from Grunt and all other declarations 
  +- dev
  |    +- pom.xml ... almost empty since everything's inherited from parent
  |    +- ... sources, resources, etc. ...
  +- release
       +- pom.xml ... contains Grunt build step only, everything else's inherited from parent
                      redirecting <build>/<[[test]source|resource>/]|
                          [[test]output|testresource>/]directory> to ../dev/...

请参见 BaseBuild元素集

See The BaseBuild Element Set, Resources and The Build Element Set in the POM Reference for all the build directories to be redirected.

请参见 Maven包含用于插件配置的另一个pom :

唯一正确的答案是使用继承.

The only correct answer is to use inheritance.

我也赞成.

但是,如果您使用此配置文件,我建议将其命名为dev而不是development.您的开发人员将永远感激不已. :)

If you go for the profile nevertheless I recommend to call it dev rather than development. Your devs will be thankful forever. :)

这篇关于Maven和配置文件:在两个不同的配置文件中使用相同的插件,并且两者同时处于活动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆