Java @ Desk: Maven | Java at your desk

Deploy Maven War Project in to Tomcat automatically



Deploy Maven War Project in to Tomcat automatically



Maven allows auto deployment of a war file into tomcat. In order to do so, maven-war-plugin is utilized. The plugin helps copying the war file into tomcat webapp's folder defined in the configuration.



<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-war-plugin</artifactId>
 <configuration>
  <outputDirectory>C:\Kumar\Software\apache-tomcat-8.0.28-windows-x64\apache-tomcat-8.0.28-windows-x64\apache-tomcat-8.0.28\webapps</outputDirectory>
  <warName>prs-war</warName>
 </configuration>
</plugin>


<outputDirectory>
- Path of the tomcat web app folder.

This helps in automatic deploying the war into tomcat and no need to restart the server after each build.

Maven create WAR file from pom.xml

Maven create WAR file from pom.xml

To create a WAR file directly from pom.xml and place it to external directory. The external directory can be a server location where the war needs to be deployed.

To achieve so, maven-war-plugin is used in the pom.xml to configure a war packaging at the external location as shown below:
1) Create Maven WebApp project in Eclipse, i.e. MavenSampleProjArtifactId
2) Add maven-war-plugin to pom.xml file
3) Run command clean install to generate .war file

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-war-plugin</artifactId>
 <configuration>
  <warName>MavenSampleProjArtifactId</warName>
  <outputDirectory>F:\Jboss\server\default\deploy</outputDirectory>
 </configuration>
</plugin>


On command run mvn install, the war file will be created at the location mentioned in tag

Console Output To check War File creation at location mentioned above

[INFO] --- maven-install-plugin:2.4:install (default-install) @ MavenSampleProjArtifactId ---
[INFO] Installing F:\Jboss\server\default\deploy\MavenSampleProjArtifactId.war

How to Create Maven Project using Archtype

How to Create Maven Project using Archtype

If you are going to start working on maven projects, there are two steps to get started with your first Maven Project-

Step 1: Install Maven in your system. you can get suitable version for your system from download maven here.

Extract downloaded archive to desired location in your system preferably under "C:\Program Files"

Once it is extracted or if it is already there in your system, please make sure you have set environment variable for M2_HOME.


Add environment variable M2_HOME


















Update the PATH variable to include Maven bin as shown below-


















Step 2: Now go to directory where you wish to create your maven project and run command

mvnarchetype:generate -DarchetypeRepository=http://repo.adobe.com/nexus/content/groups/public/
-DarchetypeGroupId=com.day.jcr.vault
-DarchetypeArtifactId=multimodule-content-package-archetype
-DarchetypeVersion=1.0.2 -DgroupId=com.example
-DartifactId=sampleproject
-Dversion=1.0-SNAPSHOT
-Dpackage=com.example
-DappsFolderName=apps
-DartifactName="Sample Maven Project"
-DcqVersion="5.6.1"
-DpackageGroup="sampleproject"


In this command
ArtifactId is going to be the name of your project.
Version specifies your build versions.
artifactName is the name will be displayed when you build it.
Package is the default package going to be created in your bundle where you can put all your java code













It’s going to take a while it downloads all the dependencies and put it in your system. And finally there it is your first maven project created "sampleproject".
You can see all the downloaded dependencies in .m2 folder created in C drive or running .m2 in run command box.











You can import that maven project in your IDE and start working on it.

This post is written by
Saurabh Thareja - Linkedin, Facebook, Google Plus
He is a freelance writer, loves to explore latest features in Java technology.


Eclipse Maven Change Repository Location

Eclipse Maven Change Repository Location

By default, the maven repository location is "C:\Documents and Settings\USER_NAME\.m2". In order to change the default location for Maven repository, location needs to be updated with the desired location.

To change the default location follow steps :

1) Navigate to folder "C:\Documents and Settings\USER_NAME\.m2"
2) If settings.xml is not available, create the file
3) Add below code in the xml


<settings><localRepository>F:/Projects/Installers/maven</localRepository></settings>


4) Once done, go to eclipse and do a "mvn install"

The above command will download all the dependencies at the above location.

To verify, change the pom.xml and verify as shown in below image