在子项目中继承gradle插件

时间:2017-05-05 17:26:20

标签: gradle flyway subproject

我想避免冗余,因此我得到了一个"分享"包含如下内容的项目:

************************************************************

Request received for GET '/api/foo?id=foo':

Request(GET //localhost:8089/api/foo?id=foo)@b4603eb

servletPath:/api/foo
pathInfo:null
headers:
Authorization: Basic cnVudXNlcjpwYXNzd29yZA==
Cookie: JSESSIONID=node0fe3b0i44a5sbpohi6jq6dkkw0.node0
Cache-Control: no-cache
Accept: */*
User-Agent: PostmanRuntime/3.0.11-hotfix.2
Connection: keep-alive
Postman-Token: 99a23213-6cf8-4686-9886-7f9c2de13c6f
Host: localhost:8089
Accept-Encoding: gzip, deflate


Security filter chain: [
  WebAsyncManagerIntegrationFilter
  SecurityContextPersistenceFilter
  HeaderWriterFilter
  LogoutFilter
  UsernamePasswordAuthenticationFilter
  DefaultLoginPageGeneratingFilter
  BasicAuthenticationFilter
  RequestCacheAwareFilter
  SecurityContextHolderAwareRequestFilter
  AnonymousAuthenticationFilter
  SessionManagementFilter
  ExceptionTranslationFilter
  FilterSecurityInterceptor
]


************************************************************
.
.
.
2017-05-08 11:31:27.817 DEBUG 5812 --- [p1731685294-106] o.s.s.w.u.matcher.AntPathRequestMatcher  : Checking match of request : '/api/foo'; against 'GET'
2017-05-08 11:31:27.817 DEBUG 5812 --- [p1731685294-106] o.s.s.w.u.matcher.AntPathRequestMatcher  : Checking match of request : '/api/foo'; against '/api/foo'
2017-05-08 11:31:27.817 DEBUG 5812 --- [p1731685294-106] o.s.s.w.a.i.FilterSecurityInterceptor    : Secure object: FilterInvocation: URL: /api/foo?id=foo; Attributes: [hasRole('ROLE_WRITE')]
2017-05-08 11:31:27.818 DEBUG 5812 --- [p1731685294-106] o.s.s.w.a.i.FilterSecurityInterceptor    : Previously Authenticated: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@a38eb23d: Principal: org.springframework.security.core.userdetails.User@5c7268d6: Username: readuser; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_READ,ROLE_USER; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null; Granted Authorities: ROLE_READ, ROLE_USER
2017-05-08 11:31:27.818 DEBUG 5812 --- [p1731685294-106] o.s.s.access.vote.AffirmativeBased       : Voter: org.springframework.security.web.access.expression.WebExpressionVoter@7b20c046, returned: -1
2017-05-08 11:31:27.819 DEBUG 5812 --- [p1731685294-106] o.s.s.w.a.ExceptionTranslationFilter     : Access is denied (user is not anonymous); delegating to AccessDeniedHandler

org.springframework.security.access.AccessDeniedException: Access is denied
.
.
.
2017-05-08 11:31:27.823 DEBUG 5812 --- [p1731685294-106] s.s.w.c.SecurityContextPersistenceFilter : SecurityContextHolder now cleared, as request processing completed

然后我还有我的常规项目从我的共享项目继承编译依赖项,如下所示:

plugins {
    id "org.flywaydb.flyway" version "4.2.0"
}

repositories {
    mavenCentral()
    jcenter()
}

apply plugin: "java"

dependencies {
    compile "commons-io:commons-io:2.4"

    // ...
}

有什么方法可以让我的常规项目继承插件块或实际的插件?

1 个答案:

答案 0 :(得分:5)

继承。在我看来,您可以通过从根项目配置子项目来实现您尝试做的事情。基本上在您的根build.gradle(将配置您的根项目的脚本)中,您可以写:

subprojects {
  // configuration
}

你可以摆脱你的共享项目,并在root项目中build.gradle

plugins {
  id "org.flywaydb.flyway" version "4.2.0" apply false
}

subprojects {    
    repositories {
        mavenCentral()
        jcenter()
    }

    apply plugin: "java"
    apply plugin: "org.flywaydb.flyway"

    dependencies {
        compile "commons-io:commons-io:2.4"

        // ...
    }
}

这样,所有子项目都将使用相同的闭包进行配置 - 这相当于将subprojects块中的所有内容复制粘贴到您的各个子项目' build.gradle个文件。优于初始解决方案的优势还在于能够应用插件,配置扩展,以及通常可以执行的所有操作。

作为旁注,您不需要jcenter()阻止中的mavenCentralrepositories - jCentermavenCentral的超集,是首选的