如何创建Parent build.gradle(具有通用配置)并从模块build.gradle中调用它?

时间:2015-04-19 14:45:10

标签: gradle build.gradle

我有一个多模块项目,我正在gradle中编写。我想运行像findbugs,pmd,jococo和checkstyle这样的测试和一些对所有人来说相同的讨论,所以我想创建父gradle和模块构建。 gradle script。我不知道如何从模块gradle脚本调用父build.gradle。有人给我一个例子我怎样才能实现这一点,这样我就不需要在每个模块中编写相同的东西了。 / p>

apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'findbugs'
apply plugin: 'pmd'
apply plugin: 'checkstyle'
apply plugin: 'maven'

//-- set the group for publishing
group = 'com.trxxxx.abcd'

/**
 * Initializing GAVC settings
 */
def buildProperties = new Properties()
file("version.properties").withInputStream { 
    stream -> buildProperties.load(stream) 
} 
//add the jenkins build version to the version
def env = System.getenv()
if (env["BUILD_NUMBER"]) buildProperties.abcdBuildVersion += "_${env["BUILD_NUMBER"]}"
version = buildProperties.abcdBuildVersion
println "${version}"

//name is set in the settings.gradle file
group = "com.ngfh.abcd"
version = buildProperties.abcdBuildVersion
println "Building ${project.group}:${project.name}:${project.version}"

sourceCompatibility = 1.6
jar {
   manifest {
       attributes 'Implementation-Title': "${project.name}",
                   'Implementation-Version': "${project.version}",
                   'Implementation-Vendor-Id': "${project.group}"
                  }
}                
  repositories {

    maven {
      url "http://cmxxxxt.tsh.thon.com:80/ncds/cache"
    }
  }

dependencies {
    compile ([
    "com.eaio.uuid:uuid:3.2",
    "org.springframework:spring-context:2.5.6",
    "org.springframework:spring-beans:1.2.6" ,
    "org.springframework:spring-core:1.2.3",
    "org.springframework:spring-aop:3.0.6.RELEASE",
    "org.springframework:spring-expression:3.0.7.RELEASE",
    "org.projectlombok:lombok:1.12.2",
    "com.fasterxml.jackson.core:jackson-core:2.1.3",
    "com.fasterxml.jackson.core:jackson-annotations:2.2.3",
    "com.fasterxml.jackson.core:jackson-databind:2.2.3",
    "org.slf4j:slf4j-api:1.7.6",
    "org.apache.commons:commons-lang3:3.0",
    "junit:junit:4.+"
      ])
}

jacoco {
    toolVersion = "0.7.1.201405082137"
}
test {
    jacoco {
        append = false
        destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
        classDumpFile = file("$buildDir/jacoco/classpathdumps")
    }
}

jacocoTestReport {
    group = "Reporting"
    description = "Generate Jacoco coverage reports after running tests."
    executionData = fileTree(dir: 'build/jacoco', include: '**/*.exec')

    reports {
        xml{
            enabled true
            //Following value is a file
            destination "${buildDir}/reports/jacoco/xml/jacoco.xml"
        }
        csv.enabled false
     html{
       enabled true
       //Following value is a folder
       destination "${buildDir}/reports/jacoco/html"
     }
    }
}

findbugs {
        ignoreFailures = true
       //sourceSets = [sourceSets.main]
   }

pmd { 
         ruleSets = ["java-basic", "java-braces", "java-design"] 
         ignoreFailures = true
         //sourceSets = [sourceSets.main]
   }

checkstyle {
      configFile = new File(rootDir, "config/checkstyle.xml")
      ignoreFailures = true
      //sourceSets = [sourceSets.main]
   }

// adding test report to taskGraph
build.dependsOn jacocoTestReport

def pomFile = file("${buildDir}/libs/${archivesBaseName.toLowerCase()}-${version}.pom")
task newPom << {
  pom {
      project {
          groupId project.group
          artifactId project.name
          version project.version
          description = "Configuration Management Gradle Plugin"
      }
  }.writeTo(pomFile)
}
// adding pom generation to taskGraph
build.dependsOn newPom

//disabling the install task since we're not using maven for real
install.enabled = false

//for publishing to artifactory via jenkins
if(project.hasProperty('artifactoryPublish')) {
  artifactoryPublish {
    mavenDescriptor pomFile
  }
}

// ------ Publish the jar file to artifactory ------
artifacts {
  jar
  newPom
}

1 个答案:

答案 0 :(得分:2)

如果您的配置对于多项目构建的所有项目都是通用的,那么您只需将其放在allprojects { }块中即可。

allprojects {
    // place config that should be included in every project
}