目录结构
│ 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
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
:compileJava
:processResources NO
-SOURCE
:classes
:jar
:assemble
:compileTestJava 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
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.
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]
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!
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 相关的配置。
apply plugin:
'java'
version =
0.1
sourceCompatibility =
1.8
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
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 包可以正常运行。
相关代码
转载请注明原文地址: https://ju.6miu.com/read-22924.html