上卷已经了解如何构建项目。
发布Jar包到本地Maven仓库
由于 spring framework 项目为 library,为了保证我们构建的项目能够作为库正常运作,需要将项目引入安装到本地 maven 仓库,进而作为 application 的依赖使用。
将项目发布到本地的 Maven 仓库可以参考官方文档:
./gradlew publishToMavenLocal -x api -x javadoc -x dokkaHtmlMultiModule -x asciidoctor -x asciidoctorPdf -x distZip
由于官方文档未指明版本,因此对于不同版本需要对 -x 跳过的任务进行修改,甚至对项目作出调整。
修正 -x <task>
为了得知某个聚合任务(Aggregate Task)具体包含哪些任务,我们可以通过 -m 以禁用所有的任务的方式去执行任务,这样所有的任务就会被列举出来,如下:
./gradlew -m publishToMavenLocal
当我们得知所有任务,就可以选择性的跳过一些任务。
当然我更推荐直接运行官方所给的命令,通过报错信息反向纠正命令(比如他会提示任务不存在、任务失败之类的有用的信息)。
以 v6.0.4 为参考,我运行官方命令会遇到下面错误:
./gradlew publishToMavenLocal -x api -x javadoc -x dokkaHtmlMultiModule -x asciidoctor -x asciidoctorPdf -x distZip
# 错误信息(部分)
> Task :framework-docs:distZip FAILED
跳过项目失败的任务:
./gradlew publishToMavenLocal -x api -x javadoc -x dokkaHtmlMultiModule -x asciidoctor -x asciidoctorPdf -x distZip -x :framework-docs:distZip
# 错误信息(部分)
> Task :framework-docs:publishMavenJavaPublicationToMavenLocal FAILED
截至为此可以发现 framework-docs 项目存在很多错误,连非副产品(文档类)的 distZip 任务都无法顺利完成,从项目名称可以判断该项目也是文档类项目,猜测对库运行影响不大,因此可以从 settings.gradle 注释掉 include "framework-docs" 。
继续执行官方提供的命令:
./gradlew publishToMavenLocal -x api -x javadoc -x dokkaHtmlMultiModule -x asciidoctor -x asciidoctorPdf -x distZip
# 错误信息(部分)
FAILURE: Build failed with an exception.
* What went wrong:
Task 'api' not found in root project 'spring' and its subprojects.
错误提示 task 'api' not found ,因此我们可以删除 -x api。
以这种方式不断运行查找错误,最终我得到下面能成功执行任务的命令:
./gradlew publishToMavenLocal -x javadoc -x dokkaHtmlMultiModule
发布到本地 Maven 仓库的包可以在本地 Maven 仓库位置中找到,比如我的可以通过如下的目录 /home/ming/.m2/repository/org/springframework/spring-core/6.0.4 看到我发布的 spring-core 包。
为什么我能确定是我发布的包呢?因为通过包的更新时间就是我发布到本地 Maven 仓库的时间。
将本地仓库包引入项目
首先我们创建一个项目:
mkdir spring-cookbook && cd spring-cookbook
gradle init
# 初始化交互
Welcome to Gradle 7.5.1!
Here are the highlights of this release:
- Support for Java 18
- Support for building with Groovy 4
- Much more responsive continuous builds
- Improved diagnostics for dependency resolution
For more details see https://docs.gradle.org/7.5.1/release-notes.html
Starting a Gradle Daemon (subsequent builds will be faster)
Select type of project to generate:
1: basic
2: application
3: library
4: Gradle plugin
Enter selection (default: basic) [1..4] 1
Select build script DSL:
1: Groovy
2: Kotlin
Enter selection (default: Groovy) [1..2] 1
Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no]
Project name (default: spring-cookbook):
> Task :init
Get more help with your project: Learn more about Gradle by exploring our samples at https://docs.gradle.org/7.5.1/samples
BUILD SUCCESSFUL in 24s
现在我们要在这片荒地中搭建我们的靶场,首先创建个子项目:
通过 settings.gradle 添加子项目:
rootProject.name = 'spring-cookbook'
# 子项目目录名
include 'getting-started'
创建 getting-started 目录并创建 build.gradle 文件:
mkdir getting-started && cd getting-start
touch build.gradle
和正常写 build.gradle 一样,这次我们使用本地仓库而非远程仓库:
plugins {
id 'java'
}
group 'com.hihusky.gettingstarted'
version '0.0.1'
repositories {
mavenLocal()
}
dependencies {
implementation "org.springframework:spring-context:6.0.4"
}
然后创建:
src/main/java/com/hihusky/gettingstarted/Application.javasrc/main/java/com/hihusky/gettingstarted/Greeting.javasrc/main/resources/spring-context.xml
Application.java
package com.hihusky.gettingstarted;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
Greeting greeting;
try (ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml")) {
greeting = context.getBean(Greeting.class);
}
System.out.println(greeting);
}
}
Greeting.java
package com.hihusky.gettingstarted;
public class Greeting {
private String message;
public Greeting() {
}
public Greeting(String message) {
this.message = message;
}
public String getMessage() {
return this.message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public String toString() {
return "Greeting{" +
"message='" + message + '\'' +
'}';
}
}
spring-context.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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="greeting" class="com.hihusky.gettingstarted.Greeting">
<property name="message" value="Hello, Bean!"/>
</bean>
</beans>
完成为通过 xml 方式向 spring context 注册 bean ,运行 main 看看输出结果:
Greeting{message='Hello, Bean!'}
总结和展望
鉴于刚刚起步会粘贴比较全面的代码,随着后面不断的深入以及对源码的讲解,我将不会再粘贴大量代码。
截至到目前,整个项目从无到有再到使用已经完成啦!剩下的就是集中力量看源码了!