How To Create A Jar File With Maven
This post is about how to create a java project template into jar package. Here are the tools i have used.
- JDK 1.6
- Apache maven 3
- Eclipse 3.7
First create a project template using maven
$ mvn archetype:generate -DgroupId=com.supun.core -DartifactId=technodyne-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Then convert it to eclipse project.
cd technodyne/
mvn eclipse:eclipse
import it to Eclipse. Will show as follows.
The pom.xml file should be like this.
[sourcecode language=”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.supun.core</groupId>
<artifactId>technodyne</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>technodyne</name>
<url>http://maven.apache.org</url>
<properties>
<jdk.version>1.6</jdk.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compile-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.supun.core.App</mainClass>
<classpathPrefix>dependency-jars</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeGroupIds>log4j</includeGroupIds>
<outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
[/sourcecode]
Then add the log4j dependencies for the project.
[sourcecode language=”java”]
package com.supun.core;
import org.apache.log4j.Logger;
/**
* Hello world!
*
*/
public class App
{
static Logger logger = Logger.getLogger(App.class);
public static void main( String[] args )
{
System.out.println( “Hello World!” );
if(logger.isDebugEnabled()){
logger.debug(“Testing”);
}
}
}
[/sourcecode]
Then create log4j.properties file inside the main/resources folder
# Root logger option
log4j.rootLogger=DEBUG, stdout# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
Now run this jar as follows.
That' s it. Happy coding ..:D