Untitled
unknown
plain_text
a year ago
19 kB
6
Indexable
1. fundamentals 1.1. devtools 1.1.1. buildtools 1.1.1.1. gradle 1.1.1.1.1. BasicProject Build (bản xây dựng) -- contains 1+ --> Project -- contains 1+ --> Task 2. Initializing a basic project managed by Gradle 2.1. Create a new directory to store files of your project and go to it. mkdir gradle-demo cd gradle-demo 2.2. Invoke the gradle init command to generate a simple project. Modern versions of Gradle will ask you to fill several parameters in a dialogue form. - This command will produce the following output: > Task :init BUILD SUCCESSFUL in 10s 2 actionable tasks: 2 executed - Gradle performed some tasks for you and now there is a simple project with the most basic structure: . ├── build.gradle ├── gradle │ └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle The build.gradle file is a primary file that specifies the Gradle's project, including its tasks and external libraries. The files gradle-wrapper.jar, gradle-wrapper.properties, gradlew and gradlew.bat belong to Gradle Wrapper which allows you to run Gradle without its manual installation. The settings.gradle file specifies which projects to include in your build. This file is optional for a build that has only one project, but it is mandatory for a multi-project build. - Let's build our project invoking the gradle build command from the same location where build.gradle resides. It will produce an output like this: > Task :buildEnvironment ------------------------------------------------------------ Root project ------------------------------------------------------------ ... BUILD SUCCESSFUL in 725ms 1 actionable task: 1 executed 3. Modifying the build file Adding some properties and one task to the build.gradle file using Groovy DSL: description = "A basic Gradle project" task helloGradle { doLast { println 'Hello, Gradle!' } } Here, we set the description property and define a simple task that prints a 'hello' message. There is an output after executing the task with the gradle -q helloGradle command: > Task :buildEnvironment ------------------------------------------------------------ Root project - A basic Gradle project ------------------------------------------------------------ ... > Task :helloGradle Hello, Gradle! BUILD SUCCESSFUL in 831ms 2 actionable tasks: 2 executed This build was completed with 2 tasks executed. Our new task printed the Hello, Gradle! message. In addition, we modified the description of the project in the build. The -q argument just simplifies the command output. 4. The list of all the tasks If you would like to see all the possible Gradle tasks to perform, just run the gradle tasks --all command. The list will include our tasks as well: > Task :tasks ------------------------------------------------------------ Tasks runnable from root project - A basic Gradle project ------------------------------------------------------------ Build Setup tasks ----------------- init - Initializes a new Gradle build. wrapper - Generates Gradle wrapper files. Help tasks ---------- buildEnvironment - Displays all buildscript dependencies declared in root project 'gradle-demo'. ... Other tasks ----------- helloGradle ############################# 1.1.1.1.2. BuildingApp ############################ 1.1.1.1.3. DependencyManagement 1. How do you add dependencies? In Gradle terminology, all external libraries are called dependencies. As a rule, they are packaged in JAR files. To add an external library to a project, you need to do exactly two steps: - Define a repository where to search for dependencies. - Define a dependency that you want to include in your project. 2. Repositories and where to find them Repositories are just places where libraries are stored. There are different possible formats of repositories: - a Maven compatible repository (e.g.: Maven Central, Google) - an Ivy compatible repository - local (flat) directories 3. Repository definition Defining a repository in Gradle is a piece of cake. Just add this to your build.gradle file: repositories { mavenCentral() jcenter() } Also, you can just download the jars you need and place them into some directory on your computer, commonly in the libs folder of your project. This comes in handy when the jars you need are not available in public repositories. repositories { flatDir { dirs 'libs' } } ################################# 1.1.1.1.4. Gradle 1. Overview Gradle is a modern automation tool that helps build and manage projects written in Java, Kotlin, Scala, and other JVM-based languages. It describes project dependencies and determines how to build a project. One of the best things about Gradle is its Groovy-based domain-specific language (DSL - Ngôn ngữ lập trình được thiết kế để giải quyết một vấn đề cụ thể (domain)) that gives developers a specific way to form custom build scripts. (tạo tập lệnh tùy chỉnh). There are two languages to write the Gradle build scripts (Groovy and Kotlin) and you can choose any of 2. Key Concepts The key features of Gradle are as follows: - Settings files: Gradle uses several types of settings files to describe how to build a project. - Build-by-convention: A programmer doesn't need to specify every building step that needs to be done. Instead, Gradle uses default settings and behavior. Although, every step of the default build process can be customized if necessary. - Dependency management: Gradle automatically downloads specified external libraries and solves conflict cases with dependencies. You can declare as many dependencies as you need for a project. - Builds: Gradle allows programmers to design well-structured and easily maintained comprehensible builds. It also supports complex cases such as multi-project or partial builds. - Ease of migration (Di chuyển dễ dàng): Gradle can easily adapt to any project structure you have. Therefore, you can always develop your project exactly the way you want. - DSL (based on Groovy and Kotlin) for writing scripts in settings files. ################################### 1.1.1.2. BuildTools 1. What is a build tool? Build tool là một chương trình tự động hóa quá trình tạo ứng dụng thực thi được từ mã nguồn. Quá trình xây dựng bao gồm biên dịch, liên kết (linking), đóng gói (packing) code thành dạng có thể sử dụng được hoặc thực thi được. A build tool is a program that automates the process of creating executable applications from source code. The build process includes compiling sources and linking and packaging the code into a usable or executable form. Một build tool tự động thường nhanh hơn người thực hiện các bước tương tự theo cách thủ công. Do đó quá trình build tự động sẽ cải thiện chất lượng sản phẩm và tiết kiệm chi phí. Automating the build process minimizes the risk of human error. Additionally, an automated build tool typically runs faster than someone performing the same steps manually. As a consequence, an automated build process improves the quality of the product and saves time and money. 2. What can build tools do? - Downloading and adding dependencies. - Compiling source code into bytecode. - Packaging compiled code. - Running tests. - Deploying to a production environment. 3. Build tools for Java-based projects - Apache Ant - Apache Maven - Gradle ##################################### 1.1.1.3. JarFile 1. What is a Java Archive? Java Archive (JAR) là một định dạng file độc lập với nền tảng để đóng gói nhiều tệp với nhau và phân phối chúng dưới dạng một đơn vị. Nó hữu ích khi ứng dụng có rất nhiều file Những lợi ích chính của JAR: Có thể tổng hợp nhiều file của nhiều loại khác nhau Nó là một kho lưu trữ nén (với thuật toán ZIP) giúp làm giảm kích thước của ứng dụng và làm nó dễ dàng khi chuyển qua network Có thể ký điện tử vào nó JRE có thể khởi động một ứng dụng được đóng gói vào một JAR nhưng để tạo một JAR thì cần sử dụng JDK 2. The structure of a JAR file A JAR file is simply an aggregation of bytecode files (.class), configuration files (e.g., .json, .xml), images, and even sound clips into a single compressed file. All files except bytecode files are usually called resources. It is also recommended that a JAR file contains a special file named MANIFEST.MF in a special folder named META-INF. This file should describe the JAR file itself (a manifest is a kind of metadata): its version, the author, and so on. example.jar ├── META-INF │ └── MANIFEST.MF ├── second │ ├── Main.class │ └── MyIcon.png └── third └── another └── OneMore.class 3. Running a JAR file There are two ways to run a JAR file, depending on whether you want to use the Main-Class header in the manifest file or not. If this header is not present or you want to specify the main class manually, run: java -cp app-without-main-class-header.jar path.to.Main If this header is present, then run: java -jar app-with-main-class-header.jar ######################### 1.2. essentials ######################### 1.3. jvm ######################### 2. javacore 2.1. addition 2.1.1. annotations Main.java package javacore.addition.annotations; public class Main { public static void main(String[] args) { // oldMethod(); // printHello(); SuperClass superClass = new SubClass(); superClass.invokeInstanceMethod(); SuperClass nullSuperClass = null; nullSuperClass.invokeInstanceMethod(); } // Phuong thuc nay khong duoc dung nua (da loi thoi hoac khong con duoc dung) @Deprecated public static void oldMethod() { System.out.println("Hello!"); } // An di mot so canh bao trong thoi gian bien dich @SuppressWarnings("unused") public static void printHello() { System.out.println("Hello!"); } } class SuperClass { public void invokeInstanceMethod() { System.out.println("SuperClass: invokeInstanceMethod"); } } class SubClass extends SuperClass { @Override public void invokeInstanceMethod() { System.out.println("SubClass: invokeInstanceMethod is overridden"); } // @Override -- method doesn't override anything public void invokeInstanceMethod(String s) { System.out.println("SubClass: overloaded invokeInstanceMethod(String)"); } } ################################### 2.1.2. essentialstandardclass 2.1.2.1. computation 2.1.2.1.1 Math package javacore.addition.essentialstandardclass.computation; public class Math { public static void main(String[] args) { java.lang.Math.max(5, 10); java.lang.Math.min(5, 10); java.lang.Math.sqrt(64); java.lang.Math.abs(-4.5); double pi = java.lang.Math.PI; int randomNum = (int)(java.lang.Math.random() * 101); //0 to 100 System.out.println(randomNum); boolean isJavaFun = true; boolean isFishTasty = false; System.out.println(isJavaFun); // Outputs true System.out.println(isFishTasty); // Outputs false int x = 10; int y = 9; System.out.println(x > y); // returns true, because 10 is higher than 9 int myAge = 25; int votingAge = 18; if (myAge >= votingAge) { System.out.println("Old enough to vote!"); } else { System.out.println("Not old enough to vote."); } double floor = java.lang.Math.floor(3.78); // floor is 3.0 double ceil = java.lang.Math.ceil(4.15); // ceil is 5.0 } } ########################################## 2.1.2.1.2 MyRandom package javacore.addition.essentialstandardclass.computation; import java.util.Scanner; /* * Cac so random trong Java khong thuc su ngau nhien, no luon duoc xac dinh boi mot gia tri ban dau (initial value) * duoc goi la seed. Moi khi tao mot so random moi, ta luon co so tiep theo trong chuoi so xac dinh truoc. Nhung so nay * duoc goi la pseudorandom. Tuc la co the tinh toan toan bo cac so neu biet seed va thuat toan cua chuoi so. Chac chan * rang cung mot seed se tao ra cung mot chuoi so neu cung phien ban java runtime * * */ public class Random { public static void main(String[] args) { //test(); exercise(); } public static void test() { java.util.Random random1 = new java.util.Random(); java.util.Random random2 = new java.util.Random(1000); System.out.println(random1.nextInt()); System.out.println(random2.nextInt()); System.out.println(random1.nextInt(10000)); // 0..9999 System.out.println(random2.nextInt(99)); // 0..98 // pseudorandom integer in a range Scanner scanner = new Scanner(System.in); int lower = scanner.nextInt(); int upper = scanner.nextInt(); java.util.Random random = new java.util.Random(); int intervalLength = upper - lower + 1; System.out.println(random.nextInt(intervalLength) + lower); System.out.println(random.nextInt(intervalLength) + lower); System.out.println(random.nextInt(intervalLength) + lower); System.out.println(random.nextInt(intervalLength) + lower); } public static void exercise() { Scanner scanner = new Scanner(System.in); int K = scanner.nextInt(); int N = scanner.nextInt(); double M = scanner.nextDouble(); double num; java.util.Random random = new java.util.Random(K); boolean check = true; while (check) { random = new java.util.Random(K); for (int i = 0; i < N; i++) { num = random.nextGaussian(); if (num > M) { check = true; K++; break; } check = false; } } System.out.println(K); } } ######################### 2.1.2.2. dataandtime 2.1.2.2.1. Exercise package javacore.addition.essentialstandardclass.dateandtime; import java.time.LocalDate; import java.util.Scanner; public class Exercise { public static void main(String[] args) { // theLastDayOfMonth(); } public static void theLastDayOfMonth() { Scanner sc = new Scanner(System.in); int year = sc.nextInt(); int day = sc.nextInt(); boolean check = false; LocalDate date = LocalDate.of(year, 1,1); for (int i = 1; i <= 12; i++) { if (day > 0) { day -= date.lengthOfMonth(); date = date.plusMonths(1); } else if (day == 0) { check = true; break; } else { break; } } System.out.println(check); } } ######################### 2.1.2.2.2. MyLocaldate package javacore.addition.essentialstandardclass.dateandtime; import java.time.DayOfWeek; import java.time.LocalDate; public class MyLocalDate { public static void main(String[] args) { creatingLocalDate(); yearMonthDay(); arithmeticMethods(); } public static void creatingLocalDate() { LocalDate now = LocalDate.now(); System.out.println(now); // 2024-01-08 LocalDate date1 = LocalDate.of(1999, 3, 14); LocalDate date2 = LocalDate.parse("2017-11-25"); LocalDate date3 = LocalDate.ofYearDay(2020, 44); // 2020-02-13 } public static void yearMonthDay() { LocalDate date = LocalDate.of(1999, 3, 14);; int year = date.getYear(); int month = date.getMonthValue(); int dayOfMonth = date.getDayOfMonth(); int dayOfYear = date.getDayOfYear(); // 73 DayOfWeek dayOfWeek = date.getDayOfWeek(); // SUNDAY int lenOfYear = date.lengthOfYear(); int lenOfMonth = date.lengthOfMonth(); } public static void arithmeticMethods() { LocalDate date = LocalDate.of(2017, 1, 1); // 2017-01-01 (1 January 2017) LocalDate tomorrow = date.plusDays(1); // 2017-01-02 (2 January 2017) LocalDate yesterday = date.minusDays(1); // 2016-12-31 (31 December 2016) LocalDate inTwoYears = date.plusYears(2); // 2019-01-01 (1 January 2019) LocalDate in2016 = date.withYear(2016); // 2016-01-01 (1 January 2016) System.out.println(LocalDate.parse("2018-01-10").minusMonths(2).withDayOfMonth(1)); } } ######################### 2.1.3. regularexpression Main package javacore.addition.regularexpression; /* * [abc] Tim mot ki tu trong pham vi dau ngoac * [0-9] tim mot ki tu tu 0 den 9 * [^abc] [^a-z] tim mot ki tu khong nam trong pham vi dau ngoac * * * */ public class Main { public static void main(String[] args) { } } ######################### Main.java public class Main { public static void main(String[] args) { jvmVersion(); } public static void binary() { System.out.println(0b10); // 2 } public static void jvmVersion() { System.out.println(System.getProperty("java.vm.version")); // 17.0.8+7-LTS } } class MyClass { static final String TEXT = "Hello"; int magic = 10; static void doSomething() { System.out.println(TEXT); } void doDo() { doSomething(); System.out.println(TEXT); System.out.println(magic); } }
Editor is loading...
Leave a Comment