Grails Integration with Maven

Source: Internet
Author: User
Tags xmlns grails java web

This article mainly introduces Grails and Maven integration. After the Grails 2.1 release, the integration of Maven has been greatly improved. This article provides an example of how Grails integrates with MAVEN, enabling readers to learn how to build a Grails project using MAVEN.

2 Reviews:

Yang Jian, software engineer, IBM

September 09, 2013 Content Overview

Grails profile: Grails is an open-source WEB development framework that is developed using dynamic language Groovy and integrates with projects that are developed in Java. Grails fully embodies the principle that the contract is better than the configuration, minimizing the configuration of the project. The current mainstream Java Web frameworks are too complex, such as Structs, Spring, and WebWork, and the users of these frameworks need to write and maintain many configuration files, and the projects that use these frameworks have different, no uniform standards. Grails's presence solves these problems, and users can find that there are few modifications and additions to the configuration file in Grails.

Maven Introduction: MAVEN is a project management tool that, based on the project object model, enables MAVEN to manage the building of projects, reports, and document generation. Maven is primarily used for projects developed in Java.

Grails support for Maven: Before Grails 2.1, the Grails plugin must be configured in the Buildconfig.groovy file, and the dependency on Java must be specified in the Pom file.

After Grails 2.1, support for Maven integration has been greatly improved, and now the Pom file also supports referencing Grails plugins, as shown in Listing 1: Listing 1. Pom.xml Support for Grails

<dependency> 
   <groupId>org.grails.plugins</groupId> 
    <artifactid>hibernate</ artifactid> 
    <version>2.1.0.RC1</version> 
    <type>zip</type> 
    <scope> Compile</scope> 
 </dependency>

The Maven plugin now recognizes other plugins and Jar packages that Grails relies on. And Grails adds a new command: Create-multi-project-build, which is called in a directory that includes multiple Grails applications or plug-ins, generates a Maven build on complex projects. After Grails2.1, Maven management for existing Grails projects requires only the CREATE-POM command to create the pom file corresponding to the project.

Development environment: Grails 2.1, Maven 3, JDK1.6.

Back to top single Grails project integration with Maven

Run the Create-app command first to generate the Grails project, and then build the Pom file with the command Create-pom at the root of the Grails project. A specific example is shown in Listing 2: Listing 2. Generating a Pom file with command Create-pom

	 #> grails create-app my-app       
	 #> cd my-app       
	 #> grails create-pom build.groupid

We give a brief description of the above three commands, the first one is the Grails Project Creation command, which will create a grails app with a user-specified name, and the name we specify is My-app, so the Grails app is known as My-app. The second command goes to the root of the my-app we created, because the third command needs to run under Grails's root directory. The third command is to create a POM file for the Grials app so that the Grails app can be built with Maven, and the parameter GroupID is the project that specifies the application's membership.

After running the Create-pom command, we can see that the file Pom.xml is generated in the My-app root directory, which is shown in Listing 3: listing 3. pom.xml files generated in the My-app root directory

<?xml version= "1.0" encoding= "Utf-8"?> 
 <project xmlns= "http://maven.apache.org/POM/4.0.0"
 xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance"        
      xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 
      Http://maven.apache.org/maven-v4_0_0.xsd ">   
	 <modelVersion>4.0.0</modelVersion>   
	 < groupid>build.groupid</groupid>   
	 <artifactId>my-app</artifactId>   
	 <packaging> grails-app</packaging>   
    <version>0.1</version>   
	 <name>my-app</name>   
	 <description>0.1</description> ... 
 </project>

You can now run MVN commands for simple tests, such as: MVN package, you can see the results as shown in Listing 4: listing 4. Run the MVN command for a simple test

| Loading Grails 2.1.0 | Configuring Classpath | 
 Running pre-compiled script. | Environment set to prod ...................... ... | Packaging Grails Application ... | Compiling 1 source files ...... | 
 Compiling 3 GSP files for package [MYAPP]: | 
 Compiling 4 GSP files for package [databasemigration]: | Building WAR file ..... ..... ....... ................... | Done creating WAR Target\my-app-0.1.war [INFO]-------------------------------------------------------------------- ----[INFO] BUILD SUCCESS [INFO]------------------------------------------------------------------------[INFO] Tot Al time:2:13.841s [info] finished at:tue Oct 13:31:29 CST [INFO] Final memory:16m/34m [INFO]------------ ------------------------------------------------------------

As you can see from the results, we can use Maven's commands to build Grails projects. Other Maven standard commands can also be run. For example, the command in Listing 5: listing 5. Other Maven standard commands

compile– compiling a Grails project package– build a Build
 War file install– build the
 War file and install it in your local Maven library
 test– run the project test
 Clea N-Cleanup Grails project

We briefly describe the process that the Maven command executes. When entering the MVN clean package at the root of the project, MAVEN will delegate this command to the Grails maven plugin to invoke the grails clean and grails package commands. When you run these commands, all dependencies are from the Maven configuration file, not the Grails profile. table 1. Description of POM file contents

Properties Description:
Modelversion The version of the POM model, MAVEN2/3, with a value of 4.0.0
GroupId The group that the project belongs to, the general project is composed of several modules, so the project (group) that each module belongs to is set by this parameter.
Artifactid The project is defined in the group, consistent with the App.name name in Application.properties under the Grails project root.
Packaging The value is Grails-app, which indicates that apps are grails apps, so packaging is packaged with grails apps.
Version The current version of the project must be consistent with the app.version content in Application.properties.
Name The name of the project.
Description Description of the project.
Properties Set the Grails version number.
Dependencies When you run the command Create-pom, Grails builds the packages, plugins, and so on that the project depends on Buildconfig.groovy in the pom file.
Build Build the settings. This section includes the Maven plugin for building Grails projects.
Repositories The warehouse settings for the project. Grails has its own Maven repository to store plug-ins and its core dependencies.
Profiles Custom build environment.

Back to top Maven archetype usage

It's about integrating Maven into an existing Grails project, and we can also use Maven's archetype to generate a grails maven project. When using archetype, we do not need to install grails, just enter the following command, as shown in Listing 6: listing 6. Generating grails maven with maven archetype

MVN archetype:generate-darchetypegroupid=org.grails 
   -darchetypeartifactid=grails-maven-archetype 
   - darchetypeversion=2.1.0 
   -dgroupid=example-dartifactid=my-app

After you use the maven archetype command, you can quickly build the skeleton of your project. Where parameters Darchetypegroupid, Darchetypeartifactid and darchetypeversion Specify a template for the Grails 2.1 to generate the project skeleton. The DgroupId and DartifactId parameters specify the GroupId and Artifactid to create the app, and we don't specify the version of the app, so when you run the command, you are prompted for version. A new Maven project is generated when you enter a value that you want to enter and then enter "Y" to press ENTER. In this project, POM files, src folders, and other files are generated. In the resulting project skeleton, we see a structure that is not like the Grails project, so we need to modify the POM file and run the MVN initialize command.

Open My-app/pom.xml and find Maven-compiler-plugin, as shown in Listing 7: listing 7. Find Maven-compiler-plugin

<plugin> 
 <artifactId>maven-compiler-plugin</artifactId> 
 <configuration> 

	 <source>1.5</source>  <target>1.5</target>
 </configuration> 
 </plugin>

We need to change the JDK version from 1.5 to 1.6 because we are installing JDK1.6 locally. Then we enter the command MVN initialize: As shown in Figure 1. Input command MVN initialize

When this command is finished, we open the My-app directory and find a complete GRAILS project structure catalog already generated.

Back to top multi-module Grails project with Maven integration

Start by creating 3 grails projects with the following commands: Figure 2. Create 3 grails commands

Then we use command Create-multi-project-build to make multi-module projects with M

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.