目錄結構
│ build.gradle
└─src
└─main
└─java
└─com
└─manning
└─gia
└─todo
│ ToDoApp.java
│
├─model
│ ToDoItem.java
│
├─repository
│ InMemoryToDoRepository.java
│ ToDoRepository.java
│
└─utils
CommandLineInput.java
CommandLineInputHandler.java
build.gradle
//使用 Java 插件
//默認在 src/main/java下查找源代碼
apply plugin: 'java'
執行構建命令
www.coderknock.com$ gradle build
Starting a Gradle Daemon, 1 incompatible and 1 stopped Daemons could not be reused, use --status for details
//編譯 Java 代碼
:compileJava
//處理資源【將 src/main/resource 下文件拷貝到 classes 此處沒有該文件夾所以被標記爲 NO-SOURCE】
:processResources NO-SOURCE
:classes
//打 jar 包
:jar
:assemble
//編譯 Java 測試代碼
:compileTestJava NO-SOURCE
//處理測試資源【將 src/test/resource 下文件拷貝到 classes 此處沒有該文件夾所以被標記爲 NO-SOURCE】
:processTestResources NO-SOURCE
:testClasses UP-TO-DATE
//進行單元測試
:test NO-SOURCE
:check UP-TO-DATE
:build
BUILD SUCCESSFUL
Total time: 13.767 secs
每一行都是 Java 插件提供的一個可執行任務,UP-TO-DATE
代表任務被跳過。
編譯之後
│ build.gradle
│
├─.gradle
│ ├─3.4
│ │ ├─file-changes
│ │ │ last-build.bin
│ │ │
│ │ ├─fileContent
│ │ │ fileContent.lock
│ │ │
│ │ └─taskHistory
│ │ fileHashes.bin
│ │ fileSnapshots.bin
│ │ taskHistory.bin
│ │ taskHistory.lock
│ │
│ └─buildOutputCleanup
│ built.bin
│ cache.properties
│ cache.properties.lock
│
├─build
│ ├─classes 【此目錄即編譯的 Java 的 class 文件的目錄】
│ │ └─main
│ │ └─com
│ │ └─manning
│ │ └─gia
│ │ └─todo
│ │ │ ToDoApp.class
│ │ │
│ │ ├─model
│ │ │ ToDoItem.class
│ │ │
│ │ ├─repository
│ │ │ InMemoryToDoRepository.class
│ │ │ ToDoRepository.class
│ │ │
│ │ └─utils
│ │ CommandLineInput.class
│ │ CommandLineInputHandler$1.class
│ │ CommandLineInputHandler.class
│ │
│ ├─libs
│ │ Project.jar 【打包的 jar 包,名稱是項目的目錄名】
│ │
│ └─tmp 【打 jar 包時使用的臨時文件】
│ ├─compileJava
│ └─jar
│ MANIFEST.MF
│
└─src
└─main
└─java
└─com
└─manning
└─gia
└─todo
│ ToDoApp.java
│
├─model
│ ToDoItem.java
│
├─repository
│ InMemoryToDoRepository.java
│ ToDoRepository.java
│
└─utils
CommandLineInput.java
CommandLineInputHandler.java
運行項目
www.coderknock.com$ java -cp build/classes/main/ com.manning.gia.todo.ToDoApp
--- To Do Application ---
Please make a choice:
(a)ll items
(f)ind a specific item
(i)nsert a new item
(u)pdate an existing item
(d)elete an existing item
(e)xit
> i
Please enter the name of the item:
> test
Successfully inserted to do item with ID 1.
--- To Do Application ---
Please make a choice:
(a)ll items
(f)ind a specific item
(i)nsert a new item
(u)pdate an existing item
(d)elete an existing item
(e)xit
> a
1: test [completed: false]
--- To Do Application ---
Please make a choice:
(a)ll items
(f)ind a specific item
(i)nsert a new item
(u)pdate an existing item
(d)elete an existing item
(e)xit
> exit
Please select a valid option!
--- To Do Application ---
Please make a choice:
(a)ll items
(f)ind a specific item
(i)nsert a new item
(u)pdate an existing item
(d)elete an existing item
(e)xit
> e
再來運行下 jar
www.coderknock.com$ java -jar Project.jar
Project.jar中沒有主清單屬性
我們發現沒有正確運行,下這是因爲,我們沒有在構建腳本中申明要生成清單文件MANIFEST.MF 下面我們來修改一下 build.gradle 相關的配置。
//使用 Java 插件
//默認在 src/main/java下查找源代碼
apply plugin: 'java'
//定義項目的版本
version = 0.1
//設置 Java 兼容版本
sourceCompatibility = 1.8
//jar 包相關配置
jar {
//清單文件配置
manifest {
//啓動類
attributes 'Main-Class': 'com.manning.gia.todo.ToDoApp'
}
}
再次編譯完成後,build/lib 目錄下的 jar 包名爲 Project-0.1.jar
www.coderknock.com$ java -jar Project-0.1.jar
--- To Do Application ---
Please make a choice:
(a)ll items
(f)ind a specific item
(i)nsert a new item
(u)pdate an existing item
(d)elete an existing item
(e)xit
可以看到,jar 包可以正常運行。