From 266a51a3a8e664bda20fc18f8a77a627ea851c04 Mon Sep 17 00:00:00 2001 From: Jarred Li Date: Wed, 25 Apr 2012 13:08:23 +0800 Subject: [PATCH] update Gradle to generate pom --- build.gradle | 2 + maven.gradle | 204 ++++++++++----------------------------------------- 2 files changed, 40 insertions(+), 166 deletions(-) diff --git a/build.gradle b/build.gradle index c301443c..6f8891b0 100644 --- a/build.gradle +++ b/build.gradle @@ -2,6 +2,7 @@ apply plugin: 'base' apply plugin: "java" apply plugin: 'eclipse' apply plugin: 'idea' +apply from: 'maven.gradle' allprojects { group = 'org.springframework.shell' @@ -45,4 +46,5 @@ dependencies { task wrapper(type: Wrapper) { } + defaultTasks 'build' diff --git a/maven.gradle b/maven.gradle index f9e8750c..8ce50bad 100644 --- a/maven.gradle +++ b/maven.gradle @@ -1,176 +1,48 @@ apply plugin: 'maven' -// Create a source jar for uploading -task sourceJar(type: Jar, dependsOn: jar) { - classifier = 'sources' - from sourceSets.main.allSource -} +ext.optionalDeps = [] +ext.providedDeps = [] -// Create a javadoc jar for uploading -task javadocJar(type: Jar, dependsOn: javadoc) { - classifier = 'javadoc' - from javadoc.destinationDir -} - -artifacts { - archives sourceJar - archives javadocJar -} - -// Configuration for SpringSource s3 maven deployer -configurations { - deployerJars -} -dependencies { - deployerJars "org.springframework.build.aws:org.springframework.build.aws.maven:3.0.0.RELEASE" -} - -task generatePom { - group = 'Build' - description = 'Generates a Maven POM file suitable for use in building the project' - - generatedPomFileName = "pom.xml" - - // ensure changes in the classpath trigger regeneration of poms - inputs.files(project.sourceSets.main.compileClasspath) - - // ensure version changes in gradle.properties trigger regeneration of poms - inputs.files(new File(project.rootProject.rootDir, Project.GRADLE_PROPERTIES)) - - // enable partial cleaning with `gradle cleanGeneratePom` - outputs.files(generatedPomFileName) - - doLast() { - // customize the pom creation process - p = pom { - project { - name = project.description - properties { - setProperty('project.build.sourceEncoding', 'UTF8') - } - build { - plugins { - plugin { - groupId = 'org.apache.maven.plugins' - artifactId = 'maven-compiler-plugin' - configuration { - source = '1.6' - target = '1.6' - } - } - } - } - } - } - - // customizing the artifact id is a special case that must be configured - // after the pom is fully configured, otherwise it'll be overwritten - p.whenConfigured { pom -> pom.artifactId = project.name } - - customizePom(p) - - // write the pom.xml file out to the filesystem - p.writeTo(generatedPomFileName) - } -} - - -// Remove the archive configuration from the runtime configuration, so that anything added to archives -// (such as the source jar) is no longer included in the runtime classpath -configurations.default.extendsFrom = [configurations.runtime] as Set -// Add the main jar into the default configuration -artifacts { 'default' jar } - -gradle.taskGraph.whenReady {graph -> - if (graph.hasTask(uploadArchives)) { - // check properties defined and fail early - s3AccessKey - s3SecretAccessKey - } -} - -def deployer = null - -uploadArchives { - description = "Maven deploy of archives artifacts to SpringSource Maven repos" // url appended below - group = "Distribution" - // Maven deployment - def releaseRepositoryUrl = "file://${project.properties.mavenSyncRepoDir}" - def milestoneRepositoryUrl = 's3://maven.springframework.org/milestone' - def snapshotRepositoryUrl = 's3://maven.springframework.org/snapshot' - - // add a configuration with a classpath that includes our s3 maven deployer - configurations { deployerJars } - dependencies { - deployerJars "org.springframework.build.aws:org.springframework.build.aws.maven:3.0.0.RELEASE" - } - - deployer = repositories.mavenDeployer { - configuration = configurations.deployerJars - // releaseBuild - if (releaseBuild) { - logger.info("Deploying to local Maven repo " + releaseRepositoryUrl) - // "mavenSyncRepoDir" should be set in properties - repository(url: releaseRepositoryUrl) - } else { - s3credentials = [userName: project.properties.s3AccessKey, passphrase: project.properties.s3SecretAccessKey] - repository(url: milestoneRepositoryUrl) { - authentication(s3credentials) - } - snapshotRepository(url: snapshotRepositoryUrl) { - authentication(s3credentials) - } - } - } - - customizePom(deployer.pom) -} +ext.optional = { optionalDeps << it } +ext.provided = { providedDeps << it } install { - customizePom(repositories.mavenInstaller.pom) + repositories.mavenInstaller { + customizePom(pom, project) + } } -def customizePom(pom) { - def optionalDeps = ['log4j','jsr250-api'] +def customizePom(pom, gradleProject) { + pom.whenConfigured { generatedPom -> + // respect 'optional' and 'provided' dependencies + gradleProject.optionalDeps.each { dep -> + generatedPom.dependencies.find { it.artifactId == dep.name }?.optional = true + } + gradleProject.providedDeps.each { dep -> + generatedPom.dependencies.find { it.artifactId == dep.name }?.scope = 'provided' + } - //pom.scopeMappings.addMapping(10, configurations.provided, 'provided') - pom.whenConfigured { p -> - // Remove test scope dependencies from published poms - //p.dependencies = p.dependencies.findAll {it.scope != 'test'} + // eliminate test-scoped dependencies (no need in maven central poms) + generatedPom.dependencies.removeAll { dep -> + dep.scope == 'test' + } - // Flag optional deps - def opDeps = configurations.testRuntime.allDependencies.findAll { gradleDep -> - gradleDep.asDynamicObject.hasProperty('optional') && gradleDep.optional - } - - p.dependencies.findAll { dep -> - optionalDeps.contains(dep.artifactId) || - dep.groupId.startsWith('org.slf4j') || - opDeps.any { op -> - (dep.groupId == op.group && dep.artifactId == op.name) - } - }*.optional = true - - p.groupId = "org.springframework.data" - } - - pom.project { - licenses { - license { - name 'The Apache Software License, Version 2.0' - url 'http://www.apache.org/licenses/LICENSE-2.0.txt' - distribution 'repo' - } - } - - // similar to Spring's configuration - dependencies { - dependency { - artifactId = groupId = 'commons-logging' - scope = 'compile' - optional = 'true' - version = '1.1.1' - } - } - } + // add all items necessary for maven central publication + generatedPom.project { + name = gradleProject.description + description = gradleProject.description + url = 'https://github.com/SpringSource/spring-shell' + organization { + name = 'SpringSource' + url = 'http://springsource.org/spring-shell' + } + licenses { + license { + name 'The Apache Software License, Version 2.0' + url 'http://www.apache.org/licenses/LICENSE-2.0.txt' + distribution 'repo' + } + } + } + } } \ No newline at end of file