当前位置:   article > 正文

gradle-spring源码构建_gradle构建spring源码

gradle构建spring源码

1.准备环境

1.1 下载spring源码

下载spring源码。通过idea直接打开,如果有build直接关掉build,因为在没改配置前,build会失败

1.2 gradle版本选择

打开文件gradle-wrapper.properties,可以查看spring版本需要的gradle版本

在这里插入图片描述

1.3 安装gradle

下载对应的gradle版本,并安装,安装教程很多,本文不在描述,本文spring版本对应的gradle版本为Gradle 4.3.1

2.改maven仓库,构建spring

在根目录下找到build.gradle文件,全局搜下这个configurations.all,把整个都注释掉然后加上阿里云的库,不然你打完王者都还没结束

//	configurations.all {
//		// Check for updates every build
//		resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
//	}
	allprojects {
		repositories {
			maven{ url 'http://maven.aliyun.com/nexus/content/groups/public'}
		}
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

在这里插入图片描述

保存好,build下,等几分钟就好了

完整的build.gradle文件

buildscript {
	repositories {
		maven { url "https://repo.spring.io/plugins-release" }
	}
	dependencies {
		classpath("io.spring.gradle:propdeps-plugin:0.0.8")
		classpath("io.spring.gradle:docbook-reference-plugin:0.3.1")
		classpath("org.asciidoctor:asciidoctorj-pdf:1.5.0-alpha.16")
		classpath("org.asciidoctor:asciidoctorj-epub3:1.5.0-alpha.7")
	}
}

// 3rd party plugin repositories can be configured in settings.gradle
plugins {
	id "com.gradle.build-scan" version "1.8"
	id "io.spring.dependency-management" version "1.0.3.RELEASE" apply false
	id "org.jetbrains.kotlin.jvm" version "1.1.61" apply false
	id "org.jetbrains.dokka" version "0.9.17"
	id "org.asciidoctor.convert" version "1.5.6"
}

buildScan {
	licenseAgreementUrl = 'https://gradle.com/terms-of-service'
	licenseAgree = 'yes'
}

ext {
	linkHomepage = 'https://projects.spring.io/spring-framework'
	linkCi = 'https://build.spring.io/browse/SPR'
	linkIssue = 'https://jira.spring.io/browse/SPR'
	linkScmUrl = 'https://github.com/spring-projects/spring-framework'
	linkScmConnection = 'scm:git:git://github.com/spring-projects/spring-framework.git'
	linkScmDevConnection = 'scm:git:ssh://git@github.com:spring-projects/spring-framework.git'

	moduleProjects = subprojects.findAll {
		!it.name.equals('spring-build-src') && !it.name.equals('spring-framework-bom')
	}
}

configure(allprojects) { project ->
	group = "org.springframework"
	version = qualifyVersionIfNecessary(version)

	ext.aspectjVersion       = "1.8.13"
	ext.freemarkerVersion    = "2.3.27-incubating"
	ext.groovyVersion        = "2.4.13"
	ext.hsqldbVersion        = "2.4.0"
	ext.jackson2Version      = "2.9.2"
	ext.jettyVersion         = "9.4.7.v20170914"
	ext.junitJupiterVersion  = "5.0.2"
	ext.junitPlatformVersion = "1.0.2"
	ext.junitVintageVersion  = "4.12.2"
	ext.kotlinVersion        = "1.1.61"
	ext.log4jVersion         = "2.10.0"
	ext.nettyVersion         = "4.1.17.Final"
	ext.reactorVersion       = "Bismuth-SR4"
	ext.rxjavaVersion        = "1.3.4"
	ext.rxjavaAdapterVersion = "1.2.1"
	ext.rxjava2Version       = "2.1.6"
	ext.slf4jVersion         = "1.7.25"
	ext.tiles3Version        = "3.0.8"
	ext.tomcatVersion        = "8.5.23"
	ext.undertowVersion      = "1.4.21.Final"

	ext.gradleScriptDir = "${rootProject.projectDir}/gradle"

	apply plugin: "propdeps"
	apply plugin: "java"
	apply plugin: "test-source-set-dependencies"
	apply from: "${gradleScriptDir}/ide.gradle"

	apply plugin: "kotlin"
	compileKotlin {
		kotlinOptions.jvmTarget = "1.8"
		kotlinOptions.freeCompilerArgs = ["-Xskip-runtime-version-check", "-Xjsr305=strict"]

	}
	compileTestKotlin {
		kotlinOptions.jvmTarget = "1.8"
		kotlinOptions.freeCompilerArgs = ["-Xskip-runtime-version-check", "-Xjsr305=strict"]
	}

//	configurations.all {
//		// Check for updates every build
//		resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
//	}
	allprojects {
		repositories {
			maven{ url 'http://maven.aliyun.com/nexus/content/groups/public'}
		}
	}

	def commonCompilerArgs =
			["-Xlint:serial", "-Xlint:cast", "-Xlint:classfile", "-Xlint:dep-ann",
			 "-Xlint:divzero", "-Xlint:empty", "-Xlint:finally", "-Xlint:overrides",
			 "-Xlint:path", "-Xlint:processing", "-Xlint:static", "-Xlint:try", "-Xlint:-options"]

	compileJava.options*.compilerArgs = commonCompilerArgs +
			["-Xlint:varargs", "-Xlint:fallthrough", "-Xlint:rawtypes",
			 "-Xlint:deprecation", "-Xlint:unchecked", "-Werror"]

	compileTestJava.options*.compilerArgs = commonCompilerArgs +
			["-Xlint:-varargs", "-Xlint:-fallthrough","-Xlint:-rawtypes",
			 "-Xlint:-deprecation", "-Xlint:-unchecked"]

	compileJava {
		sourceCompatibility = 1.8
		targetCompatibility = 1.8
		options.encoding = 'UTF-8'
	}

	compileTestJava {
		sourceCompatibility = 1.8
		targetCompatibility = 1.8
		options.encoding = 'UTF-8'
		options.compilerArgs += "-parameters"
	}

	test {
		systemProperty("java.awt.headless", "true")
		systemProperty("testGroups", project.properties.get("testGroups"))
		scanForTestClasses = false
		include(["**/*Tests.class", "**/*Test.class"])
		// Since we set scanForTestClasses to false, we need to filter out inner
		// classes with the "$" pattern; otherwise, using -Dtest.single=MyTests to
		// run MyTests by itself will fail if MyTests contains any inner classes.
		exclude(["**/Abstract*.class", '**/*$*'])
		reports.junitXml.setDestination(file("$buildDir/test-results"))
	}

	repositories {
		maven { url "https://repo.spring.io/libs-release" }
		maven { url "https://repo.spring.io/milestone" }  // for AspectJ 1.9 beta
	}

	dependencies {
		testCompile("junit:junit:4.12") {
			exclude group:'org.hamcrest', module:'hamcrest-core'
		}
		testCompile("org.mockito:mockito-core:2.12.0") {
			exclude group:'org.hamcrest', module:'hamcrest-core'
		}
		testCompile("com.nhaarman:mockito-kotlin:1.5.0") {
			exclude module:'kotlin-stdlib'
			exclude module:'kotlin-reflect'
			exclude module:'mockito-core'
		}
		testCompile("org.hamcrest:hamcrest-all:1.3")
		testCompile("org.xmlunit:xmlunit-matchers:2.3.0")
		testRuntime("org.apache.logging.log4j:log4j-core:${log4jVersion}")
        // JSR-305 only used for non-required meta-annotations
		compileOnly("com.google.code.findbugs:jsr305:3.0.2")
		testCompileOnly("com.google.code.findbugs:jsr305:3.0.2")
	}

	ext.javadocLinks = [
		"http://docs.oracle.com/javase/8/docs/api/",
		"http://docs.oracle.com/javaee/7/api/",
		"http://docs.oracle.com/cd/E13222_01/wls/docs90/javadocs/",  // CommonJ
		"http://pic.dhe.ibm.com/infocenter/wasinfo/v7r0/topic/com.ibm.websphere.javadoc.doc/web/apidocs/",
		"http://glassfish.java.net/nonav/docs/v3/api/",
		"http://docs.jboss.org/jbossas/javadoc/4.0.5/connector/",
		"http://docs.jboss.org/jbossas/javadoc/7.1.2.Final/",
		"http://tiles.apache.org/tiles-request/apidocs/",
		"http://tiles.apache.org/framework/apidocs/",
		"http://www.eclipse.org/aspectj/doc/released/aspectj5rt-api/",
		"http://ehcache.org/apidocs/2.10.4",
		"http://quartz-scheduler.org/api/2.2.1/",
		"http://fasterxml.github.io/jackson-core/javadoc/2.8/",
		"http://fasterxml.github.io/jackson-databind/javadoc/2.8/",
		"http://fasterxml.github.io/jackson-dataformat-xml/javadoc/2.8/",
		"http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/"
	] as String[]
}

configure(subprojects - project(":spring-build-src")) { subproject ->
	apply from: "${gradleScriptDir}/publish-maven.gradle"

	jar {
		manifest.attributes["Implementation-Title"] = subproject.name
		manifest.attributes["Implementation-Version"] = subproject.version
		manifest.attributes["Automatic-Module-Name"] = subproject.name.replace('-', '.')  // for Jigsaw
		manifest.attributes["Created-By"] =
				"${System.getProperty("java.version")} (${System.getProperty("java.specification.vendor")})"

		from("${rootProject.projectDir}/src/dist") {
			include "license.txt"
			include "notice.txt"
			into "META-INF"
			expand(copyright: new Date().format("yyyy"), version: project.version)
		}
	}

	javadoc {
		description = "Generates project-level javadoc for use in -javadoc jar"

		options.memberLevel = org.gradle.external.javadoc.JavadocMemberLevel.PROTECTED
		options.author = true
		options.header = project.name
		options.use = true
		options.links(project.ext.javadocLinks)
		options.addStringOption('Xdoclint:none', '-quiet')

		// Suppress warnings due to cross-module @see and @link references.
		// Note that global 'api' task does display all warnings.
		logging.captureStandardError LogLevel.INFO
		logging.captureStandardOutput LogLevel.INFO  // suppress "## warnings" message
	}

	task sourcesJar(type: Jar, dependsOn: classes) {
		duplicatesStrategy = DuplicatesStrategy.EXCLUDE
		classifier = 'sources'
		from sourceSets.main.allSource
		// Don't include or exclude anything explicitly by default. See SPR-12085.
	}

	task javadocJar(type: Jar) {
		classifier = "javadoc"
		from javadoc
	}

	artifacts {
		archives sourcesJar
		archives javadocJar
	}
}

configure(rootProject) {
	description = "Spring Framework"

	apply plugin: "groovy"
	apply plugin: "io.spring.dependency-management"
	apply from: "${gradleScriptDir}/jdiff.gradle"
	apply from: "${gradleScriptDir}/docs.gradle"

	dependencyManagement {
		imports {
			mavenBom "io.projectreactor:reactor-bom:${reactorVersion}"
		}
		resolutionStrategy {
			cacheChangingModulesFor 0, 'seconds'
		}
		applyMavenExclusions = false
	}

	// don't publish the default jar for the root project
	configurations.archives.artifacts.clear()

	dependencies {  // for integration tests
		testCompile(project(":spring-aop"))
		testCompile(project(":spring-beans"))
		testCompile(project(":spring-context"))
		testCompile(project(":spring-core"))
		testCompile(project(":spring-expression"))
		testCompile(project(":spring-jdbc"))
		testCompile(project(":spring-orm"))
		testCompile(project(":spring-test"))
		testCompile(project(":spring-tx"))
		testCompile(project(":spring-web"))
		testCompile("javax.inject:javax.inject:1")
		testCompile("javax.resource:javax.resource-api:1.7")
		testCompile("javax.servlet:javax.servlet-api:3.1.0")
		testCompile("org.aspectj:aspectjweaver:${aspectjVersion}")
		testCompile("org.hsqldb:hsqldb:${hsqldbVersion}")
		testCompile("org.hibernate:hibernate-core:5.1.10.Final")
	}

	artifacts {
		archives docsZip
		archives schemaZip
		archives distZip
	}

	task wrapper(type: Wrapper) {
		description = "Generates gradlew[.bat] scripts"
		gradleVersion = '4.3.1'

		doLast() {
			def gradleOpts = "-XX:MaxMetaspaceSize=1024m -Xmx1024m"
			def gradleBatOpts = "$gradleOpts -XX:MaxHeapSize=256m"
			File wrapperFile = file("gradlew")
			wrapperFile.text = wrapperFile.text.replace("DEFAULT_JVM_OPTS=",
					"GRADLE_OPTS=\"$gradleOpts \$GRADLE_OPTS\"\nDEFAULT_JVM_OPTS=")
			File wrapperBatFile = file("gradlew.bat")
			wrapperBatFile.text = wrapperBatFile.text.replace("set DEFAULT_JVM_OPTS=",
					"set GRADLE_OPTS=$gradleBatOpts %GRADLE_OPTS%\nset DEFAULT_JVM_OPTS=")
		}
	}

}

/*
 * Support publication of artifacts versioned by topic branch.
 * CI builds supply `-P BRANCH_NAME=<TOPIC>` to gradle at build time.
 * If <TOPIC> starts with 'SPR-', change version
 *     from BUILD-SNAPSHOT => <TOPIC>-SNAPSHOT
 *     e.g. 3.2.1.BUILD-SNAPSHOT => 3.2.1.SPR-1234-SNAPSHOT
 */
def qualifyVersionIfNecessary(version) {
	if (rootProject.hasProperty("BRANCH_NAME")) {
		def qualifier = rootProject.getProperty("BRANCH_NAME")
		if (qualifier.startsWith("SPR-")) {
			return version.replace('BUILD', qualifier)
		}
	}
	return version
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308

3、新增module,并测试构建是否成功

步骤:【File】->【New】->【Module…】 在Spring中添加自己的module模块,同样选择gradle构建

在这里插入图片描述

在这里插入图片描述

新建user类

package com.zx;

public class User {
	private Integer id;
	private String name;

	public User(Integer id, String name) {
		this.id = id;
		this.name = name;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "User{" +
				"id=" + id +
				", name='" + name + '\'' +
				'}';
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

添加配置类

package com.zx;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
public class MyConfig {

	@Bean
	public User user() {
		return new User(1, "zx");
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

添加测试类

package com.zx;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Demo {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);

		User user = (User) context.getBean("user");
		System.out.println(user);
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

运行结果
在这里插入图片描述

到这里,恭喜你,spring源码编译成功,子模块构建成功,可以开始搞你想搞的事情了

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/305783
推荐阅读
相关标签
  

闽ICP备14008679号