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.
Like this with some simplification:
<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>
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.
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.
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.
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
.
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.
This would cause duplicate code because I'd have to define the frontend-maven-plugin
with 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.
Is there a way to define the same plugin twice in the pom.xm
, and have Maven run both instances in the right order?
You are in luck, the
grunt
goal of that plugin defines askip
property so you only need to set this property totrue
in your custom profile.As such, you need to create a profile
development
that sets a custom propertyskipGrunt
totrue
and a default profile that sets it tofalse
.Then, in the plugin configuration of the
grunt
goal, you can addThis remark can be made more general: when you need to skip a specific execution of a plugin, there's generally a custom
skip
property that you can set totrue
.Let me cite from the Maven forum:
I have made my experiences with profiles. I second this view.
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:
See The BaseBuild Element Set, Resources and The Build Element Set in the POM Reference for all the build directories to be redirected.
See Maven include another pom for plugin configuration:
I second that, as well.
If you go for the profile nevertheless I recommend to call it
dev
rather thandevelopment
. Your devs will be thankful forever. :)