先确定spring源码中使用的gradle的版本,然后再去官方下载安装
IDEA中配置gradle
执行gradle build命令,编译源代码
创建module的时候, 名称要以spring-为前缀,
plugins {
id 'war'
id 'java'
}
dependencies {
api(project(":spring-web"))
api(project(":spring-webmvc"))
compileOnly("io.projectreactor.tools:blockhound")
optional(project(":spring-aop"))
optional(project(":spring-context"))
optional(project(":spring-oxm"))
optional("javax.servlet:javax.servlet-api") // Servlet 4 for mapping type
optional("javax.servlet.jsp:javax.servlet.jsp-api")
optional("javax.el:javax.el-api")
optional("javax.faces:javax.faces-api")
optional("javax.json.bind:javax.json.bind-api")
optional("javax.mail:javax.mail-api")
optional("javax.validation:validation-api")
optional("javax.xml.bind:jaxb-api")
optional("javax.xml.ws:jaxws-api")
optional("org.glassfish.main:javax.jws")
optional("io.reactivex.rxjava3:rxjava")
optional("io.netty:netty-buffer")
optional("io.netty:netty-handler")
optional("io.netty:netty-codec-http") // Until Netty4ClientHttpRequest is removed
optional("io.netty:netty-transport") // Until Netty4ClientHttpRequest is removed
optional("io.projectreactor.netty:reactor-netty-http")
optional("io.undertow:undertow-core")
optional("org.eclipse.jetty:jetty-server") {
exclude group: "javax.servlet", module: "javax.servlet-api"
}
optional("org.eclipse.jetty:jetty-servlet") {
exclude group: "javax.servlet", module: "javax.servlet-api"
}
optional("org.eclipse.jetty:jetty-reactive-httpclient")
optional('org.apache.httpcomponents.client5:httpclient5')
optional('org.apache.httpcomponents.core5:httpcore5-reactive')
optional("com.squareup.okhttp3:okhttp")
optional("org.apache.httpcomponents:httpclient")
optional("org.apache.httpcomponents:httpasyncclient")
optional("commons-fileupload:commons-fileupload")
optional("org.synchronoss.cloud:nio-multipart-parser")
optional("com.fasterxml.woodstox:woodstox-core")
optional("com.fasterxml:aalto-xml")
optional("com.fasterxml.jackson.core:jackson-databind")
optional("com.fasterxml.jackson.dataformat:jackson-dataformat-xml")
optional("com.fasterxml.jackson.dataformat:jackson-dataformat-smile")
optional("com.fasterxml.jackson.dataformat:jackson-dataformat-cbor")
optional("com.google.code.gson:gson")
optional("com.google.protobuf:protobuf-java-util")
optional("com.googlecode.protobuf-java-format:protobuf-java-format")
optional("com.rometools:rome")
optional("com.caucho:hessian")
optional("org.codehaus.groovy:groovy")
optional("org.jetbrains.kotlin:kotlin-reflect")
optional("org.jetbrains.kotlin:kotlin-stdlib")
optional("org.jetbrains.kotlinx:kotlinx-serialization-json")
testImplementation(testFixtures(project(":spring-beans")))
testImplementation(testFixtures(project(":spring-context")))
testImplementation(testFixtures(project(":spring-core")))
testImplementation("io.projectreactor:reactor-test")
testImplementation("org.apache.taglibs:taglibs-standard-jstlel")
testImplementation("com.fasterxml.jackson.datatype:jackson-datatype-jdk8")
testImplementation("com.fasterxml.jackson.datatype:jackson-datatype-joda")
testImplementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")
testImplementation("com.fasterxml.jackson.module:jackson-module-kotlin")
testImplementation("org.apache.tomcat:tomcat-util")
testImplementation("org.apache.tomcat.embed:tomcat-embed-core")
testImplementation("org.eclipse.jetty:jetty-server")
testImplementation("org.eclipse.jetty:jetty-servlet")
testImplementation("com.squareup.okhttp3:mockwebserver")
testImplementation("org.jetbrains.kotlin:kotlin-reflect")
testImplementation("org.skyscreamer:jsonassert")
testImplementation("org.xmlunit:xmlunit-assertj")
testImplementation("org.xmlunit:xmlunit-matchers")
testImplementation("io.projectreactor.tools:blockhound")
testRuntimeOnly("com.sun.mail:javax.mail")
testRuntimeOnly("com.sun.xml.bind:jaxb-core")
testRuntimeOnly("com.sun.xml.bind:jaxb-impl")
testRuntimeOnly("javax.json:javax.json-api")
testRuntimeOnly("org.apache.johnzon:johnzon-jsonb")
testFixturesApi("javax.servlet:javax.servlet-api")
testFixturesApi("org.junit.jupiter:junit-jupiter-api")
testFixturesApi("org.junit.jupiter:junit-jupiter-params")
testFixturesImplementation("io.projectreactor:reactor-test")
testFixturesImplementation("org.apache.taglibs:taglibs-standard-jstlel")
testFixturesImplementation("org.assertj:assertj-core")
testFixturesImplementation("org.bouncycastle:bcpkix-jdk18on") {
because("needed by Netty's SelfSignedCertificate on JDK 15+")
}
}
test {
useJUnitPlatform()
}
有几点需要特别注意
配置文件的名称必须和模块的名称一致。
并且必须以spring-开头,比如spring-mvctest.gradle。只有这样才能使用spring源码中公共的gradle配置文件,即spring-module.gradle文件。
dependencies中除了引入spring源码中需要的模块,还必须引入其他的配置,比如org.jetbrains.kotlin:kotlin-reflect,如果不引入则使用tomcat启动的时候,run模式是可以正常启动的,但是debug模式的时候会报类不存在。
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring-root.xmlparam-value>
context-param>
<servlet>
<servlet-name>mvc-testservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring-mvc.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>mvc-testservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
web-app>
如果创建的是web项目的话,artifact会自动创建好的
配置artifact
tomcat启动会启不来,在安装的tomcat路径中打开bin/catalina.bat 文件,把下面这串代码注释掉,即可解决