[JAVA] Gradle Memorandum

Steps to use Gradle quickly

Once you get a little better, read the following to get a better understanding (especially if you don't know groovy).

Introduction to build.gradle reading and writing for those who do not know Groovy

Installation

Download the binary from https://gradle.org/install/#manually, unzip it, put the path in the bin directory and finish

export GRADLE_HOME=・ ・ ・
export PATH="${GRADLE_HOME}/bin:${PATH}"

Directory structure

Create a directory as follows. It seems to follow Maven's plugin development, but I don't think deeply. Top directory becomes the name of the jar

[Top directory]
  +-- build.gradle
  +-- src/
      +-- main/
          +-- groovy/
          +-- java/
          +-- resources/
      +-- test/
          +Omitted because it is the same as under main

The roles under main are as follows

--build.gradle: Makefile for make. File equivalent to ant's build.xml --groovy: Insert the groovy file --java: Java source code --resources: Enter properties, images (icons), etc. It is automatically entered as a classpath when running with gradle. The directory files in this are also included in the main jar, so do not put anything you do not want to include here

compile

Put the appropriate code in main / src / java and put the following line in the build.gradle file

build.gradle


apply plugin: 'java'

Run gradle build

A build directory is created under the top directory, and various files are created. It's amazing. At this point I decided to break up with ant

build
|-- classes
|   `-- java
|       `-- main
|           `-- Test.class
|-- distributions
|   |-- sandbox.tar
|   `-- sandbox.zip
|-- libs
|   `-- sandbox.jar
|-- scripts
|   |-- sandbox
|   `-- sandbox.bat
`-- tmp
    |-- compileJava
    `-- jar
        `-- MANIFEST.MF

Add classpath

dependencies {
  compile fileTree(dir: 'libs', include: '*.jar')
  runtime files('src/main/dist/lib/properties')
}

--The one specified in compile will be included in the lib directory of the uniform distribution. --Do not include runtime (if you want to include it below)

Run

Just add the following two lines to build.gradle and run gradle run

apply plugin: 'application'
mainClassName = 'Class name you want to execute'

~~ If there is something to search for the classpath, such as a property file, put it in the resources directory ~~ Since it affects the distribution, set it appropriately using dependencies If you just want to move it, just plunge into resources and it's OK

To add the classpath at runtime, add the following to build.gradle (the following is an example of adding a directory). Feel free to use filetree etc. here

dependencies {
   runtime files('src/main/dist/lib/properties')
}

Handouts

The tar (zip) file under build / distributions / is a file that contains a set such as jar and startup shell. You can expand this

If only Java source code, the contents are as follows

# tar tvf build/distributions/sandbox.tar
drwxr-xr-x 0/0               0 2017-10-14 09:54 sandbox/
drwxr-xr-x 0/0               0 2017-10-14 09:54 sandbox/lib/
-rw-r--r-- 0/0             885 2017-10-14 09:54 sandbox/lib/sandbox.jar
drwxr-xr-x 0/0               0 2017-10-14 09:54 sandbox/bin/
-rwxr-xr-x 0/0            5210 2017-10-14 09:54 sandbox/bin/sandbox
-rwxr-xr-x 0/0            2180 2017-10-14 09:54 sandbox/bin/sandbox.bat

Steps for adding a property file to this distribution The conclusion is to add the property file to the distribution with the following configuration

+-- sandbox
    +-- lib/properties/~~.properties <--add to
    +-- bin/

It feels like it's under the lib directory, but this seems to be good if you don't mess with build.gradle too much.

How to do Place the property file as follows

[Top directory]
  +-- build.gradle
  +-- src/
      +-- main/
          +-- dist/
              +-- lib/
                  +-- properties/
                      + ~~.properties

Add the following to build.gradle

build.gradle


startScripts {
    classpath += files('dist/lib/properties')
}

I'm not sure, but you can also do it with files ('dist / properties'). Under dist, you have to set it to dist / lib / ~~~. If you remove the lib directory, a properties directory will be created in the directory directly under the distribution.

If you do gradle build, as I wrote at the beginning, it will be included in the lib / properties directory and also added to the CLASSPATH of the startup script.

Adding CLASSPATH to this launch script is the bottleneck. It will be $ APP_HOME / lib / ~~, so there is no choice but to use this configuration ...

I used to write symlink proposals with dist and resources, but the things in the resources directory are included in the jar file of the main body, so if you want to go out property, NG However, if the property is not tampered with during operation, it is OK to plunge into the resources directory.

Recommended Posts

Gradle Memorandum
Gradle
memorandum
memorandum
MyBatis memorandum
Java memorandum
Rails memorandum
Error memorandum
Jaba Jaba Memorandum
JavaFx memorandum
JAVA memorandum
JUnit memorandum
Docker memorandum
Docker memorandum
Gradle installation
thymeleaf memorandum
Gradle task definitions
Technical memorandum (Docker)
Log Level Memorandum
Apache FreeMarker memorandum
Screen transition memorandum
Gradle Japanese translation
Rails tutorial memorandum 1
[Ruby] Method memorandum
Rails tutorial memorandum 2
What is Gradle?
Docker command memorandum
Java study memorandum
Spring Boot Memorandum
[Java] Optional memorandum
Memorandum (Spring Web)