Gradle to touch and remember

What makes Gradle happy?

I thought this was the same when I recommended Docker to people, but to be honest, it's difficult to convey the goodness. Whether it's gradle or Docker, each person has their own appeal, and even if you try to convey the convenience, it doesn't really come to your mind. Those who are listening stay in the position of "I see, it seems convenient." In the first place, people who say, "Seriously! Let's use it now!" Do not need to explain because they try to touch it by themselves and try to grasp what it is. I just want to be able to "use it for the time being". However, there are many cases where the skill to do it has not been reached. For the time being ... what should I do?

Therefore, "training" is attached to the tag of this article. The best way to be able to turn some of the useful tools in the world into a force is to build a track record of turning some of them into a force.

This article will help you treat gradle as your strength. By the time I knew what I was happy with, I already had a weapon.

gradle installation

In this article, the OS of the development machine is assumed to be Windows. The IDE is Eclipse. Therefore, there are some procedures and descriptions that are specific to it. If you haven't installed it yet, you should definitely try using a package manager. There is Homebrew etc. on Mac, but since Windows is assumed, the procedure with scoop is described. First, let's launch Powershell with administrator privileges.

Allow script execution

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force

install scoop

iex (new-object net.webclient).downloadstring('https://get.scoop.sh')

install gradle

scoop install gradle

If you go through a proxy, there are a few more steps (how to dive in a proxy with PowerShell is my manuscript (almost) CUI procedure from installation to use of Docker for Window It's a convenient time because you can install it by copying and pasting 3 lines of the command. There is no loss in including a package manager called scoop. If you take this opportunity, you may be happy later.

Create a project folder

Let's use gradle immediately. Anyway, create a folder for the project. Create a folder with the project name. This time it was called "Yebisu". Of course, this is the beer I'm drinking now. Once created, use Explorer to enter the Yebisu folder and press ** Shift + Right Click ** to bring up the menu. There should be a menu called ** Open PowerShell window here **. Select it and launch PowerShell.

Create gradle project

gradle init

When you do this, there are some surveys. This time, let's input as follows because it is java-appication.

PS D:\git-private\Yebisu> gradle init
Starting a Gradle Daemon, 1 busy Daemon could not be reused, use --status for details

Select type of project to generate:
  1: basic
  2: application
  3: library
  4: Gradle plugin
Enter selection (default: basic) [1..4] 2

Select implementation language:
  1: C++
  2: Groovy
  3: Java
  4: Kotlin
  5: Swift
Enter selection (default: Java) [1..5] 3

Select build script DSL:
  1: Groovy
  2: Kotlin
Enter selection (default: Groovy) [1..2] 1

Select test framework:
  1: JUnit 4
  2: TestNG
  3: Spock
  4: JUnit Jupiter
Enter selection (default: JUnit 4) [1..4] 4

Project name (default: Yebisu):

Source package (default: Yebisu): yebisu


> Task :init
Get more help with your project: https://docs.gradle.org/6.1/userguide/tutorial_java_projects.html

BUILD SUCCESSFUL in 1m 28s
2 actionable tasks: 2 executed

You have now created a project. Please leave this PowerShell window unclosed.

Make it an Eclipse project

The IDE this time is Eclipse. Use the Eclipse plugin to make it an Eclipse project. This area is a routine procedure. There should already be build.gradle in the folder, so let's open it with a text editor. Add eclipse to plugins. Add ** id'eclipse' ** to the next line of ** id'application' **. By the way, I don't like clear comments, so I've deleted the comments.

build.gradle


plugins {
    id 'java'
    id 'application'
    id 'eclipse'
}

//Specify encoding as needed
compileJava.options.encoding = 'UTF-8'
compileTestJava.options.encoding = 'UTF-8'

repositories {
    jcenter()
}

dependencies {
    implementation 'com.google.guava:guava:28.1-jre'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.2'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.5.2'
}

application {
    mainClassName = 'yebisu.App'
}

test {
    useJUnitPlatform()
}

After editing, run the following command in PowerShell.

./gradlew eclipse

This will create a **. Project ** **. Classpath ** file, etc., which can be imported into Eclipse.

Import into eclipse

Use ** [File] **-> ** Import ** on the menu bar to bring up the Import Wizard. image.png

Select ** Existing Projects into Workspace ** and go to ** Next **.

image.png

Enter the path of the project you just created in ** Select root directory **. Make sure it is checked in ** Projects ** and press the ** Finish ** button.

There is a source file called App.java, and main should be there.

image.png

--src / main / java Main body source file --src / main / resources Main resource file --src / test / java Unit test source file --src / test / resources Unit test resource files

Add library

I want to use FilenameUtils of ** Apache Commons IO **! What would you do in such a case? Maybe I downloaded the Commons IO jar and added it to the project dependencies. You don't need that with gradle. All you have to do is find a line to use it. Let's google the search word as ** FilenameUtils maven **. You should be able to find a site called ** MVNrepository **.

image.png

If you select the ** Gradle ** tab, the spell will appear in the text box below it. Let's copy this spell. Paste this into the dependencies of build.gradle. Actually, compile should be implemented, but this time let's paste it as it is.

build.gradle


dependencies {
    implementation 'com.google.guava:guava:28.1-jre'

	// https://mvnrepository.com/artifact/org.apache.commons/commons-io
	compile group: 'org.apache.commons', name: 'commons-io', version: '1.3.2'

    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.2'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.5.2'
}

After saving build.gradle, go back to PowerShell and use the following command.

./gradlew eclipse

Filename Utils is now ready for use

Let's go back to Eclipse and update the project with F5. You can also right-click on the project and refresh. If you open the ** Referenced Libraries ** tree, you will find commmons-io-1.3.2.jar. Let's try it out.

If you write the following line in main of App.java ...

System.out.println(FilenameUtils.getExtension("c:/foo/bar/hoge.fuga"));

I think ** Ctrl + Shift +'O'** will automatically insert ** import org.apache.commons.io.FilenameUtils; **. Let's run it right away.

image.png

In the image, it was executed from Eclipse, but it can also be executed from gradle.

./gradlew run
> Task :run
fuga
Hello world.

BUILD SUCCESSFUL in 2s
2 actionable tasks: 1 executed, 1 up-to-date

Rewrite correctly

compile is actually an old way of writing. The description of group, name and version can be made simpler.

build.gralde


compile group: 'org.apache.commons', name: 'commons-io', version: '1.3.2'
 ↓
implementation 'org.apache.commons:commons-io:1.3.2'

Also, multiple implementations can be written together.

    implementation('com.google.guava:guava:28.1-jre'
                  ,'org.apache.commons:commons-io:1.3.2')

Add Lombok

Most libraries can be imported using the above method, but annotation processors have a different method. I unconditionally add the following to dependencies because I struggle in the cage of my own personality that I built without knowing Lombok.

build.gradle


	compileOnly 'org.projectlombok:lombok:1.18.4'
	annotationProcessor "org.projectlombok:lombok:1.18.4"
	testCompileOnly 'org.projectlombok:lombok:1.18.4'
	testAnnotationProcessor "org.projectlombok:lombok:1.18.4"

If you haven't installed Lombok, you'll need to install it. The explanation is left to the site that explains how to use Lombok, but in simple terms, you can start Lombok.jar by double-clicking and install it. Since it is described in eclipse.ini, if you install it once, you can use it in the project by emptying the above 4 lines and pasting it with Beton.

Make a jar

Even so far, I think you can feel the wonderful feeling that libraries in the world can be easily imported and used, and that all the dependencies are imported. I want it to be transmitted. I hope it is transmitted.

When I started using springBoot, I started to worship gradle more, but this article is the last one to make Jar.

Actually, Jar can still be made at this point.

./gradlew jar

That's it. I think there is a jar under the ./build/libs folder. This is just a jar so it doesn't work with -jar and nothing is written in the manifest.

And this time, let's put all the dependencies in one jar file and make it an executable jar that can work by itself. (I don't usually do it) Add the following description to build.gradle.

build.gradle


jar {
    manifest {
        attributes('Implementation-Title': 'Yebisu'
                 , 'Implementation-Version': 1.0 
                 , 'Main-Class': 'yebisu.App')
    }
    from configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}

If you generate a jar with `` `./gradlew jar``` and execute the following command, it will work properly.

java -jar .\build\libs\Yebisu.jar

The version and title are properly included in MANIFEST.MF.

MANIFEST.MF


Manifest-Version: 1.0
Implementation-Title: Yebisu
Implementation-Version: 1.0
Main-Class: yebisu.App

Finally

Will this next step be to write unit tests or get coverage with Jacoco? build.gradle is written in the language Groovy. (Can also be written in Kotlin)

That doesn't mean that reading and writing Groovy or Kotlin is mandatory. Of course it's better to have it. But well, if you can do what you want with gradle, use google to ask the world and the general answer is there. That's it. ** Because gralde is not a new technology. ** **

There are still many useful things in the world. I think many of them are disliked by learning costs. However, most of the libraries and tools that are widely used in the world are worth the learning cost.

It is a bad habit to think that there is no problem because it can be done with the current method. By all means, I want you to actively search for weapons, pick them up, and make them your own. The difference in productivity cultivated in this way will be enormous in five to ten years.         May.

Recommended Posts

Gradle to touch and remember
Add spring boot and gradle to eclipse
How to use Gradle
How to install Gradle and Kotlin with SDKMAN (Mac)
Contributed to Gradle and was named in the release notes
Unable to get resources when using modules in Gradle and IntelliJ
Collection of programming selection tasks to make and remember (Java basics)
[Convenient to remember !!!] How to convert from LocalDate type to character string and from character string to LocalDate type
Things to remember and concepts in the Ruby on Rails tutorial
Popular trends in gradle and maven
11 Corresponds to comparison and logical operators
How to use EventBus3 and ThreadMode
Compared to Sum and Summing Int
How to run Ant in Gradle
How to call classes and methods
How to use equality and equality (how to use equals)
How to connect Heroku and Sequel
How to convert LocalDate and Timestamp
Challenge to install WSL2 and docker
Try "Introduction to Vert.x" on Gradle
Hello world in Java and Gradle
[Useful to remember !!!] Easy creation of constructor and getter / setter in Eclipse
How to create a server executable JAR and WAR with Spring gradle
I introduced OpenAPI (Swagger) to Spring Boot (gradle) and tried various settings