How to Create a Java Project with Maven in Eclipse

By Arvind Rai, August 22, 2013
We will create a java project with maven in eclipse. We will start from scratch and learn step by step.

1. Install Maven in your Window Click Here
2. Install Maven in eclipse Click Here
3. In eclipse we will create a simple java project for demo. I have created a java project named as JavaProject in my eclipse. There is a class TestLogger.java . In our demo , there will be a apache logger to print the a message.

TestLogger.java
package com.concretepage;
import org.apache.log4j.Logger;
public class TestLogger {
	public static void main(String[] args) {
		Logger logger = Logger.getLogger(TestLogger.class);
		logger.info("Hello World!");
	}
}
 
4. Create pom.xml in project root and configure dependency for log4j.jar.

pom.xml
<?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>com.concretepage.app</groupId>
  <artifactId>commonapp</artifactId>
  <version> 1.0-SNAPSHOT</version>
  <packaging>jar</packaging>      
  <name>Java App with Maven</name>
  <dependencies>
	  <dependency>
		<groupId>log4j</groupId>
		<artifactId>log4j</artifactId>
		<version>1.2.17</version>
	</dependency>
  </dependencies>
 </project>
 
5. Create log4.properties in classpath.

log4j.properties
log4j.rootLogger = DEBUG, A
# Set the appender named A to be a File appender
log4j.appender.A=org.apache.log4j.ConsoleAppender
# Define the layout for A appender
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.conversionPattern=%m%n
 
6. Run pom.xml. Find link how to run pom.xml Click Here
7. Configure project .classpath for new downloaded jar by pom.xml Maven does this by a command. Go to project directory and run command
mvn eclipse:eclipse
 
8. We can use below mvn commands to manage project. clean, install, package, deploy, test etc.
9. Find the package structure in eclipse.

How to Create a Java Project with Maven in Eclipse

Download Source Code
how-to-create-java-project-with-maven-in-eclipse.zip
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us