Gradle Create a Jar file with dependencies

摘要: In this tutorial, we will show you how to use Gradle build tool to create a single Jar file with dependencies.

In this tutorial, we will show you how to use Gradle build tool to create a single Jar file with dependencies.

Tools used :

  1. Gradle 2.0
  2. JDK 1.7
  3. Logback 1.1.2

1. Project Directory

Create following project folder structure :

By default, Gradle is using the standard Maven project structure.

  1. ${Project}/src/main/java/
  2. ${Project}/src/main/resources/
  3. ${Project}/src/test/java/

2. Java Files

A single Java file to print out the current date time, and logs the message with logback.

DateUtils.java
package com.mkyong;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DateUtils{
	private static final Logger logger = LoggerFactory.getLogger(DateUtils.class);
	public static void main(String[] args) {
		logger.debug("[MAIN] Current Date : {}", getCurrentDate());
		System.out.println(getCurrentDate());
	private static Date getCurrentDate(){
		return new Date();
logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
	  <layout class="ch.qos.logback.classic.PatternLayout">
		<Pattern>
			%-5level %logger{36} - %msg%n
		</Pattern>
	  </layout>
	</appender>
	<root level="debug">
	  <appender-ref ref="STDOUT" />
	</root>
</configuration>

3. build.gradle

A build.gradle sample to create a Jar file along with its logback dependencies.

build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
version = '1.0'
sourceCompatibility = 1.7
targetCompatibility = 1.7
//create a single Jar with all dependencies
task fatJar(type: Jar) {
	manifest {
        attributes 'Implementation-Title': 'Gradle Jar File Example',  
        	'Implementation-Version': version,
        	'Main-Class': 'com.mkyong.DateUtils'
    baseName = project.name + '-all'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
//Get dependencies from Maven central repository
repositories {
    mavenCentral()
//Project dependencies
dependencies {
	compile 'ch.qos.logback:logback-classic:1.1.2'

4. Create a Jar File

Clean the project.

$ gradle clean 

Run the Gradle fatJar task.

$ gradle fatJar 
:compileJava
:processResources
:classes
:fatJar
BUILD SUCCESSFUL
Total time: 6.4 secs

The Jar is created under the $project/build/libs/ folder.

5. Run It

Run it – java -jar hello-all-1.0.jar.

$Project\build\libs> java -jar hello-all-1.0.jar
16:22:13,249 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
16:22:13,249 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
//...
DEBUG com.mkyong.DateUtils - [MAIN] Current Date : Wed Aug 27 16:22:13 SGT 2014
Wed Aug 27 16:22:13 SGT 2014

Done.

References

  1. Gradle : Jar Task
  2. Wikipedia : Gradle

上一篇: How to configure hot deploy in Eclipse
下一篇: Gradle bootstrap class path not set in conjunction with -source 1.5
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号