gradle学习(4)-构建java项目
最后更新于:2022-04-01 06:51:27
## 1.认识plugin
告诉gradle某个项目是特殊的项目。比如java和android
## 2.定义java项目
~~~
apply plugin: 'java'
~~~
就一步就可以定义一个java项目。上面就是告诉gradle我需要你准备java项目所需的组件来满足需求。
## 3.java项目的目录结构
存放java源码的目录:src/main/java
~~~
mkdir -p src/main/java
~~~
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-07_568e46651b9d1.jpg)
存放测试源码的目录:src/test/java
~~~
mkdir -p src/test/java
~~~
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-07_568e466536741.jpg)
存放jar包要包含的其他文件(例如资源文件)的目录:src/main/resources
存放执行case需要的文件的目录:src/test/resources
所有新生成的文件存放目录:build
存放jar包目录或者java项目自身压缩后的jar包存放的目录:build/libs
(build目录gradle会自己生成)。
## 4.构建项目
使用gradle build命令
~~~
qianhuis-Mac-mini:1228_1 qianhui$ gradle build
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar
:assemble
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build
BUILD SUCCESSFUL
Total time: 10.155 secs
~~~
项目的根目录下多了build目录,build下多了libs和tmp两个文件夹。
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-07_568e466551ac2.jpg)
## 5.清除上次的构建
使用命令 gradle clean
~~~
qianhuis-Mac-mini:1228_1 qianhui$ gradle clean
:clean
BUILD SUCCESSFUL
Total time: 3.764 secs
~~~
会发现build目录消失了。
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-07_568e466573fba.jpg)
## 6.只编译java源码,生成jar包。不执行测试
使用命令gradle assemble
~~~
qianhuis-Mac-mini:1228_1 qianhui$ gradle assemble
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar
:assemble
BUILD SUCCESSFUL
Total time: 9.968 secs
~~~
注意它和build的区别在于,执行只到:assemble就停止了,生成build目录,而build还多了以下任务。
~~~
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build
~~~
## 7.编译和测试代码
~~~
qianhuis-Mac-mini:1228_1 qianhui$ gradle check
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
BUILD SUCCESSFUL
Total time: 2.854 secs
~~~
只进行测试,不生成build目录。
## 8.扩展依赖-dependencies
java项目一般会依赖外部jar包,之前我们都会通过maven获取这些jar包。gradle也是这么做多。使用
~~~
repositories
~~~
任务来获取maven中央库。这是项目告诉gradle去什么地方找所需要的jar包。具体如下
~~~
apply plugin: 'java'
repositories{
mavenCentral()
}
~~~
在java编译的时候,需要一些用到一些jar包。这个时候可以通过dependencies任务来指定这些依赖的jar包,构建项目的时候,gradle会去中央库里下载这些jar包到本地
~~~
apply plugin: 'java'
repositories{
mavenCentral()
}
dependencies {
compile group:'commons-collections',name:'commons-collections',version:'3.2'
testCompile group:'junit',name:'junit',version:'4.0+'
}
~~~
## 9.定制项目
#### 修改MANIFEST.MF文件中的内容
~~~
Manifest-Version: 1.0
~~~
给该文件添加一些熟悉
~~~
apply plugin: 'java'
repositories{
mavenCentral()
}
dependencies {
compile group:'commons-collections',name:'commons-collections',version:'3.2'
testCompile group:'junit',name:'junit',version:'4.0+'
}
sourceCompatibility = 1.5
version = '1.0'
jar {
manifest {
attributes 'Implementation-Title':'Gradle Quickstart',
'Implementation-Version':version
}
}
~~~
然后该文件的内容会添加2个属性。而且生成的jar包也会多加了个后缀
~~~
Manifest-Version: 1.0
Implementation-Title: Gradle Quickstart
Implementation-Version: 1.0
~~~
jar包名为:1228_1-1.0.jar
说明我们可以随意的给任务添加属性。
#### 给某一个任务添加一个系统属性
~~~
test {
systemProperties 'property': 'value'
}
~~~
可以通过命令gradle properties来浏览所有的属性
~~~
qianhuis-Mac-mini:1228_1 qianhui$ gradle properties
:properties
------------------------------------------------------------
Root project
------------------------------------------------------------
allprojects: [root project '1228_1']
ant: org.gradle.api.internal.project.DefaultAntBuilder@310a7aa1
antBuilderFactory: org.gradle.api.internal.project.DefaultAntBuilderFactory@26f8f92e
archivesBaseName: 1228_1
artifacts: org.gradle.api.internal.artifacts.dsl.DefaultArtifactHandler_Decorated@23af4eff
asDynamicObject: org.gradle.api.internal.ExtensibleDynamicObject@4a6b59a6
assemble: task ':assemble'
baseClassLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderScope@4dcac775
binaries: [classes 'main', classes 'test']
build: task ':build'
buildDependents: task ':buildDependents'
buildDir: /Users/qianhui/Documents/Developer/gradle_project/1228_1/build
buildFile: /Users/qianhui/Documents/Developer/gradle_project/1228_1/build.gradle
buildNeeded: task ':buildNeeded'
buildScriptSource: org.gradle.groovy.scripts.UriScriptSource@5e5064ba
buildTasks: [build]
buildscript: org.gradle.api.internal.initialization.DefaultScriptHandler@70051fb5
check: task ':check'
childProjects: {}
class: class org.gradle.api.internal.project.DefaultProject_Decorated
classLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderScope@6064dd87
classes: task ':classes'
clean: task ':clean'
compileJava: task ':compileJava'
compileTestJava: task ':compileTestJava'
components: [org.gradle.api.internal.java.JavaLibrary@7e0bfb81]
configurationActions: org.gradle.configuration.project.DefaultProjectConfigurationActionContainer@44ad12cd
configurations: [configuration ':archives', configuration ':compile', configuration ':default', configuration ':runtime', configuration ':testCompile', configuration ':testRuntime']
convention: org.gradle.api.internal.plugins.DefaultConvention@3ead73f4
defaultArtifacts: org.gradle.api.internal.plugins.DefaultArtifactPublicationSet_Decorated@6d97b851
defaultTasks: []
dependencies: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler_Decorated@3c6060d
dependencyCacheDir: /Users/qianhui/Documents/Developer/gradle_project/1228_1/build/dependency-cache
dependencyCacheDirName: dependency-cache
depth: 0
description: null
distsDir: /Users/qianhui/Documents/Developer/gradle_project/1228_1/build/distributions
distsDirName: distributions
docsDir: /Users/qianhui/Documents/Developer/gradle_project/1228_1/build/docs
docsDirName: docs
ext: org.gradle.api.internal.plugins.DefaultExtraPropertiesExtension@3db7416a
extensions: org.gradle.api.internal.plugins.DefaultConvention@3ead73f4
fileOperations: org.gradle.api.internal.file.DefaultFileOperations@4a27b5cd
fileResolver: org.gradle.api.internal.file.BaseDirFileResolver@7510cbc3
gradle: build '1228_1'
group:
inheritedScope: org.gradle.api.internal.ExtensibleDynamicObject$InheritedDynamicObject@21b1ab40
jar: task ':jar'
javadoc: task ':javadoc'
libsDir: /Users/qianhui/Documents/Developer/gradle_project/1228_1/build/libs
libsDirName: libs
logger: org.gradle.api.logging.Logging$LoggerImpl@17620808
logging: org.gradle.logging.internal.DefaultLoggingManager@24fd5136
modelRegistry: org.gradle.model.internal.registry.DefaultModelRegistry@72dec332
module: org.gradle.api.internal.artifacts.ProjectBackedModule@5c92e1b3
name: 1228_1
parent: null
parentIdentifier: null
path: :
plugins: [org.gradle.api.plugins.HelpTasksPlugin@5a1698f1, org.gradle.language.base.plugins.LifecycleBasePlugin@522f63e7, org.gradle.api.plugins.BasePlugin@2aa9233d, org.gradle.api.plugins.ReportingBasePlugin@2b040d5b, org.gradle.language.base.plugins.LanguageBasePlugin@7a96cf9, org.gradle.api.plugins.LegacyJavaComponentPlugin@6d3725a1, org.gradle.api.plugins.JavaBasePlugin@6d935671, org.gradle.api.plugins.JavaPlugin@1547cbc9]
processOperations: org.gradle.api.internal.file.DefaultFileOperations@4a27b5cd
processResources: task ':processResources'
processTestResources: task ':processTestResources'
project: root project '1228_1'
projectDir: /Users/qianhui/Documents/Developer/gradle_project/1228_1
projectEvaluationBroadcaster: ProjectEvaluationListener broadcast
projectEvaluator: org.gradle.configuration.project.LifecycleProjectEvaluator@7ee14288
projectRegistry: org.gradle.api.internal.project.DefaultProjectRegistry@76d72fcb
properties: {...}
rebuildTasks: [clean, build]
reporting: org.gradle.api.reporting.ReportingExtension_Decorated@2982f561
reportsDir: /Users/qianhui/Documents/Developer/gradle_project/1228_1/build/reports
repositories: [org.gradle.api.internal.artifacts.repositories.DefaultMavenArtifactRepository_Decorated@54bbc891]
resources: org.gradle.api.internal.resources.DefaultResourceHandler@746cd30c
rootDir: /Users/qianhui/Documents/Developer/gradle_project/1228_1
rootProject: root project '1228_1'
runtimeClasspath: file collection
scriptHandlerFactory: org.gradle.api.internal.initialization.DefaultScriptHandlerFactory@1493f48b
scriptPluginFactory: org.gradle.configuration.DefaultScriptPluginFactory@7d9bdc3a
serviceRegistryFactory: org.gradle.internal.service.scopes.ProjectScopeServices$5@397a443
services: ProjectScopeServices
sourceCompatibility: 1.5
sourceSets: [source set 'main', source set 'test']
sources: [source set 'main', source set 'test']
standardOutputCapture: org.gradle.logging.internal.DefaultLoggingManager@24fd5136
state: project state 'EXECUTED'
status: integration
subprojects: []
targetCompatibility: 1.5
tasks: [task ':assemble', task ':build', task ':buildDependents', task ':buildNeeded', task ':check', task ':classes', task ':clean', task ':compileJava', task ':compileTestJava', task ':jar', task ':javadoc', task ':processResources', task ':processTestResources', task ':properties', task ':test', task ':testClasses']
test: task ':test'
testClasses: task ':testClasses'
testReportDir: /Users/qianhui/Documents/Developer/gradle_project/1228_1/build/reports/tests
testReportDirName: tests
testResultsDir: /Users/qianhui/Documents/Developer/gradle_project/1228_1/build/test-results
testResultsDirName: test-results
version: 1.0
BUILD SUCCESSFUL
Total time: 3.311 secs
~~~
## 10.发布项目
~~~
sourceCompatibility = 1.5
version = '2.0'
jar {
manifest {
attributes 'Implementation-Title':'Gradle Quickstart',
'Implementation-Version':version
}
}
test {
systemProperties 'property':'value'
}
uploadArchives {
repositories {
flatDir {
dirs 'repos'
}
}
}
~~~
执行该task:
~~~
qianhuis-Mac-mini:1228_1 qianhui$ gradle uploadArchives
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar
:uploadArchives
BUILD SUCCESSFUL
Total time: 3.205 secs
~~~
在项目根目录下生成repos目录,里面生成了可发布的jar包和xml文件等。
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-07_568e466589001.jpg)