Jai’s Weblog – Tech, Security & Fun…

Tech, Security & Fun…

  • Jaibeer Malik

    Jaibeer Malik
  • View Jaibeer Malik's profile on LinkedIn
  • Subscribe

  • Feedburner

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 27 other followers

  • Archives

  • Categories

  • Stats

    • 264,984
  • Live Traffic

Running grunt from your maven build

Posted by Jai on October 24, 2013

This post shares a quick tip on running grunt from your maven build. You would be able to generate resources using grunt and handle those in your war files.

Grunt

Grunt  is javascript task runner. Have a look at grunt getting started  for further details.

Grunt Maven Integration

You can integrate grunt with maven using maven-antrun-plugin, as shared on github

Generate resource using grunt

To generate resource and to run grunt, you can use maven-antrun-plugin.

<plugins>
	<plugin>
	 <groupId>org.apache.maven.plugins</groupId>
	  <artifactId>maven-antrun-plugin</artifactId>
	  <version>1.7</version>
	  <executions>
		  <execution>
			  <phase>generate-sources</phase>
			  <configuration>
				  <target name="building">
					  <echo>
					  </echo>
					  <exec executable="cmd" dir="${project.basedir}" osfamily="windows" failonerror="true">
						  <arg line="/c npm config set color false"/>
					  </exec>
					  <exec executable="bash" dir="${project.basedir}" osfamily="unix" failonerror="true">
						  <arg line="-c 'npm config set color false'"/>
					  </exec>
					  <exec executable="cmd" dir="${project.basedir}" osfamily="windows" failonerror="true">
						  <arg line="/c npm install"/>
					  </exec>
					  <exec executable="bash" dir="${project.basedir}" osfamily="unix" failonerror="true">
						  <arg line="-c 'npm install'"/>
					  </exec>
					  <echo>
					  </echo>
					  <exec executable="cmd" dir="${project.basedir}" osfamily="windows" resultproperty="cmdresult" output="grunt.status">
						  <arg line="/c grunt" />
					  </exec>
					  <exec executable="bash" dir="${project.basedir}" osfamily="unix" resultproperty="cmdresult" output="grunt.status">
						  <arg line="-c 'grunt'"/>
					  </exec>

					  <loadfile property="grunt.status" srcFile="grunt.status"/>
					  <echo>${grunt.status}</echo>
					  <delete file="grunt.status" quiet="true"/>
					  <condition property="cmdsuccess">
						  <equals arg1="${cmdresult}" arg2="0"/>
					  </condition>
					  <fail unless="cmdsuccess"/>
				  </target>
			  </configuration>
			  <goals>
				  <goal>run</goal>
			  </goals>
		  </execution>
	  </executions>
	</plugin>
</plugins>

Check out the original code on github https://gist.github.com/nadavdav/5308453

Read further on making grunt maven http://addyosmani.com/blog/making-maven-grunt/ to integrate further with war files in your project.

You can use maven profile feature to control on which environment it should be build automatically, eg. build using grunt only on integration environment and not on dev environment by default. This is also dependent on assumption that you need to have grunt/node envirment setup separately.

Copy generated resources to war files

Assuming the grunt resources are already generated in previous step. You can copy the generated resource during war builidng process. You can use maven-resources-plugin to copy generated resources

<plugin>
	            <groupId>org.apache.maven.plugins</groupId>
	            <artifactId>maven-resources-plugin</artifactId>
	            <version>2.6</version>
	            <executions>
		          <execution>
		            <phase>validate</phase>
		            <goals>
		              <goal>copy-resources</goal>
		            </goals>
		            <configuration>
		              <outputDirectory>${project.build.directory}/${project.build.finalName}/assets</outputDirectory>
		              <resources>          
		                <resource>
		                  <directory>${project.basedir}/../../gruntproject</directory>
		                  <filtering>true</filtering>
		                  <includes>
          					<include>css/**</include>
          					<include>js/**</include>
          				  </includes>
		                </resource>
		              </resources>
		            </configuration>            
		          </execution>
		        </executions>
	        </plugin>

Hope this will help you to integrate grunt building with your existing maven build structure.

About these ads

One Response to “Running grunt from your maven build”

  1. Eirik said

    Hey, thanks for sharing the info!
    Here’s a maven plugin I wrote that does roughly the same thing: https://github.com/eirslett/frontend-maven-plugin
    What do you think about it?

    BTW; you can specify a target directory in Grunt directly if you want, then you won’t need the copy-resources plugin.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 27 other followers

%d bloggers like this: