Code Monkey home page Code Monkey logo

runnablejargradle's Introduction

How to create a runnable JAR file with Gradle Java, Gradle

This post will show you how we can compile and package a simple Java Project using Gradle.

Project Layout The default project layout of a Java project with Gradle is following (just like Maven):

The src/main/java directory contains the source code. The src/main/resources directory contains the resources (such as properties files). The src/test/java directory contains the test classes. The src/test/resources directory contains the test resources.

so know make a new folder where you would like the project to start..

mkdir runnablejar cd runnablejar

Now lets create the project layout by using the following commands mkdir -p src/main/java mkdir -p src/main/resources mkdir -p src/test/java mkdir -p src/test/resources

As you can see from above the project layout looks just like a Maven project.

Creating the Main Class Lets create the main class for this project and lets have it display "Hello World"

Now cd into src/main/java

and create the package layout

mkdir -p com/johnathanmarksmith/gradle

Now we have the project layout created and also the package layout so now lets create the Main Class

vi com/johnathanmarksmith/gradle/HelloWorld.java

and insert the following lines package com.johnathanmarksmith.gradle; public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } }

Creating the Gradle Build File.. Now its time to create the Gralde Build file, Go back to the root of the project. cd ../../..

Now lets create the build file vi build.gradle

and add the following lines

apply plugin: 'java' jar { baseName = 'smith' version = '1.0' manifest { attributes 'Main-Class': 'com.johnathanmarksmith.gradle.HelloWorld' } }

Building the project and creating the JAR

To build the project and to create the JAR you need to run the following command gradle build

Executeing the JAR java -jar ./build/libs/smith-1.0.jar

runnablejargradle's People

Contributors

johnathanmarksmith avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.