spring源码调试——环境搭建(第一次搭建)

spring源码搭建环境:idea2019.2.4   jdk-8.0_221  gradle 4.9  spring版本  :Spring-framework-5.1.x

 

idea官网:https://www.jetbrains.com/idea/download/other.html

gradle官网 :https://services.gradle.org/distributions/

 

jdk和gradle 环境变量配置:

从github 拉取Spring-framework源码。

   git clone速度太慢:

解决方法:

    使用码云     传送门

     1、登录完成后点击页面右上角的 “+” 号,选择新建项目

 

   2、在新页面中最下面 选择“导入已有项目”

   3、复制需要导入的Github项目地址

   

4、填入地址之后点击“创建”

   5、等待Gitee拉取完成,这个过程的时间长短因拉取项目大小而异。

    6、拉取完成后就可以在gitee上git clone 项目了,速度贼快(注意:不要用master上的分支,我选择的是 5.1.x版本的分支

 

不过我是下载的zip文件。解压下载的zip

 

再使用idea  导入解压文件夹:

 

接下来就是静静等待了,这个过程应该会要点时间的,因为这是第二次所以比较快

 

再修改docs.gradle文件

 

1,2注释,3修改(在Linux上编写的所以要改成Windows的)

docs.gradle文件改为:

/*
 * Copyright 2002-2019 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       https://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

task api(type: Javadoc) {
    group = "Documentation"
    description = "Generates aggregated Javadoc API documentation."
    title = "${rootProject.description} ${version} API"

    dependsOn {
        subprojects.collect {
            it.tasks.getByName("jar")
        }
    }
    options.encoding = "UTF-8"
    options.memberLevel = org.gradle.external.javadoc.JavadocMemberLevel.PROTECTED
    options.author = true
    options.header = rootProject.description
    options.use = true
    options.overview = "src/docs/api/overview.html"
    options.stylesheetFile = file("src/docs/api/stylesheet.css")
    options.splitIndex = true
    options.links(project.ext.javadocLinks)
    options.addStringOption('Xdoclint:none', '-quiet')

    source subprojects.collect { project ->
        project.sourceSets.main.allJava
    }

    maxMemory = "1024m"
    destinationDir = new File(buildDir, "api")

    doFirst {
        classpath = files(
                // ensure the javadoc process can resolve types compiled from .aj sources
                project(":spring-aspects").sourceSets.main.output
        )
        classpath += files(subprojects.collect { it.sourceSets.main.compileClasspath })
    }
}

/*dokka {
    dependsOn {
        tasks.getByName("api")
    }
    doFirst {
        classpath = subprojects.collect { project -> project.jar.outputs.files.getFiles() }.flatten()
        classpath += files(subprojects.collect { it.sourceSets.main.compileClasspath })

    }
    moduleName = "spring-framework"
    outputFormat = "html"
    outputDirectory = "$buildDir/docs/kdoc"

    sourceDirs = files(subprojects.collect { project ->
        def kotlinDirs = project.sourceSets.main.kotlin.srcDirs.collect()
        kotlinDirs -= project.sourceSets.main.java.srcDirs
    })
    externalDocumentationLink {
        url = new URL("https://docs.spring.io/spring-framework/docs/$version/javadoc-api/")
        packageListUrl = new File(buildDir, "api/package-list").toURI().toURL()
    }
    externalDocumentationLink {
        url = new URL("https://projectreactor.io/docs/core/release/api/")
    }
    externalDocumentationLink {
        url = new URL("https://www.reactive-streams.org/reactive-streams-1.0.1-javadoc/")
    }
}*/

/*asciidoctor {
    sources {
        include '*.adoc'
    }
    resources {
        from(sourceDir) {
            include 'images/*', 'stylesheets/*', 'tocbot-3.0.2/*'
        }
    }
    logDocuments = true
    backends = ["html5"]
    // only ouput PDF documentation for non-SNAPSHOT builds
    if(!project.getVersion().toString().contains("BUILD-SNAPSHOT")) {
        backends += "pdf"
    }
    options doctype: 'book', eruby: 'erubis'
    attributes  'icons': 'font',
            'idprefix': '',
            'idseparator': '-',
            docinfo: '',
            revnumber: project.version,
            sectanchors: '',
            sectnums: '',
            'source-highlighter': 'coderay@', // TODO switch to 'rouge' once supported by the html5 backend
            stylesdir: 'stylesheets/',
            stylesheet: 'main.css',
            'spring-version': project.version

}*/

task docsZip(type: Zip, dependsOn: ['api', 'asciidoctor', 'dokka']) {
    group = "Distribution"
    baseName = "spring-framework"
    classifier = "docs"
    description = "Builds -${classifier} archive containing api and reference " +
            "for deployment at https://docs.spring.io/spring-framework/docs."

    from("src/dist") {
        include "changelog.txt"
    }

    from (api) {
        into "javadoc-api"
    }

    from ("$asciidoctor.outputDir/html5") {
        into "spring-framework-reference"
    }

    from ("$asciidoctor.outputDir/pdf") {
        into "spring-framework-reference/pdf"
    }

    from (dokka) {
        into "kdoc-api"
    }
}

//task schemaZip(type: Zip) {
//    group = "Distribution"
//    baseName = "spring-framework"
//    classifier = "schema"
//    description = "Builds -${classifier} archive containing all " +
//            "XSDs for deployment at https://springframework.org/schema."
//    duplicatesStrategy 'exclude'
//    moduleProjects.each { subproject ->
//        def Properties schemas = new Properties();
//
//        subproject.sourceSets.main.resources.find {
//            (it.path.endsWith("META-INF/spring.schemas") || it.path.endsWith("META-INF\\spring.schemas"))
//        }?.withInputStream { schemas.load(it) }
//
//        for (def key : schemas.keySet()) {
//            def shortName = key.replaceAll(/http.*schema.(.*).spring-.*/, '$1')
//            assert shortName != key
//            File xsdFile = subproject.sourceSets.main.resources.find {
//                (it.path.endsWith(schemas.get(key)) || it.path.endsWith(schemas.get(key).replaceAll('\\/','\\\\')))
//            }
//            assert xsdFile != null
//            into (shortName) {
//                from xsdFile.path
//            }
//        }
//    }
//}
task schemaZip(type: Zip) {
    group = "Distribution"
    baseName = "spring-framework"
    classifier = "schema"
    description = "Builds -${classifier} archive containing all " +
            "XSDs for deployment at https://springframework.org/schema."
    duplicatesStrategy 'exclude'
    moduleProjects.each { subproject ->
        def Properties schemas = new Properties();

        subproject.sourceSets.main.resources.find {
            //此处修改为\\
            it.path.endsWith("META-INF\\spring.schemas")
        }?.withInputStream { schemas.load(it) }

        for (def key : schemas.keySet()) {
            def shortName = key.replaceAll(/http.*schema.(.*).spring-.*/, '$1')
            assert shortName != key
            File xsdFile = subproject.sourceSets.main.resources.find {
                //此处将Linux路径符号替换为Windows路径符号
                it.path.endsWith(schemas.get(key).replaceAll('\\/','\\\\'))
            }
            assert xsdFile != null
            into (shortName) {
                from xsdFile.path
            }
        }
    }
}

task distZip(type: Zip, dependsOn: [docsZip, schemaZip]) {

    group = "Distribution"
    baseName = "spring-framework"
    classifier = "dist"
    description = "Builds -${classifier} archive, containing all jars and docs, " +
            "suitable for community download page."

    ext.baseDir = "${baseName}-${project.version}";

    from("src/docs/dist") {
        include "readme.txt"
        include "license.txt"
        include "notice.txt"
        into "${baseDir}"
        expand(copyright: new Date().format("yyyy"), version: project.version)
    }

    from(zipTree(docsZip.archivePath)) {
        into "${baseDir}/docs"
    }

    from(zipTree(schemaZip.archivePath)) {
        into "${baseDir}/schema"
    }

    moduleProjects.each { subproject ->
        into ("${baseDir}/libs") {
            from subproject.jar
            if (subproject.tasks.findByPath("sourcesJar")) {
                from subproject.sourcesJar
            }
            if (subproject.tasks.findByPath("javadocJar")) {
                from subproject.javadocJar
            }
        }
    }
}

distZip.mustRunAfter subprojects.test

// Create a distribution that contains all dependencies (required and optional).
// Not published by default; only for use when building from source.
task depsZip(type: Zip, dependsOn: distZip) { zipTask ->
    group = "Distribution"
    baseName = "spring-framework"
    classifier = "dist-with-deps"
    description = "Builds -${classifier} archive, containing everything " +
            "in the -${distZip.classifier} archive plus all runtime dependencies."

    from zipTree(distZip.archivePath)

    gradle.taskGraph.whenReady { taskGraph ->
        if (taskGraph.hasTask(":${zipTask.name}")) {
            def projectNames = rootProject.subprojects*.name
            def artifacts = new HashSet()
            subprojects.each { subproject ->
                (subproject.configurations.runtime.resolvedConfiguration.resolvedArtifacts +
                        subproject.configurations.optional.resolvedConfiguration.resolvedArtifacts).each { artifact ->
                    def dependency = artifact.moduleVersion.id
                    if (!projectNames.contains(dependency.name)) {
                        artifacts << artifact.file
                    }
                }
            }

            zipTask.from(artifacts) {
                into "${distZip.baseDir}/deps"
            }
        }
    }
}
 

 

再分别构建:spring-oxm和spring-core项目,这两个过程要花费时间很长:我大概每个花了半个小时。(由于我是第二次构建所以很快)

1.spring-oxm

2.spring-core

 

至此,spring源码环境构建完成。接下来新建一个模块来测试是否构建成功。

1.右键新建一个模块,直接点击下一步

2.选择组织:(也就是模块名:spring-logmark-demo)然后点击下一步,接着直接点 finish

 

 

可以查看是否创建成功:

可以把自己模块的  .gradle文件名称改成 模块名,也可以不改(我改了)

目录结构:

student类:

package com.logmark.demo.po;

/**
 * @program: spring
 * @description:
 * @author: heliangbin
 * @create: 2020-02-18  17:10
 **/

public class Student {
   private Integer id;
   private String 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 "Student{" +
            "id=" + id +
            ", name='" + name + '\'' +
            '}';
   }
}

 

StudentTest类:
package com.logmark.dmeo;

import com.logmark.demo.po.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @program: spring
 * @description:
 * @author: heliangbin
 * @create: 2020-02-18  17:11
 **/

public class StudentTest {
   @Test
   public void test() {
      ApplicationContext ac = new ClassPathXmlApplicationContext("com/logmark/demo/spring-student.xml");
      Student student = (Student) ac.getBean("student");
      System.out.println(student);
   }
}

 

spring-student.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

   <bean id="student" class="com.logmark.demo.po.Student">
      <property name="id" value="1"/>
      <property name="name" value="fff"/>
   </bean>
</beans>

 

spring-logmark-demo.gradle文件内容:

plugins {
    id 'java'
}

group 'org.springframework'
version '5.1.14.BUILD-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
//添加这一个就够了
    compile(project(":spring-context"))
//不需要这个
//    testCompile group: 'junit', name: 'junit', version: '4.12'
}

 

 

测试成功:

 

至此,spring源码构建就成功勒。

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值