Creating a project (and GitHub repository) using Java and Gradle in IntelliJ IDEA

Overview

This is the procedure from creating a repository on GitHub to developing on IntelliJ IDEA in a Java application project using Gradle.

What to use

table of contents

  1. Create a GitHub repository
  2. Create a project in IntelliJ IDEA
  3. Gradle build file settings
  4. Creating java source
  5. Build with Gradle
  6. Use JUnit 5 for Gradle testing

Creating a GitHub repository

  1. Open the repository creation screen
  2. Enter the repository information
    • Repository name --Enter your favorite name
    • Description(optional) --The explanation set here is automatically described in README.md.
    • public/private --Check who needs it
    • initialize this repository with a README --It is recommended to check, basically check
    • Add .gitignore --Select Java, but it may or may not be present as it will be overwritten with the .gitignore content described below. --If you create it, you don't need to create a new file.
    • Add a license --Select if you want to license
  3. Click the Create repository button to complete

Create a project in IntelliJ IDEA

What to do here

  1. Create an IntelliJ IDEA project while cloning the GitHub repository
  2. Check if the project is tied to the Gradle feature on IntelliJ IDEA
  3. Create .gitignore

What happens if you do

--Gradle build settings will be reflected in the project at any time, and IntelliJ IDEA will automatically import dependency libraries etc. --Directoryes and files created by the OS, IDE, and build tools that are not managed by the repository are no longer included in the repository changes.

procedure

  1. Open the Intellij IDEA start screen
  2. Click Check out from Version Control
  3. Select Git and clone the repository
  4. Copy the repository cloning URL created above to the clipboard and paste it into the URL: field.
  5. The repository name is automatically entered in the Directory: field, so change it if necessary.
  6. Click the Clone button
  7. A dialog saying Checkout From Version Control will appear. Click the Yes button for confirmation.
  8. Create a project that moves from the clone screen to project creation
  9. The Import Project screen opens
  10. Project settings
  11. Create project from existing sources and Import project from external model can be selected, but Import project ... is selected.
  12. There are choices such as Eclipse, Gradle, Maven ..., but select Gradle
  13. Click the Next button
  14. Gradle settings - Use auto-import --If you check it, you can import it automatically, but it may become heavier as the project grows. --It is recommended to check - Create directories for empty content root automatically --If checked, the necessary directories such as the source folder will be created when the project is created. --It is recommended to check - Group modules --Setting how to handle modules on IntelliJ IDEA in consideration of the module system that appeared in Java9 --using explicit module groups: Explicitly specify groups --using qualified names: Specify a group from qualified names —— Either way, depending on the content of the project - Create separate module per source set --If checked by default, you can create one module for each subproject. --If you have a Gradle subproject, we recommend unchecking it ――I try to uncheck it --Whereabouts of Gradle - Use default gradle wrapper (not configured for the current project) - Use gradle "wrapper" task configuration - Use local gradle distribution --Personally, it's easy to understand to specify Gradle home directly in the third Use local ... - Gradle home --Required if Use local gradle distribution is selected --Set the absolute path to libexec under the Gradle directory, like / ~ / gradle / 4.8 / libexec - Gradle JVM --Set the JVM version when running Gradle --No confusion when combined with the JVM used in the project - Project format --Select .idea (directory based) unless you are particular about it
  15. Click the Finish button to complete
  16. Do you want to add the following file to Git? Dialog appears --The .idea directory and the files under it will be created, so ask if you want to add it to the Git repository. --The .idea directory is set to be ignored by .gitignore, so select No.
  17. The project screen opens
  18. Check if the project is tied to the Gradle feature
  19. Confirm the project motivation and build success on the Build tool screen at the bottom of the window --Project name: synced successfully and displayed on the first line --Run build is successful --A green icon will be attached to each line if it is successful, and a red icon will be attached to each line if it is unsuccessful.
  20. The Gradle menu of the project is displayed on the Gradle tool screen on the right side of the window. --It is displayed as project name (auto-import enabled) etc.
  21. Create .gitignore --Create .gitignore directly under the project directory with the contents described below --Exclude unnecessary directories and files from commit

.gitignore content

I refer to RxJava's .gitignore, and personally it includes support for each IDE and build tool, so I think it's better to set it from the beginning.

#Build result file alone
*.com
*.class
*.dll
*.exe
*.o
*.so

#Build result package file
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip

#logfile
*.log

#Generated by OS
.DS_Store*
ehthumbs.db
Icon?
Thumbs.db

#File being edited
*~
*.swp

#Generated by Gradle
.gradle
.gradletasknamecache
.m2
!gradle-wrapper.jar

#Generated by build tool
target/
build/

#Generated by IntelliJ IDEA
out
.idea
*.ipr
*.iws
*.iml
atlassian-ide-plugin.xml

#Generated by Eclipse
.classpath
.project
.settings
.metadata
bin/

#Generated by NetBeans
.nbattrs
/.nb-gradle/profiles/private/
.nb-gradle-properties

#Scala relationship
*.cache
/.nb-gradle/private/

#Generated by PMD
.pmd
.ruleset

Gradle build file settings

What to do here

  1. Create build.gradle
  2. Confirmation description in build.gradle

What happens if you do

--If you add the contents to build.gradle, the change will be detected on IntelliJ IDEA and the dependency library and build will be executed automatically.

procedure

  1. Create a build.gradle file directly under the project directory
  2. Make a minimum description in build.gradle for confirmation
  3. Paste the confirmation build.gradle text below
  4. Build: Sync works with changes to build.gradle
  5. Confirm that the dependency libraries are added to the path (External Libraries on the project tool screen on the left side of the window)
  6. OK if Junit5 is added

Confirm build.gradle

apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.2.0'
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.2.0'
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.2.0'
}

test {
    useJUnitPlatform()
}

Java source creation

What to do here

  1. Create a directory of Java sources and resources for product / testing
  2. In the module settings, set the role (Mark as) of the created directory.
  3. Make sure you can build and run the Java source for your product
  4. Make sure you can build and run the test Java source

What happens if you do

--Java source can be created, built and executed on IntelliJ IDEA --Tests can be run on IntelliJ IDEA

procedure

  1. Open the module setting screen
  2. Right-click on the project tool screen
  3. Click Open Module Settings
  4. Select Modules
  5. Select an open project
  6. Create a directory and set Mark as on the Sources tab
  7. Create each directory - src/main/java - src/main/resources - src/test/java - src/test/resources
  8. Mark as settings --src / main / java: Select Sources --src / main / resources: Select Resources --src / test / java: Select Tests --src / test / resources: Select Test Resources
  9. On the Paths tab, set the output destination directory for builds on IntelliJ IDEA
  10. Select Use module compile output path for Compiler output
  11. Set the output path: to the project directory / out / production / classes
  12. Set the project directory / out / test / classes in Test output path:
  13. Create a product Java source for verification
  14. Create a suitable program in src / main / java
  15. Make sure you can build and run on IntelliJ IDEA as you write the main method
  16. To execute, right-click on the source and click Run'class name.method name ()'
  17. The result is output to the Run tool screen of IntelliJ IDEA.
  18. Creating a test Java source for verification
  19. Create a suitable program in src / test / java
  20. Create a test class with the name ~ Test
  21. Create a test method with the method with @Test (import passes because JUnit5 is set in the dependency library)
  22. Test run
  23. To run the test, right-click on the source and click Run'class name' (If right-click on the method, Run'method name ()')
  24. Test results are output to the Run tool screen of IntelliJ IDEA --The test class and test method are in the result

Java source for confirmation

product

Main.java


public class Main {

	public static void main(String[] args) {
		System.out.println("test product");
	}

}

test

MainTest.java


import org.junit.jupiter.api.Test;

public class MainTest {

	@Test
	public void test(){
		System.out.println("test test");
	}

}

Build with Gradle

What to do here

  1. Add main class to build.gradle
  2. Build and test with Gradle execution function on IntelliJ IDEA
  3. Confirm that the test result is output in HTML

What happens if you do

--You can see that running Gradle through IntelliJ IDEA can perform various functions such as building, running, and testing the source on the project. --You can see that you can run tests using JUnit 5 in Gradle and output the results in HTML --As a result, it turns out that there is no problem with the cooperation between IntelliJ IDEA and Gradle, and the project and Gradle settings are completed.

procedure

  1. Added to build.gradle
    //Specify the application plug-in to specify the executable class and the class that has the executable main method.
    apply plugin: 'application'
    //Specify the execution target class
    mainClassName = '<Java source class for product>'
    //If you follow the example, mainClassName= 'Main'
    
  2. Build with Gradle through IntelliJ IDEA
  3. Open the Gradle tool screen
  4. Execute build of build item
  5. Successful build on the Run tool screen
  6. The result file such as class file is output to build directly under the project directory.
  7. Run the main class from Gradle through IntelliJ IDEA
  8. Open the Gradle tool screen
  9. Run the application item run
  10. On the Run tool screen, the execution result of the Java source class for the product is displayed.
  11. Being able to test from Gradle through JUnit 5 through IntelliJ IDEA
  12. Open the Gradle tool screen
  13. Execute test of verification item
  14. On the Run tool screen, the test result of the Java source for testing is displayed.
  15. You can check the test result in the browser by clicking the icon with the arrow on the Gradle mark on the Run tool screen.
  16. The test result displayed on the browser shows the number of test digestions described in the test class.

What's going on in the end

Project directory structure

.gradle
.idea
build
out
	production
		classes
			Main.class
	test
		classes
			MainTest.class
src
	main
		java
	        Main
		resources
	test
		java
			MainTest
		resources
.gitignore
build.gradle
README.md

.gitignore

Same as above.

build.gradle

//Specify java plugin to build main and test respectively
apply plugin: 'java'

//Specify the repository to use
repositories {
    //Use Maven repository
    mavenCentral()
}

dependencies {
    // JUnit
    // https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.2.0'
    // https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.2.0'
    // https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.2.0'
}

//Test run-time settings in Gradle
test {
    //Settings for running Junit5 with Gradle, Gradle4.Can be described because Gradle natively supports Junit 5 from 6
    useJUnitPlatform()
}

//Specify the application plug-in to specify the executable class and the class that has the executable main method.
apply plugin: 'application'
//Specify the execution target class
mainClassName = 'Main'

Other

--When you run the wrapper in Gradle's build setup, the following is created --Gradle directory and files related to gradle wrapper are created under it --gradlew is created --gradlew.bat is created

Recommended Posts

Creating a project (and GitHub repository) using Java and Gradle in IntelliJ IDEA
How to create a new Gradle + Java + Jar project in Intellij 2016.03
Java tips-Create a Spring Boot project in Gradle
How to convert A to a and a to A using AND and OR in Java
Until you create a Spring Boot project in Intellij and push it to Github
Build a Java project with Gradle
Do HelloWorld in Java / IntelliJ / Gradle
Create a Java project using Eclipse
[Java] A technique for writing constructors, getters, and setters in one shot with IntelliJ IDEA.
Java + OpenCV 3.X in IntelliJ IDEA
Hello world in Java and Gradle
Unable to get resources when using modules in Gradle and IntelliJ
Exporting project and war files when creating server-side Java in Eclipse
[Creating] A memorandum about coding in Java
Optimize Java import declarations in IntelliJ IDEA
Create Java Spring Boot project in IntelliJ
Creating a matrix class in Java Part 1
Create a Spring Boot project in intellij and exit immediately after launching
Java --Introduce CheckStyle plugin to IntelliJ IDEA and reflect it in formatter
Add .gitignore when creating a project in Xcode
Create a Spring Boot application using IntelliJ IDEA
Run JUnit and Spock in a maven project
What to do if Command line is too long appears when building a gradle project in Intellij IDEA
Resolve "Cannot resolve symbol" in IntelliJ projects using Gradle
Add a project in any folder with Gradle
When using a list in Java, java.awt.List comes out and an error occurs
Create a Java (Gradle) project with VS Code and develop it on a Docker container
Create a Java Servlet and JSP WAR file to deploy to Apache Tomcat 9 in Gradle
Create a portfolio app using Java and Spring Boot
Convert JSON and YAML in Java (using Jackson and SnakeYAML)
How to create a Spring Boot project in IntelliJ
Write a class in Kotlin and call it in Java
Use Coveralls with GitHub Actions in a Ruby repository
Creating a local repository
When a breakpoint is set in IntelliJ IDEA but it does not stop in debugging [Gradle]
Implement Thread in Java and try using anonymous class, lambda
I tried using the GitHub repository as a library server
[Java] A story about IntelliJ IDEA teaching Map's putIfAbsent method
You are currently using Java 6. Solution in Android Studio Gradle
Create a tomcat project using Eclipse Pleiades All in One
How to develop and register a Sota app in Java
Dependency management in Gradle using Maven repository on Amazon S3
Tips for using Salesforce SOAP and Bulk API in Java
Socket communication with a web browser using Java and JavaScript ②
Socket communication with a web browser using Java and JavaScript ①
Create a Java and JavaScript team development environment (gradle environment construction)
A Simple CRUD Sample Using Java Servlet / JSP and MySQL
Easily convert Java application to Docker container with Jib ~ Build with gradle and register in local repository
Let's make a LAN communication application Part 1 New project creation using Maven and Java entry point
Try using RocksDB in Java
Write Processing in IntelliJ IDEA
Creating a calendar using Ruby
Find a subset in Java
Try using IntelliJ IDEA once
Using Docker from Java Gradle
Make a rhombus using Java
Create a private repository in Amazon ECR and push/pull the image
In Java 10, when you do gradle eclipse and JavaSE-1.10 comes out, ...
I can't create a Java class with a specific name in IntelliJ
NLP4J [001a] Morphological analysis in Java (using Yahoo! Developer Network Japanese morphological analysis)
Automatically deploy a Web application developed in Java using Jenkins [Preparation]