[Java] Create an executable module with Gradle

Purpose

After creating a simple module without UI in Java, place it in the execution environment.

Prerequisites

・ Eclipse ・ Gradle

build.gradle settings

ʻAdd the application` plugin and the definition of the Main classpath.

build.gradle


apply plugin: 'application'

mainClassName = 'jp.vicugna_pacos.admainte.main.AdMainteMain'

compile

  1. Run Gradle's task ʻinstallDist`.
  2. An executable module will be created in the project folder / build / install. Copy it and move it to the specified location.
  3. The bat file in the bin folder is the startup script.

If there is no JAVA_HOME

Looking at the bat file created above, it is assumed that the environment variable JAVA_HOME is defined. If there is no JAVA_HOME, create a wrapper bat file as follows.

Wrapper bat sample


@echo off
setlocal

set ARGS=%*
set DIRNAME=%~dp0
set BATNAME=%DIRNAME%AD_MAINTE\bin\AD_MAINTE.bat
set JAVA_HOME=E:\jre\8

%BATNAME% %ARGS%

endlocal

Go out property files etc.

First, put the files you want to go out in the src / dist folder.

As it is, the property file in src / main / resources has priority, so do not include it in the Jar file.

build.gradle


jar {
    exclude '**/config.properties'
    exclude '**/logback.xml'
}

In addition, the files placed in the dist folder should be included in the classpath at runtime.

build.gradle


startScripts {
  classpath += files('src/dist/XxxAPlaceHolderForAConfigxxX')
  doLast {
    def windowsScriptFile = file getWindowsScript()
    def unixScriptFile    = file getUnixScript()
    windowsScriptFile.text = windowsScriptFile.text.replace('%APP_HOME%\\lib\\XxxAPlaceHolderForAConfigxxX', '%APP_HOME%\\config')
    unixScriptFile.text  = unixScriptFile.text.replace('$APP_HOME/lib/XxxAPlaceHolderForAConfigxxX', '$APP_HOME/config')
  }
}

In this sample, the files placed in the dist / config folder will be included in the classpath.

reference: java - Adding classpath entries using Gradle's Application plugin - Stack Overflow https://stackoverflow.com/questions/10518603/adding-classpath-entries-using-gradles-application-plugin

Recommended Posts

[Java] Create an executable module with Gradle
Create an immutable class with JAVA
Java multi-project creation with Gradle
Create a simple DRUD application with Java + SpringBoot + Gradle + thymeleaf (1)
Build a Java project with Gradle
Create an app with Spring Boot 2
Create an excel file with poi
Create an app with Spring Boot
Run an application made with Java8 with Java6
[Java] Module
Let's create an instance with .new yourself. .. ..
Create an infinite scroll with Infinite Scroll and kaminari
CICS-Run Java applications-(3) Build management with Gradle
Java automated test implementation with JUnit 5 + Gradle
Create an or search function with Ransack.
Create Java applications with IBM Cloud Functions
How to create a server executable JAR and WAR with Spring gradle
Make SpringBoot1.5 + Gradle4.4 + Java8 + Docker environment compatible with Java11
Create a CSR with extended information in Java
Create a simple bulletin board with Java + MySQL
[Windows] [IntelliJ] [Java] [Tomcat] Create a Tomcat9 environment with IntelliJ
Let's create a timed process with Java Timer! !!
Create an RSA encryption-enabled JSON API with wicket
Static code analysis with Checkstyle in Java + Gradle
Getting started with Gradle (until you create a Java project and combine external libraries into one executable JAR)
Build and test Java + Gradle applications with Wercker
[Java] Create a collection with only one element
Build an E2E test environment with Selenium (Java)
Using Gradle with VS Code, build Java → run
Create an ARM-cpu environment with qemu on mac and run java [Result → Failure]
[Java] Find prime numbers with an Eratosthenes sieve
Create an EC site with Rails5 ⑤ ~ Customer model ~
Create an Annotator that uses kuromoji with NLP4J [007]
[Swift] Create an image selection UI with PhotoKit
Create an EC site with Rails 5 ⑩ ~ Create an order function ~
Create a Java (Gradle) project with VS Code and develop it on a Docker container
[Java] Find prime numbers with an Eratosthenes sieve (Part 2)
Create an HTTPS file server for development with ring-jetty-adapter
Create an EC site with Rails5 ⑦ ~ Address, Genre model ~
Create an EC site with Rails 5 ⑨ ~ Create a cart function ~
[Beginner] Create a competitive game with basic Java knowledge
Create an EC site with Rails5 ④ ~ Header and footer ~
Create an E2E test environment with Docker x Cypress
Create a user with an empty password on CentOS7
Create an EC site with Rails5 ⑥ ~ seed data input ~
Quickly implement a singleton with an enum in Java
[Note] Create a java environment from scratch with docker
Create a Service with an empty model Liferay 7.0 / DXP
Check coverage with Codecov in Java + Gradle + Wercker configuration
Try building Java into a native module with GraalVM
Install java with Homebrew
Try create with Trailblazer
Use ProGuard with Gradle
Integration Test with Gradle
Change seats with java
Install Java with Ansible
[Java] Create a filter
[java] throw an exception
Comfortable download with JAVA
First gradle build (Java)
Install Gradle with ubuntu16.04