Rework split package detection code
Allow packages to be split across projects which will be merged into a single JAR file. Make split package detection a dependency of the 'check' task. This is idiomatic gradle as well as allowing the 'test' task (another dependency of 'check') to be executed without split packages being detected. Omit the project spring-instructment-tomcat from the check on the basis of SPR-10150. Issues: SPR-9990, SPR-10150 Conflicts: build.gradle
This commit is contained in:
parent
a27a3be76c
commit
2df08bdfbd
19
build.gradle
19
build.gradle
|
@ -8,14 +8,6 @@ buildscript {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
configure(rootProject) {
|
|
||||||
apply plugin: org.springframework.build.gradle.SplitPackageDetectorPlugin
|
|
||||||
|
|
||||||
diagnoseSplitPackages {
|
|
||||||
inputDir = project.projectDir
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
configure(allprojects) { project ->
|
configure(allprojects) { project ->
|
||||||
group = "org.springframework"
|
group = "org.springframework"
|
||||||
version = qualifyVersionIfNecessary(version)
|
version = qualifyVersionIfNecessary(version)
|
||||||
|
@ -761,13 +753,18 @@ configure(rootProject) {
|
||||||
apply plugin: "docbook-reference"
|
apply plugin: "docbook-reference"
|
||||||
apply plugin: "groovy"
|
apply plugin: "groovy"
|
||||||
apply from: "${gradleScriptDir}/jdiff.gradle"
|
apply from: "${gradleScriptDir}/jdiff.gradle"
|
||||||
|
apply plugin: org.springframework.build.gradle.SplitPackageDetectorPlugin
|
||||||
|
|
||||||
reference {
|
reference {
|
||||||
sourceDir = file("src/reference/docbook")
|
sourceDir = file("src/reference/docbook")
|
||||||
pdfFilename = "spring-framework-reference.pdf"
|
pdfFilename = "spring-framework-reference.pdf"
|
||||||
}
|
}
|
||||||
|
|
||||||
// don"t publish the default jar for the root project
|
diagnoseSplitPackages {
|
||||||
|
projectsToScan = project.subprojects - project(":spring-instrument-tomcat") // SPR-10150
|
||||||
|
}
|
||||||
|
|
||||||
|
// don"t publish the default jar for the root project
|
||||||
configurations.archives.artifacts.clear()
|
configurations.archives.artifacts.clear()
|
||||||
|
|
||||||
dependencies { // for integration tests
|
dependencies { // for integration tests
|
||||||
|
@ -791,6 +788,8 @@ configure(rootProject) {
|
||||||
testCompile("hsqldb:hsqldb:${hsqldbVersion}")
|
testCompile("hsqldb:hsqldb:${hsqldbVersion}")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
check.dependsOn diagnoseSplitPackages
|
||||||
|
|
||||||
task api(type: Javadoc) {
|
task api(type: Javadoc) {
|
||||||
group = "Documentation"
|
group = "Documentation"
|
||||||
description = "Generates aggregated Javadoc API documentation."
|
description = "Generates aggregated Javadoc API documentation."
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2012 the original author or authors.
|
* Copyright 2013 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -16,110 +16,116 @@
|
||||||
|
|
||||||
package org.springframework.build.gradle
|
package org.springframework.build.gradle
|
||||||
|
|
||||||
import java.io.File;
|
import org.gradle.api.DefaultTask
|
||||||
import java.util.HashSet;
|
import org.gradle.api.Plugin
|
||||||
import java.util.Map;
|
import org.gradle.api.Project
|
||||||
import java.util.Set;
|
import org.gradle.api.Task
|
||||||
|
|
||||||
import org.gradle.api.*
|
|
||||||
import org.gradle.api.artifacts.Configuration
|
import org.gradle.api.artifacts.Configuration
|
||||||
import org.gradle.api.artifacts.ProjectDependency;
|
import org.gradle.api.artifacts.ProjectDependency
|
||||||
import org.gradle.api.artifacts.maven.Conf2ScopeMapping
|
import org.gradle.api.artifacts.maven.Conf2ScopeMapping
|
||||||
import org.gradle.api.plugins.MavenPlugin
|
import org.gradle.api.plugins.MavenPlugin
|
||||||
import org.gradle.api.tasks.*
|
import org.gradle.api.tasks.Input
|
||||||
|
import org.gradle.api.tasks.TaskAction
|
||||||
import org.gradle.plugins.ide.eclipse.EclipsePlugin
|
import org.gradle.plugins.ide.eclipse.EclipsePlugin
|
||||||
import org.gradle.plugins.ide.eclipse.model.EclipseClasspath;
|
import org.gradle.plugins.ide.eclipse.model.EclipseClasspath
|
||||||
import org.gradle.plugins.ide.idea.IdeaPlugin
|
import org.gradle.plugins.ide.idea.IdeaPlugin
|
||||||
import org.gradle.api.invocation.*
|
|
||||||
|
|
||||||
|
|
||||||
class SplitPackageDetectorPlugin implements Plugin<Project> {
|
class SplitPackageDetectorPlugin implements Plugin<Project> {
|
||||||
public void apply(Project project) {
|
public void apply(Project project) {
|
||||||
Task diagnoseSplitPackages = project.tasks.add('diagnoseSplitPackages', SplitPackageDetectorTask.class)
|
Task diagnoseSplitPackages = project.tasks.add('diagnoseSplitPackages', SplitPackageDetectorTask.class)
|
||||||
diagnoseSplitPackages.setDescription('Detects split packages')
|
diagnoseSplitPackages.setDescription('Detects packages which will be split across JARs')
|
||||||
//project.tasks.findByName('build').dependsOn(diagnoseSplitPackages)
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class SplitPackageDetectorTask extends DefaultTask {
|
public class SplitPackageDetectorTask extends DefaultTask {
|
||||||
@InputDirectory
|
@Input
|
||||||
File inputDir
|
Set<Project> projectsToScan
|
||||||
|
|
||||||
@TaskAction
|
@TaskAction
|
||||||
public final void diagnoseSplitPackages() {
|
public final void diagnoseSplitPackages() {
|
||||||
def projects = project.subprojects.findAll { it.plugins.findPlugin(org.springframework.build.gradle.MergePlugin) }.findAll { it.merge.into }
|
def Map<Project, Project> mergeMap = [:]
|
||||||
projects.each { p ->
|
def projects = projectsToScan.findAll { it.plugins.findPlugin(org.springframework.build.gradle.MergePlugin) }.findAll { it.merge.into }
|
||||||
println ' > The project directory '+ p.projectDir + ' will merge into ' + p.merge.into.projectDir
|
projects.each { p ->
|
||||||
}
|
mergeMap.put(p, p.merge.into)
|
||||||
def splitFound = new org.springframework.build.gradle.SplitPackageDetector(inputDir.absolutePath, project.logger).diagnoseSplitPackages();
|
}
|
||||||
assert !splitFound
|
def splitFound = new org.springframework.build.gradle.SplitPackageDetector(projectsToScan, mergeMap, project.logger).diagnoseSplitPackages();
|
||||||
}
|
assert !splitFound // see error log messages for details of split packages
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class SplitPackageDetector {
|
class SplitPackageDetector {
|
||||||
|
|
||||||
private static final String HIDDEN_DIRECTORY_PREFIX = "."
|
private static final String HIDDEN_DIRECTORY_PREFIX = "."
|
||||||
|
|
||||||
private static final String JAVA_FILE_SUFFIX = ".java"
|
private static final String JAVA_FILE_SUFFIX = ".java"
|
||||||
|
|
||||||
private static final String SRC_MAIN_JAVA = "src" + File.separator + "main" + File.separator + "java"
|
private static final String SRC_MAIN_JAVA = "src" + File.separator + "main" + File.separator + "java"
|
||||||
|
|
||||||
private final Map<File, Set<String>> pkgMap = [:]
|
private static final String PACKAGE_SEPARATOR = "."
|
||||||
|
|
||||||
private final logger
|
private final Map<Project, Project> mergeMap
|
||||||
|
|
||||||
SplitPackageDetector(baseDir, logger) {
|
private final Map<Project, Set<String>> pkgMap = [:]
|
||||||
this.logger = logger
|
|
||||||
dirList(baseDir).each { File dir ->
|
|
||||||
def packages = getPackagesInDirectory(dir)
|
|
||||||
if (!packages.isEmpty()) {
|
|
||||||
pkgMap.put(dir, packages)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private File[] dirList(String dir) {
|
private final logger
|
||||||
dirList(new File(dir))
|
|
||||||
}
|
|
||||||
|
|
||||||
private File[] dirList(File dir) {
|
SplitPackageDetector(projectsToScan, mergeMap, logger) {
|
||||||
dir.listFiles({ file -> file.isDirectory() && !file.getName().startsWith(HIDDEN_DIRECTORY_PREFIX) } as FileFilter)
|
this.mergeMap = mergeMap
|
||||||
}
|
this.logger = logger
|
||||||
|
projectsToScan.each { Project p ->
|
||||||
|
def dir = p.projectDir
|
||||||
|
def packages = getPackagesInDirectory(dir)
|
||||||
|
if (!packages.isEmpty()) {
|
||||||
|
pkgMap.put(p, packages)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private Set<String> getPackagesInDirectory(File dir) {
|
private File[] dirList(String dir) {
|
||||||
def pkgs = new HashSet<String>()
|
dirList(new File(dir))
|
||||||
addPackagesInDirectory(pkgs, new File(dir, SRC_MAIN_JAVA), "")
|
}
|
||||||
return pkgs;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean diagnoseSplitPackages() {
|
private File[] dirList(File dir) {
|
||||||
def splitFound = false;
|
dir.listFiles({ file -> file.isDirectory() && !file.getName().startsWith(HIDDEN_DIRECTORY_PREFIX) } as FileFilter)
|
||||||
def dirs = pkgMap.keySet().toArray()
|
}
|
||||||
def numDirs = dirs.length
|
|
||||||
for (int i = 0; i < numDirs - 1; i++) {
|
|
||||||
for (int j = i + 1; j < numDirs - 1; j++) {
|
|
||||||
def di = dirs[i]
|
|
||||||
def pi = new HashSet(pkgMap.get(di))
|
|
||||||
def dj = dirs[j]
|
|
||||||
def pj = pkgMap.get(dj)
|
|
||||||
pi.retainAll(pj)
|
|
||||||
if (!pi.isEmpty()) {
|
|
||||||
logger.error("Packages $pi are split between directories '$di' and '$dj'")
|
|
||||||
splitFound = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return splitFound
|
|
||||||
}
|
|
||||||
|
|
||||||
private void addPackagesInDirectory(HashSet<String> packages, File dir, String pkg) {
|
private Set<String> getPackagesInDirectory(File dir) {
|
||||||
def scanDir = new File(dir, pkg)
|
def pkgs = new HashSet<String>()
|
||||||
def File[] javaFiles = scanDir.listFiles({ file -> !file.isDirectory() && file.getName().endsWith(JAVA_FILE_SUFFIX) } as FileFilter)
|
addPackagesInDirectory(pkgs, new File(dir, SRC_MAIN_JAVA), "")
|
||||||
if (javaFiles != null && javaFiles.length != 0) {
|
return pkgs;
|
||||||
packages.add(pkg)
|
}
|
||||||
}
|
|
||||||
dirList(scanDir).each { File subDir ->
|
boolean diagnoseSplitPackages() {
|
||||||
addPackagesInDirectory(packages, dir, pkg + File.separator + subDir.getName())
|
def splitFound = false;
|
||||||
}
|
def projs = pkgMap.keySet().toArray()
|
||||||
}
|
def numProjects = projs.length
|
||||||
|
for (int i = 0; i < numProjects - 1; i++) {
|
||||||
|
for (int j = i + 1; j < numProjects - 1; j++) {
|
||||||
|
def pi = projs[i]
|
||||||
|
def pkgi = new HashSet(pkgMap.get(pi))
|
||||||
|
def pj = projs[j]
|
||||||
|
def pkgj = pkgMap.get(pj)
|
||||||
|
pkgi.retainAll(pkgj)
|
||||||
|
if (!pkgi.isEmpty() && mergeMap.get(pi) != pj && mergeMap.get(pj) != pi) {
|
||||||
|
pkgi.each { pkg ->
|
||||||
|
def readablePkg = pkg.substring(1).replaceAll(File.separator, PACKAGE_SEPARATOR)
|
||||||
|
logger.error("Package '$readablePkg' is split between $pi and $pj")
|
||||||
|
}
|
||||||
|
splitFound = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return splitFound
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addPackagesInDirectory(HashSet<String> packages, File dir, String pkg) {
|
||||||
|
def scanDir = new File(dir, pkg)
|
||||||
|
def File[] javaFiles = scanDir.listFiles({ file -> !file.isDirectory() && file.getName().endsWith(JAVA_FILE_SUFFIX) } as FileFilter)
|
||||||
|
if (javaFiles != null && javaFiles.length != 0) {
|
||||||
|
packages.add(pkg)
|
||||||
|
}
|
||||||
|
dirList(scanDir).each { File subDir ->
|
||||||
|
addPackagesInDirectory(packages, dir, pkg + File.separator + subDir.getName())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue