Monday, 17 April 2017

Create a simple executable JAR with Maven in Eclipse

Create a simple executable JAR with Maven in Eclipse


In eclipse create a new project, a new Maven project, select a simple project(skip archetype selection).
Also select a workspace location from the harddrive.
Enter a group-id like, nl.blogspot.janvanoverveld.executableJar
a Artifact id like : executablejar
and a name and description. (my case both executablejar ).

Now we have a simple maven project.

Open the pom.xml and enter text below:

<project 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>nl.blogspot.janvanoverveld.executableJar</groupId>
  <artifactId>executablejar</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>executablejar</name>
  <description>executablejar</description>
  
<properties>
<slf4j.version>1.7.0</slf4j.version>
</properties>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>

<!-- TEST dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
</plugin>

<!-- below configuration for adding dependencies into jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>

<executions>
<execution>
<phase>package</phase>

<goals>
<goal>shade</goal>
</goals>

<configuration>
<!-- below configuration for making jar executable -->
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>executable.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>  
  
</project>

The class that has to be started, is the class described in the mainClass tag above. Create this package and class, below an example:

package executable;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Main {

private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

private void messageLoop(String msg){
   while(true) {
    Date nu = new Date();
    String strDate = sdf.format(nu);
       LOGGER.info(strDate + " :: " + msg);
       try 
          { Thread.sleep(10000); } 
       catch (InterruptedException ex) 
          { return; }
   }

}

    public static void main(String[] args) {
LOGGER.info("Start main" );
Main mainKlasse = new Main();
mainKlasse.messageLoop("Hier ben ik ;o)");
LOGGER.info("Einde main" );
    }

}

Create a log4j.properties file in src/main/resources, so we will see output when we start our jar.
The log4j.properties should have entries below
log4j.rootLogger = DEBUG, STDOUT, file
log4j.appender.STDOUT = org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout = org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern = %5p [%t] (%F:%L) - %m%n

Now lets try to run the executable jar. From the commandline go to the worspace directory and execute
mvn clean install.
java -jar ./target/





download
alternative link download

Like the Post? Do share with your Friends.