当前位置:   article > 正文

SpringBoot + Gradle 多模块项目 构建、运行、打包_spring-boot-gradle-plugin访问jsp

spring-boot-gradle-plugin访问jsp

工具: 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

  1. public class Student {
  2. private Integer id;
  3. private String name;
  4. public Integer getId() {
  5. return id;
  6. }
  7. public void setId(Integer id) {
  8. this.id = id;
  9. }
  10. public String getName() {
  11. return name;
  12. }
  13. public void setName(String name) {
  14. this.name = name;
  15. }
  16. }
  1. @Component
  2. public class StudentService {
  3. public Student query(){
  4. Student student = new Student();
  5. student.setId(1);
  6. student.setName("嘿");
  7. return student;
  8. }
  9. }
五、在web子模块下创建一个接口类StudentController
  1. @RestController
  2. public class StudentController {
  3. @Autowired
  4. private StudentService studentService;
  5. @GetMapping("/hello")
  6. public Student hello(){
  7. return studentService.query();
  8. }
  9. }

六、dao子模块的bulid.gradle

  1. group 'com.zeke'
  2. version '1.0-SNAPSHOT'
  3. apply plugin: 'java'
  4. sourceCompatibility = 1.8
  5. repositories {
  6. mavenCentral()
  7. }
  8. dependencies {
  9. testCompile group: 'junit', name: 'junit', version: '4.12'
  10. }

七、web子模块的bulid.gradle

  1. buildscript {
  2. ext {
  3. springBootVersion = '1.5.3.RELEASE'
  4. }
  5. repositories {
  6. mavenCentral()
  7. }
  8. dependencies {
  9. classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
  10. }
  11. }
  12. apply plugin: 'java'
  13. apply plugin: 'idea'
  14. apply plugin: 'org.springframework.boot'
  15. apply plugin: 'war'
  16. version = '0.0.1-SNAPSHOT'
  17. sourceCompatibility = 1.8
  18. repositories {
  19. mavenCentral()
  20. }
  21. configurations {
  22. providedRuntime
  23. }
  24. dependencies {
  25. compile('org.springframework.boot:spring-boot-starter-thymeleaf')
  26. compile('org.springframework.boot:spring-boot-starter-web')
  27. // providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
  28. testCompile('org.springframework.boot:spring-boot-starter-test')
  29. }

八、父工程bulid.gradle

  1. allprojects {
  2. apply plugin: 'java'
  3. group 'com.zeke'
  4. version = '1.0'
  5. sourceCompatibility = 1.8
  6. targetCompatibility = 1.8
  7. }
  8. subprojects {
  9. ext {
  10. // slf4jVersion = '1.7.7'
  11. springVersion = '4.3.8.RELEASE'
  12. hibernateVersion = '4.3.1.Final'
  13. }
  14. [compileJava, compileTestJava, javadoc]*.options*.encoding = 'UTF-8'
  15. repositories {
  16. mavenCentral()
  17. }
  18. configurations {
  19. //compile.exclude module: 'commons-logging'
  20. all*.exclude module: 'commons-logging'
  21. }
  22. dependencies {
  23. compile(
  24. 'redis.clients:jedis:2.9.0',
  25. 'org.slf4j:slf4j-api:1.7.25',
  26. 'ch.qos.logback:logback-core:1.1.11',
  27. 'ch.qos.logback:logback-classic:1.1.11',
  28. // "org.slf4j:jcl-over-slf4j:${slf4jVersion}",
  29. // "org.slf4j:slf4j-log4j12:${slf4jVersion}",
  30. "org.springframework:spring-context:$springVersion",
  31. // "org.springframework:spring-orm:$springVersion",
  32. // "org.springframework:spring-tx:$springVersion",
  33. // "org.springframework.data:spring-data-jpa:1.5.2.RELEASE",
  34. // "org.hibernate:hibernate-entitymanager:$hibernateVersion",
  35. // "c3p0:c3p0:0.9.1.2",
  36. "mysql:mysql-connector-java:5.1.35",
  37. // "commons-fileupload:commons-fileupload:1.3.1",
  38. "com.fasterxml.jackson.core:jackson-databind:2.3.1"
  39. )
  40. testCompile(
  41. "org.springframework:spring-test:$springVersion",
  42. "junit:junit:4.12"
  43. )
  44. }
  45. }
  46. project(':dao') {
  47. }
  48. project(':web') {
  49. apply plugin: "war"
  50. dependencies {
  51. compile project(":dao")
  52. compile(
  53. 'org.springframework.boot:spring-boot-starter-thymeleaf',
  54. 'org.springframework.boot:spring-boot-starter-web'
  55. )
  56. testCompile(
  57. 'org.springframework.boot:spring-boot-starter-test'
  58. )
  59. // providedCompile(
  60. // "javax.servlet:javax.servlet-api:3.1.0",
  61. // "javax.servlet.jsp:jsp-api:2.2.1-b03",
  62. // "javax.servlet.jsp.jstl:javax.servlet.jsp.jstl-api:1.2.1"
  63. // )
  64. }
  65. processResources{
  66. /* 从'$projectDir/src/main/java'目录下复制文件到'WEB-INF/classes'目录下覆盖原有同名文件*/
  67. from("$projectDir/src/main/java")
  68. }
  69. /*自定义任务用于将当前子项目的java类打成jar包,此jar包不包含resources下的文件*/
  70. def jarArchiveName="${project.name}-${version}.jar"
  71. task jarWithoutResources(type: Jar) {
  72. from sourceSets.main.output.classesDir
  73. archiveName jarArchiveName
  74. }
  75. /*重写war任务:*/
  76. war {
  77. dependsOn jarWithoutResources
  78. /* classpath排除sourceSets.main.output.classesDir目录,加入jarWithoutResources打出来的jar包 */
  79. classpath = classpath.minus(files(sourceSets.main.output.classesDir)).plus(files("$buildDir/$libsDirName/$jarArchiveName"))
  80. }
  81. /*打印编译运行类路径*/
  82. task jarPath << {
  83. println configurations.compile.asPath
  84. }
  85. }
  86. /*从子项目拷贝War任务生成的压缩包到根项目的build/explodedDist目录*/
  87. task explodedDist(type: Copy) {
  88. into "$buildDir/explodedDist"
  89. subprojects {
  90. from tasks.withType(War)
  91. }
  92. }

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

十、打包:

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

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

        java -jar 执行即可访问

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

闽ICP备14008679号