gradle当下其实相比于maven正变得越来越流行的一款构建工具,我们每一个开发人员都很有必要了解一下。今天我来剖析下gradle java项目结构。
build.gradle
这是gradle项目的构建脚本文件
settings.gradle
这是gradle环境配置文件
build目录
gradle默认的输出产物目录
用gradle来构建JAVA项目
在build.gradle中添加如下脚本:
plugins {
id 'java'
}
或者
apply plugin : 'java'
这个时候你的工程
主代码应该放在src/main/java
下
测试代码放在src/test/java
下
自定义源代码结构
如果想把
主代码放在src
下
测试代码放在test
下
构建脚本加如下配置
sourceSets {
main {
java {
srcDirs = ['src']
}
}
test {
java {
srcDirs = ['test']
}
}
}
Gradle Tasks
应用了以上java plugin,在idea中我们可以看到有如下可用任务
Tasks下面列出了所有可能任务的分组,以及每个分组下面有具体的哪些任务
gradle tasks
如同执行以上命令输出如下:
------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------
Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles test classes.
Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.
Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.
Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'mybatisplus'.
components - Displays the components produced by root project 'mybatisplus'. [incubating]
dependencies - Displays all dependencies declared in root project 'mybatisplus'.
dependencyInsight - Displays the insight into a specific dependency in root project 'mybatisplus'.
dependentComponents - Displays the dependent components of components in root project 'mybatisplus'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'mybatisplus'. [incubating]
projects - Displays the sub-projects of root project 'mybatisplus'.
properties - Displays the properties of root project 'mybatisplus'.
tasks - Displays the tasks runnable from root project 'mybatisplus'.
Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.
gradle build
执行以上命令,则会执行构建任务