gradle 依赖 搭建

来源:2-2 代码结构介绍

SpringSecurity

2018-04-30

  老师您的项目我是用gradle 搭建的虽然项目能用但是还是遇到了不少问题,在视频中我详细了看了代码结构的介绍根据代码结构的介绍去构建项目项目是可以运行了但是并不知道这样的依赖关系是否有按照您的要求来,希望您过目一下。

父模块 security 的 gradle.build

//给Gradle本身添加的依赖 给插件使用 插件添加到子项目中所以子项目中不需要引插件
buildscript {
   ext {
       kotlinVersion = '1.2.40'
       springBootVersion = '2.0.1.RELEASE'
   }
   repositories {
       jcenter()
       mavenCentral()
   }
   dependencies {
       classpath 'io.spring.gradle:dependency-management-plugin:1.0.0.RELEASE'
       classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
       classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
       classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
   }
}


allprojects {
   group = 'com.fara.security'
   version = '0.0.1-SNAPSHOT'
}

//所有的模块都要基于 kotlin
subprojects {


   apply plugin: 'kotlin'
   apply plugin: 'kotlin-spring'
   apply plugin: 'org.springframework.boot'
   apply plugin: 'io.spring.dependency-management'

   sourceCompatibility = 1.8

   compileKotlin {
       kotlinOptions {
           freeCompilerArgs = ["-Xjsr305=strict"]
           jvmTarget = "1.8"
       }
   }
   compileTestKotlin {
       kotlinOptions {
           freeCompilerArgs = ["-Xjsr305=strict"]
           jvmTarget = "1.8"
       }
   }

   repositories {
       mavenCentral()
       maven { url "https://repo.spring.io/milestone" }
   }



   repositories {
       mavenCentral()
       maven { url "https://repo.spring.io/milestone" }
   }


   ext {
       springIoVersion = 'Cairo-RELEASE'
       springCloudVersion = 'Finchley.RC1'
   }

   dependencies {
       compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
       compile("org.jetbrains.kotlin:kotlin-reflect")
       compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.22.5'
   }

   //spring 版本控制器
   dependencyManagement {
       imports {
           mavenBom "io.spring.platform:platform-bom:${springIoVersion}"
           mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
       }
   }
   kotlin {
       experimental {
           coroutines "enable"
       }
   }

}

这是父模块的代码,当中添加了spring boot 与 dependency-management 插件,根据视频所说的控制子模块的依赖。

setting.gradle

rootProject.name = 'fara-security'

include 'fara-security-core'
include 'fara-security-browser'
include 'fara-security-app'
include 'fara-security-demo'

视频中有提到 demo 依赖 browser 依赖core 

我在 core 中的 gradle 的依赖为

dependencies {

   compile('org.springframework.cloud:spring-cloud-starter-oauth2')
   compile('org.springframework.boot:spring-boot-starter-data-redis')
   compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2')
   compile('mysql:mysql-connector-java')



   
   // https://mvnrepository.com/artifact/org.springframework.social/spring-social-config
   compile group: 'org.springframework.social', name: 'spring-social-config'
   // https://mvnrepository.com/artifact/org.grails.plugins/spring-social-core
   compile group: 'org.springframework.social', name: 'spring-social-core'
   compile group: 'org.springframework.social', name: 'spring-social-security'
   compile group: 'org.springframework.social', name: 'spring-social-web'



   // https://mvnrepository.com/artifact/commons-lang/commons-lang
   compile group: 'commons-lang', name: 'commons-lang'
   // https://mvnrepository.com/artifact/commons-collections/commons-collections
   compile group: 'commons-collections', name: 'commons-collections'
   // https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils
   compile group: 'commons-beanutils', name: 'commons-beanutils'
   // https://mvnrepository.com/artifact/commons-io/commons-io
   compile group: 'commons-io', name: 'commons-io'



   // https://mvnrepository.com/artifact/io.springfox/springfox-swagger2
   compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.8.0'
   // https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui
   compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.8.0'
   // https://mvnrepository.com/artifact/com.github.tomakehurst/wiremock
   compile group: 'com.github.tomakehurst', name: 'wiremock', version: '2.17.0'

}

在browser gradle.build的依赖为

dependencies {

   //依赖core 模块
   compile project(':fara-security-core')
   // https://mvnrepository.com/artifact/org.springframework.session/spring-session
   compile group: 'org.springframework.session', name: 'spring-session', version: '1.3.2.RELEASE'
}

session 如果不添加版本的话 无法被实现

Demo的gradle.build 的文件为

dependencies{
   //依赖core 模块
   compile project(':fara-security-browser')
   testCompile('org.springframework.boot:spring-boot-starter-test')
   // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-aop
   compile group: 'org.springframework.boot', name: 'spring-boot-starter-aop'
}

app gradle.build

dependencies{
   //依赖core 模块
   compile project(':fara-security-browser')
}

由于之前没有用过gradle部署多模块 所以这段依赖配置,所以我在依赖这部分就埋下了很多坑,我还是kotlin的新手所,想让所有的模块都可以用Kotlin的语言所以kotlin的插件就写在了父模块中,这里我也纠结了很久不知道这样做是否合理,我很希望老师能帮我指点一下。这当中的错误。麻烦老师您了。

写回答

4回答

JoJo

2018-05-03

额...不好意思,没用过gradle和kotlin,自己多琢磨吧,为你点赞。

0
1
SpringSecurity
好哒 我自己琢磨
2018-05-04
共1条回复

代码有毒

2018-08-08

我也用gradle

1
1
春生啊
一起交流吧
2018-08-14
共1条回复

春生啊

2018-08-06

我是用gradle,新手也入坑啦

1
0

春生啊

2018-09-05

我是用gradle来构建项目的。

项目的依赖如下

buildscript {
    ext {
        springBootVersion = '1.5.4.RELEASE'
//        springBootVersion = '2.0.4.RELEASE'
        projectJdk = '1.8'
        projectGroup = 'com.security'
        projectVersion = '0.0.1'
        projectName = 'securitydemo'
        mavenUrl = "http://192.168.0.14/nexus/repository/maven-public/";
    }
    repositories {
        mavenLocal()
        maven { url = mavenUrl }
        mavenCentral();

        maven { url "http://repo.spring.io/milestone" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'war'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'maven-publish'

group = projectGroup
version = projectVersion
sourceCompatibility = projectJdk
repositories {
    mavenLocal()
    maven { url = mavenUrl }
    maven { url "http://repo.spring.io/milestone" }
}
dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-aop')
    compile("org.apache.tomcat.embed:tomcat-embed-jasper")
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    testCompile('org.springframework.boot:spring-boot-starter-test')

    // 导入spring security的依赖
    compile('org.springframework.boot:spring-boot-starter-security')
    compile group: 'org.thymeleaf.extras', name: 'thymeleaf-extras-springsecurity4', version: '3.0.2.RELEASE'
    compile 'org.springframework.security:spring-security-web:5.0.7.RELEASE'

    compile('com.alibaba:druid-spring-boot-starter:1.1.10')
    compile("org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2")
    compile("mysql:mysql-connector-java:8.0.11")
    compile("com.github.pagehelper:pagehelper-spring-boot-starter:1.2.5")
    compile("org.springframework.boot:spring-boot-devtools")
    compile("tk.mybatis:mapper-spring-boot-starter:2.0.3")
    compile("org.mybatis.generator:mybatis-generator-core:1.3.7")
    compile group: 'commons-lang', name: 'commons-lang', version: '2.6'
    compile group: 'commons-beanutils', name: 'commons-beanutils', version: '1.9.3'
    compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
    compile 'com.github.joschi.jackson:jackson-datatype-threetenbp:2.6.4'
    compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.6'
    compile('org.springframework.social:spring-social-web:1.1.0.RELEASE')

    /*security */
    compile 'org.springframework.social:spring-social-config:1.1.6.RELEASE'
    compile 'org.springframework.social:spring-social-security:1.1.6.RELEASE'
    compile 'org.springframework.social:spring-social-core:1.1.6.RELEASE'
    compile 'org.springframework.social:spring-social-web:1.1.6.RELEASE'


}


// 以下内容为我用gradle自定义的任务。  可以忽略
def creatDir = {
    path ->
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdirs();
        }
}

task makeJavaDir() {
    def paths = ['src/main/java', 'src/main/resources', 'src/test/java', 'src/test/resources'];
// 在创建任务之前 先遍历 路径
    doFirst {
        paths.forEach(creatDir);
    }
}

// src main resources 项目目录结构
task makeResourcesDir() {
    dependsOn 'makeJavaDir';
    def paths = ['src/main/resources/templates', 'src/main/resources/static', 'src/main/resources/mappers', 'src/main/resources/views'];
    doLast {
        paths.forEach(creatDir)
    }
}

// web 项目目录结构 包含了java项目目录结构
task makeWebDir() {
    dependsOn 'makeJavaDir';
    def paths = ['src/main/webapp', 'src/test/webapp'];
    doLast {
        paths.forEach(creatDir)
    }
}


0
0

Spring Security技术栈开发企业级认证与授权

Spring Security技术栈,REST风格开发常见接口,独立开发认证授权模块保证REST服务安全

2662 学习 · 1561 问题

查看课程