SpringBoot + Gradle 多模块项目 构建、运行、打包

工具: IDEA

一、在IDEA中使用工具创建SpringBoot + Gradle的父工程

       new -> project -> Spring Initializr  -> type选项选中Gradle Project(其他视情况填写)

       父工程 : demo-springboot-gradle

二、在父工程下新建两个模块 web、dao

       右键单击父工程 new -> module -> Spring Initializr -> type选项选中Gradle Project(其他视情况填写)

       创建module的最后一步要注意,子模块最好在父工程的目录下,避免不必要的麻烦

       

三、删除dao子模块下的所有.java文件 、 删除父工程src文件

四、在dao子模块下创建两个类,用于测试Student 和 StudentService

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;
    }
}
@Component
public class StudentService {

    public Student query(){
        Student student = new Student();
        student.setId(1);
        student.setName("嘿");
        return student;
    }

}
五、在web子模块下创建一个接口类StudentController
@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/hello")
    public Student hello(){
        return studentService.query();
    }
}

六、dao子模块的bulid.gradle

group 'com.zeke'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

七、web子模块的bulid.gradle

buildscript {
	ext {
		springBootVersion = '1.5.3.RELEASE'
	}
	repositories {
		mavenCentral()
	}
	dependencies {
		classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
	}
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
	mavenCentral()
}

configurations {
	providedRuntime
}

dependencies {
	compile('org.springframework.boot:spring-boot-starter-thymeleaf')
	compile('org.springframework.boot:spring-boot-starter-web')
//	providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
	testCompile('org.springframework.boot:spring-boot-starter-test')
}

八、父工程bulid.gradle

allprojects {
	apply plugin: 'java'
	group 'com.zeke'
	version = '1.0'
	sourceCompatibility = 1.8
	targetCompatibility = 1.8
}


subprojects {
	ext {
//        slf4jVersion = '1.7.7'
		springVersion = '4.3.8.RELEASE'
		hibernateVersion = '4.3.1.Final'
	}
	[compileJava, compileTestJava, javadoc]*.options*.encoding = 'UTF-8'
	repositories {
		mavenCentral()
	}
	configurations {
		//compile.exclude module: 'commons-logging'
		all*.exclude module: 'commons-logging'
	}
	dependencies {
		compile(
				'redis.clients:jedis:2.9.0',
				'org.slf4j:slf4j-api:1.7.25',
				'ch.qos.logback:logback-core:1.1.11',
				'ch.qos.logback:logback-classic:1.1.11',

//                "org.slf4j:jcl-over-slf4j:${slf4jVersion}",
//                "org.slf4j:slf4j-log4j12:${slf4jVersion}",
				"org.springframework:spring-context:$springVersion",
//                "org.springframework:spring-orm:$springVersion",
//                "org.springframework:spring-tx:$springVersion",
//                "org.springframework.data:spring-data-jpa:1.5.2.RELEASE",
//                "org.hibernate:hibernate-entitymanager:$hibernateVersion",
//                "c3p0:c3p0:0.9.1.2",
				"mysql:mysql-connector-java:5.1.35",
//                "commons-fileupload:commons-fileupload:1.3.1",
				"com.fasterxml.jackson.core:jackson-databind:2.3.1"
		)
		testCompile(
				"org.springframework:spring-test:$springVersion",
				"junit:junit:4.12"
		)
	}
}

project(':dao') {

}
project(':web') {
	apply plugin: "war"
	dependencies {
		compile project(":dao")
		compile(
				'org.springframework.boot:spring-boot-starter-thymeleaf',
				'org.springframework.boot:spring-boot-starter-web'
		)
		testCompile(
				'org.springframework.boot:spring-boot-starter-test'
		)
//        providedCompile(
//                "javax.servlet:javax.servlet-api:3.1.0",
//                "javax.servlet.jsp:jsp-api:2.2.1-b03",
//                "javax.servlet.jsp.jstl:javax.servlet.jsp.jstl-api:1.2.1"
//        )
	}
	processResources{
		/* 从'$projectDir/src/main/java'目录下复制文件到'WEB-INF/classes'目录下覆盖原有同名文件*/
		from("$projectDir/src/main/java")
	}

	/*自定义任务用于将当前子项目的java类打成jar包,此jar包不包含resources下的文件*/
	def jarArchiveName="${project.name}-${version}.jar"
	task jarWithoutResources(type: Jar) {
		from sourceSets.main.output.classesDir
		archiveName jarArchiveName
	}

	/*重写war任务:*/
	war {
		dependsOn jarWithoutResources
		/* classpath排除sourceSets.main.output.classesDir目录,加入jarWithoutResources打出来的jar包 */
		classpath = classpath.minus(files(sourceSets.main.output.classesDir)).plus(files("$buildDir/$libsDirName/$jarArchiveName"))
	}
	/*打印编译运行类路径*/
	task jarPath << {
		println configurations.compile.asPath
	}
}

/*从子项目拷贝War任务生成的压缩包到根项目的build/explodedDist目录*/
task explodedDist(type: Copy) {
	into "$buildDir/explodedDist"
	subprojects {
		from tasks.withType(War)
	}
}

九、启动web子模块的application启动类,访问localhost:8080/hello

十、打包:

            在父工程目录下输入命令 gradle build

       取出 web子模块下 build -> libs -> web-1.0.jar 

        java -jar 执行即可访问

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值