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
- GitHub
- IntelliJ IDEA
--2018.1 is used in this article
- Gradle
--This article uses version 4.8
- JUnit
--This article uses JUnit5
table of contents
- Create a GitHub repository
- Create a project in IntelliJ IDEA
- Gradle build file settings
- Creating java source
- Build with Gradle
- Use JUnit 5 for Gradle testing
Creating a GitHub repository
- Open the repository creation screen
- 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
- Click the Create repository button to complete
Create a project in IntelliJ IDEA
What to do here
- Create an IntelliJ IDEA project while cloning the GitHub repository
- Check if the project is tied to the Gradle feature on IntelliJ IDEA
- 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
- Open the Intellij IDEA start screen
- Click Check out from Version Control
- Select Git and clone the repository
- Copy the repository cloning URL created above to the clipboard and paste it into the URL: field.
- The repository name is automatically entered in the Directory: field, so change it if necessary.
- Click the Clone button
- A dialog saying Checkout From Version Control will appear. Click the Yes button for confirmation.
- Create a project that moves from the clone screen to project creation
- The Import Project screen opens
- Project settings
- Create project from existing sources and Import project from external model can be selected, but Import project ... is selected.
- There are choices such as Eclipse, Gradle, Maven ..., but select Gradle
- Click the Next button
- 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
- Click the Finish button to complete
- 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.
- The project screen opens
- Check if the project is tied to the Gradle feature
- 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.
- 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.
- 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
- Create build.gradle
- 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
- Create a build.gradle file directly under the project directory
- Make a minimum description in build.gradle for confirmation
- Paste the confirmation build.gradle text below
- Build: Sync works with changes to build.gradle
- Confirm that the dependency libraries are added to the path (External Libraries on the project tool screen on the left side of the window)
- 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
- Create a directory of Java sources and resources for product / testing
- In the module settings, set the role (Mark as) of the created directory.
- Make sure you can build and run the Java source for your product
- 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
- Open the module setting screen
- Right-click on the project tool screen
- Click Open Module Settings
- Select Modules
- Select an open project
- Create a directory and set Mark as on the Sources tab
- Create each directory
- src/main/java
- src/main/resources
- src/test/java
- src/test/resources
- Mark as settings
--src / main / java: Select Sources
--src / main / resources: Select Resources
--src / test / java: Select Tests
--src / test / resources: Select Test Resources
- On the Paths tab, set the output destination directory for builds on IntelliJ IDEA
- Select Use module compile output path for Compiler output
- Set the output path: to the project directory / out / production / classes
- Set the project directory / out / test / classes in Test output path:
- Create a product Java source for verification
- Create a suitable program in src / main / java
- Make sure you can build and run on IntelliJ IDEA as you write the main method
- To execute, right-click on the source and click Run'class name.method name ()'
- The result is output to the Run tool screen of IntelliJ IDEA.
- Creating a test Java source for verification
- Create a suitable program in src / test / java
- Create a test class with the name ~ Test
- Create a test method with the method with @Test (import passes because JUnit5 is set in the dependency library)
- Test run
- To run the test, right-click on the source and click Run'class name' (If right-click on the method, Run'method name ()')
- 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
- Add main class to build.gradle
- Build and test with Gradle execution function on IntelliJ IDEA
- 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
- 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'
- Build with Gradle through IntelliJ IDEA
- Open the Gradle tool screen
- Execute build of build item
- Successful build on the Run tool screen
- The result file such as class file is output to build directly under the project directory.
- Run the main class from Gradle through IntelliJ IDEA
- Open the Gradle tool screen
- Run the application item run
- On the Run tool screen, the execution result of the Java source class for the product is displayed.
- Being able to test from Gradle through JUnit 5 through IntelliJ IDEA
- Open the Gradle tool screen
- Execute test of verification item
- On the Run tool screen, the test result of the Java source for testing is displayed.
- 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.
- 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