Maven Plugins

5 Maven Plugins

Maven is actually a plugin execution framework where every task is actually done by plugins.

Maven Plugins are generally used to :

  • create jar file
  • create war file
  • compile code files
  • unit testing of code
  • create project documentation
  • create project reports

A plugin generally provides a set of goals and which can be executed using following syntax:

mvn [plugin-name]:[goal-name]

For example, a Java project can be compiled with the maven-compiler-plugin’s compile-goal by running following command.

mvn compiler:compile

5.1 Plugin Types

Maven provided following two types of Plugins:

Type
Description
Build plugins
They execute during the build and should be configured in the <build/> element of pom.xml
Reporting plugins
They execute during the site generation and they should be configured in the <reporting/> element of the pom.xml

Following is the list of few common plugins:

Plugin
Description
clean
Clean up target after the build. Deletes the target directory.
compiler
Compiles Java source files.
surefire
Run the JUnit unit tests. Creates test reports.
jar
Builds a JAR file from the current project.
war
Builds a JAR file from the current project.
javadoc
Generates Javadoc for the project.
antrun
Runs a set of ant tasks from any phase mentioned of the build.

 

5.2 How do I deploy a maven web application to Tomcat?

The Tomcat Maven Plugin (homepage at http://mojo.codehaus.org/tomcat-maven-plugin/introduction.html ), can be used to perform tasks such as deploying, redeploying, and undeploying a war file to Tomcat using the Tomcat Manager application.

After it has been set up, all you need to do is the following command to build and deploy a project to Tomcat using the maven tomcat plugin:

mvn tomcat:deploy

Now, let’s see how to set up a maven project to use the maven tomcat plugin.

First off, to access the Tomcat Web Application Manager, you must use a name/password with a manager role in Tomcat’s tomcat-users.xml file. In my Tomcat’s tomcat-users.xml file, the name “test” with password “test” fulfills this requirement.

tomcat-users.xml

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="manager"/>
<user username="test" password="test" roles="manager"/>
</tomcat-users>

In my settings.xml file, I need to create a server entry specifying a name for my tomcat server and the manager name and password. I called my tomcat “mytomcat” and used “test” for username and “test” for password.

settings.xml server entry

<server>
<id>mytomcat</id>
<username>test</username>       
<password>test</password>
</server>

Next, I added a tomcat-maven-plugin entry to the “mywebapp” project’s pom.xml. I specified the url to my Tomcat manager (by default this is http://localhost:8080/manager). I specified the server to be “mytomcat” so that the “mytomcat” server name and password in server.xml would be used to connect to Tomcat. I specified a path element to be “/mywebapp”, although this wasn’t really necessary since the build finalName is the same in this case.

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>   
<configuration>
<url>http://localhost/manager</url>                    
<server>mytomcat</server>                    
<path>/mywebapp</path>
</configuration>
</plugin>

complete pom.xml file is shown here:

mywebapp/pom.xml

<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>com.maventest</groupId>
<artifactId>mywebapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>mywebapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId> 
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>  
</dependency>  
<dependency>
<groupId>javax.servlet</groupId> 
<artifactId>servletapi</artifactId> 
<version>2.5</version>
<scope>provided</scope>
 </dependency>
</dependencies>
<build>
<finalName>mywebapp</finalName> 
<plugins> 
<plugin> 
<groupId>org.codehaus.mojo</groupId> 
<artifactId>tomcat-maven plugin</artifactId> 
<configuration>
 <url>http://localhost:8080/manager</url>
<server>mytomcat</server>
<path>/mywebapp</path> 
</configuration> 
</plugin>
<plugin> 
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse plugin</artifactId>
<inherited>true</inherited>
<configuration>
<classpathContainers>
<classpathContainer>org.eclipse.jdt.launching.
JRE_CONTAINER</classpathContainer>
<classpathContainer>org.eclipse.jdt.USER_LIBRARY/TOMCAT_6.0.14_LIBRARY</classpathContainer>
</classpathContainers>
</configuration>
</plugin>
</plugins>
</build>
</project>

 

Now, before using the tomcat maven plugin, I’ll log on to my manager application manually to see what happens. I log on with username “test” and password “test”. In this list of applications, the “mywebapp” application isn’t present yet.

tomcat1

Now, I’d like to create an Eclipse external tool configuration to perform a deploy of a selected project using the tomcat maven plugin. To do so, I first perform a “clean” phase to clean the selected project. After this I perform the “tomcat:deploy” goal to build and deploy the war file.

Name:
mvn clean tomcat~deploy
Location:
C:\apache-maven-3.3.9\bin\mvn.cmd
Working Directory:
${project_loc}
Arguments:
clean tomcat:deploy

The external tool configuration is shown here.

apache2

I selected the “mywebapp” project in my Eclipse Navigator view and then ran the external tool that we just created. It generated the following console output:

apache3

In the Tomcat Manager, if I click the “List Applications” link, the “mywebapp” application appears at the bottom of the list.

tomcat2

5.3 How to Create a .war file from Eclipse using Maven Plugin

Motivation:Our requirement here is to generate single .war file using Maven which you could run on Apache Tomcat .

step1:Create Dynamic maven Web Project in to Eclipse ,

File->new->other->maven project->select create a simple project->enter groupid,artifactid->select packagin WAR->click finish.

If you dont find web.xml then you can do it by Pradeepit-DynamicWebProject –> RightClick –> Java EE Tools –> Generate Deployment Descriptor Stub.

 

 war1

You should see web.xml file immediately after that.Here is a project structure.

war2

step2:Open your pom.xml file and update it with below code. We are packaging project as war using <packaging>war</packaging>.

<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/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>in.com.pradeepit</groupId>
 <artifactId>Pradeepit-DynamicWebProject</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
</project>

Step3: Now Right Click on Project -> Run-As ->Maven Build

war3

Step4:Maven clean install on project.

war4

clean install:This command tells Maven to build all the modules, and to install it in the local repository. The local repository is created in your home directory (or alternative location that you created it), and is the location that all downloaded binaries and the projects you built are stored.

That’s it! If you look in the target subdirectory, you should find the build output and the final library or application that was being built.

 

Step5: Checkout your Build result in Eclipse console.

If u face the below error while running the maven build.

follow these steps

go to run configuration, create new maven build launch configuration where in the first tab you fill the base directory and the goal, then, go to jre tab and add to the vm args this config:-Dmaven.multiModuleProjectDirectory=%M2_HOME%where M2_HOME is an environment variable pointing to the base directory of your maven installation

war5

Step6:

That’s it. Now get your .war file from /target directory and deploy into Apache Tomcat or any other Web Container.

war6

 

 

 

 

 

 

 

December 31, 2015

0 responses on "Maven Plugins"

Leave a Message

Your email address will not be published. Required fields are marked *

About Us

Pradeep Academy is an online & Training institute To make learning easy, interesting, affordable and accessible to millions of learners across the Globe.
To create an alternative learning platform, using a unique learning methodology of live online interactive courses along with 24x7 support. We aim to empower our customers with skills which will help them get an edge in their careers and improve their lives.

Contact Us

Address :

#Hosabelaku Building, 3417/13, Near TTMC Bus Stop,, 30 Feet Service Road,

Vijaya Nagar, Bengaluru, Karnataka 560040
Email : [email protected]
Ph No: +91 8660907231
Web: https://academy.pradeepit.com
top
© Pradeep Academy.  Design & Developed by