Spring 学习 171005 Building Java Projects with Gradle
Posted BugQ
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring 学习 171005 Building Java Projects with Gradle相关的知识,希望对你有一定的参考价值。
网址:
http://spring.io/guides/gs/gradle/#initial
1.git clone https://github.com/spring-guides/gs-gradle.git
2.cd gs-gradle/initial
3.install Gradle
需要安装sdkman
到http://sdkman.io/,点击右上角的install
执行命令:
$ curl -s "https://get.sdkman.io"| bash
出错
Please install zip on your system using yourfavourite package manager.
查找解决返回
到 https://github.com/bmatzelle/gow/releases网站
下载Gow-0.8.0.exe,并且安装
然后,重新开sourcetree,开terminal,有了 zip
注意: C:\Program Files (x86)\VMware\VMware Workstation的下面也有一个zip.exe,不知道是否好用
zip在 C:\Program Files(x86)\Gow\bin
souretree的terminal在C:\Users\k\AppData\Local\Atlassian\SourceTree\git_local\mingw32\bin
执行$ curl -s "https://get.sdkman.io" | bash
成功,
在执行
k@acer-pc MINGW32 /d/ws/gosrc (master)
$ source $HOME/.sdkman/bin/sdkman-init.sh
k@acer-pc MINGW32 /d/ws/gosrc (master)
$ sdk version
==== BROADCAST=================================================================
* 02/10/17: Gradle 4.2.1 released onSDKMAN! #gradle
* 29/09/17: Kotlin 1.1.51 released onSDKMAN! #kotlin
* 28/09/17: JDK 9-zulu by Azul is nowgenerally available for download from SDKMAN! on Linux, MacOS, and Windows.#java
================================================================================
SDKMAN 5.5.11+256
成功
然后装gradle
https://gradle.org/install/ 的网站有说明,如下
先测试java,再安装,结果不好用,不兼容的意思
k@acer-pc MINGW32 /d/ws/gosrc (master)
$ java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01,mixed mode)
k@acer-pc MINGW32 /d/ws/gosrc (master)
$ sdk install gradle 4.2
Stop! gradle 4.2 is not available. Possiblecauses:
*4.2 is an invalid version
*gradle binaries are incompatible with MINGW32_NT-10.0-WOW
到https://gradle.org/releases/ 下载4.2
下载完成后,创建D:\ws\Gradle\gradle-4.2,解压到这个文件夹中
然后设定环境变量,追加到PATH中,在最后即可。
新开command,测试,如下,成功
C:\Users\k>gradle -v
------------------------------------------------------------
Gradle 4.2
------------------------------------------------------------
Build time: 2017-09-20 14:48:23 UTC
Revision: 5ba503cc17748671c83ce35d7da1cffd6e24dfbd
Groovy: 2.4.11
Ant: Apache Ant(TM) version 1.9.6 compiledon June 29 2015
JVM: 1.7.0_45 (OracleCorporation 24.45-b08)
OS: Windows 8 6.2 amd64
重新开sourcetree+terminal,测试,也ok
k@acer-pc MINGW32 /d/ws/gosrc (master)
$ gradle -v
------------------------------------------------------------
Gradle 4.2
------------------------------------------------------------
Build time: 2017-09-20 14:48:23 UTC
Revision: 5ba503cc17748671c83ce35d7da1cffd6e24dfbd
Groovy: 2.4.11
Ant: Apache Ant(TM) version 1.9.6 compiledon June 29 2015
JVM: 1.7.0_45 (Oracle Corporation24.45-b08)
OS: Windows 8 6.2 amd64
那我费半天劲装sdk有什么用了?也许以后能够用得上
可以实验 gradle,gradle tasks
需要有个build.gradle配置文件,内容为apply plugin: 'java'
然后执行gradle build
k@acer-pc MINGW32 /d/ws/gs-gradle/initial(master)
$ gradle build
Support for running Gradle using Java 7 hasbeen deprecated and is scheduled to be removed in Gradle 5.0. Please seehttps://docs.gradle.org/4.2/userguide/java_plugin.html#sec:java_cross_compilationfor more 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 in 7s
2 actionable tasks: 2 executed
查看生成的build目录,有2个class文件和一个jar文件
接着练习:
到 complete目录里面,用最新的build.gradle文件
sourceCompatibility = 1.8
targetCompatibility = 1.8
这个地方会出错,开始怀疑是gradle的version问题,看gradle -v是1.7,下载了2.13版本,还是同样到问题。看version还是一样。
后来上网查,发现是自己的环境变量的java_home还是1.7,虽然jdk的path是1.8,但是gradle使用了java_home,所以用的1.7,就compile不过了
花费了2个小时啊
改完了javahome,再次执行,如下,
$ gradle build
:compileJava UP-TO-DATE
:processResources NO-SOURCE
:classes UP-TO-DATE
:jar UP-TO-DATE
:startScripts UP-TO-DATE
:distTar UP-TO-DATE
:distZip UP-TO-DATE
:assemble UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources NO-SOURCE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build UP-TO-DATE
BUILD SUCCESSFUL in 7s
7 actionable tasks: 7 up-to-date
可以看到之前出现的support java7的字样,其实是个warn,只不过自己没有注意到而已。惭愧啊
下一步,可以通过执行 gradle wrapper 来得到一个通用的jar 和 script进行仓库管理,这样其他人就不需要自己安装了
complete和initial目录下面都已经既存了,也可以找个其他的目录执行一下。就会发现这些文件会生成出来。
第一次在complete下面运行, ./gradlew.bat build ,会进行下载,然后生成build文件夹
k@acer-pc MINGW32 /d/ws/gs-gradle/complete(master)
$ ./gradlew.bat build
Downloadinghttps://services.gradle.org/distributions/gradle-2.13-bin.zip
执行jar命令,可以看生成的jar的文件的内容,注意有引号,因为javapath中有space
k@acer-pc MINGW32 /d/ws/gs-gradle/complete(master)
$ "$JAVA_HOME/bin/jar" tvf./build/libs/gs-gradle-0.1.0.jar
0 Thu Oct 05 07:43:56 CST 2017 META-INF/
25 Thu Oct 05 07:43:56 CST 2017 META-INF/MANIFEST.MF
0 Thu Oct 05 07:43:56 CST 2017 hello/
369 Thu Oct 05 07:43:56 CST 2017 hello/Greeter.class
988 Thu Oct 05 07:43:56 CST 2017 hello/HelloWorld.class
然后看直接运行的效果
k@acer-pc MINGW32 /d/ws/gs-gradle/complete(master)
$ ./gradlew.bat run
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:run
The current local time is: 07:55:14.693
Hello world!
BUILD SUCCESSFUL
Total time: 16.728 secs
最后附上一个完整的build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'
mainClassName = 'hello.HelloWorld'
// tag::repositories[]
repositories {
mavenCentral()
}
// end::repositories[]
// tag::jar[]
jar {
baseName = 'gs-gradle'
version = '0.1.0'
}
// end::jar[]
// tag::dependencies[]
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile "joda-time:joda-time:2.2"
testCompile "junit:junit:4.12"
}
// end::dependencies[]
// tag::wrapper[]
// end::wrapper[]
以上是关于Spring 学习 171005 Building Java Projects with Gradle的主要内容,如果未能解决你的问题,请参考以下文章
Building Microservices with Spring Cloud - Load balancing
Building Microservices with Spring Cloud - Service discovery
Building Microservices with Spring Cloud - Configuration Server
Building Microservices with Spring Cloud - Fault tolerance Circuit breaker
SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-002-Controller的requestMappingmodel
Building Microservices with Spring Boot and Apache Thrift. Part 1 with servlet