I made a project with Gradle because I wanted to build Java code with Editor. I used the Gradle wrapper, and the wrapper can fix the gradle version and can be used on Linux or Windows. You can build without modifying your code. For this reason, Android Project also uses wrappers.
My development environment is as follows.
- Ubuntu 17.04
- Java 1.8
- Gradle 4.5
Create Project
First you need to create a source podler.
$ mkdir -p src/main/java/hello
And I created a source code that simply prints Hello World.
package hello;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
The Dir structure is as shown below.
$ tree
.
└── src
└── main
└── java
└── hello
└── HelloWorld.java
Now we need to generate the Gradle wrapper file.
If you enter ./gradlew
in the top-level dir of the project, wrapper files are created.
- gradlew is a script used in Unix-like Linux and Mac.
- gradlew.bat is a script used in Windows series.
- gradle/wrapper/ contains jars and properties files.
$ ./gradlew
$ tree
.
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── src
└── main
└── java
└── hello
└── HelloWorld.java
After the gradle file is created, you need to define the build-related contents by creating a bulid.gradle
file in the project dir.
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'hello.HelloWorld'
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
testCompile "junit:junit:4.12"
}
Build & Run
Now everything is ready to build.
You can build with ./gradlew build
. If you look at the tree after the build is complete, a class file has been created.
$ ./gradlew build
$ tree
.
├── build
│ ├── classes
│ │ └── java
│ │ └── main
│ │ └── hello
│ │ └── HelloWorld.class
│ ├── libs
│ │ └── build_with_gradle.jar
│ └── tmp
│ ├── compileJava
│ └── jar
│ └── MANIFEST.MF
├── build.gradle
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── src
└── main
└── java
└── hello
└── HelloWorld.java
You can also run compiled Java programs with ./gradlew run
.
$ ./gradlew run
> Task :run
Hello World!
BUILD SUCCESSFUL in 0s
2 actionable tasks: 1 executed, 1 up-to-date
bulid.gradle
I tried from building to running, but in fact, if I dont define a script in build.gradle, it doesn
t work as expected.
If you are building in Java, you need to define it as follows.
apply plugin: 'java'
If you want to run a Java program compiled with Gradle, you need to define it as follows.
apply plugin: 'application'
mainClassName = 'hello.HelloWorld'
If you want to use a 3rd party dependency library, you need to define it as follows.
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
testCompile "junit:junit:4.12"
}
If you are using a local file, you can set the dependency with compile fileTree
or compile files
.
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile files('libs/library.jar').
}
Reference
Related Posts
- Java - Remove items from List while iterating
- Java - How to find key by value in HashMap
- Java - Update the value of a key in HashMap
- Java - How to put quotes in a string
- Java - How to put a comma (,) after every 3 digits
- BiConsumer example in Java 8
- Java 8 - Consumer example
- Java 8 - BinaryOperator example
- Java 8 - BiPredicate Example
- Java 8 - Predicate example
- Java 8 - Convert Stream to List
- Java 8 - BiFunction example
- Java 8 - Function example
- Java - Convert List to Map
- Exception testing in JUnit
- Hamcrest Collections Matcher
- Hamcrest equalTo () Matcher
- AAA pattern of unit test (Arrange/Act/Assert)
- Hamcrest Text Matcher
- Hamcrest Custom Matcher
- Why Junit uses Hamcrest
- Java - ForkJoinPool
- Java - How to use Futures
- Java - Simple HashTable implementation
- Java - Create a file in a specific path
- Java - Mockito의 @Mock, @Spy, @Captor, @InjectMocks
- Java - How to write test code using Mockito
- Java - Synchronized block
- Java - How to decompile a ".class" file into a Java file (jd-cli decompiler)
- Java - How to generate a random number
- Java - Calculate powers, Math.pow()
- Java - Calculate the square root, Math.sqrt()
- Java - How to compare String (==, equals, compare)
- Java - Calculate String Length
- Java - case conversion & comparison insensitive (toUpperCase, toLowerCase, equalsIgnoreCase)