Check for duplicates in merged properties
@@ -35,25 +35,26 @@ final Path sourceDir = Paths.get(properties['group-tests.source.dir'])
final String[] concatRelPaths = properties['group-tests.concat.rel.paths'].split('[\\s,]+')
final Path destinationModuleDir = Paths.get(properties['group-tests.dest.module.dir'])
-final Map<String, StringBuilder> mergedFiles = new HashMap<>()
-concatRelPaths.each {relPath -> mergedFiles.put(relPath, new StringBuilder())}
+final Map<String, ResourceConcatenator> mergedFiles = new HashMap<>()
+concatRelPaths.each {relPath -> mergedFiles.put(relPath, new ResourceConcatenator()) }
Files.list(sourceDir)
.filter { p -> Files.exists(p.resolve('pom.xml')) }
.sorted()
.forEach { p ->
- mergedFiles.each { relPath, sb -> (Files.exists(p.resolve(relPath))) ? (sb.append(p.resolve(relPath).getText('UTF-8') + '\n')) : sb }
+ mergedFiles.each { relPath, cat ->
+ cat.append(p.resolve(relPath))
+ }
copyResources(p.resolve('src/main/java'), destinationModuleDir.resolve('target/src/main/java'))
copyResources(p.resolve('src/test/java'), destinationModuleDir.resolve('target/src/test/java'))
copyResources(p.resolve('src/main/resources'), destinationModuleDir.resolve('target/classes'))
copyResources(p.resolve('src/test/resources'), destinationModuleDir.resolve('target/test-classes'))
}
-mergedFiles.each { relPath, sb ->
+mergedFiles.each { relPath, cat ->
String destRelPath = relPath.replace('src/main/resources/', 'target/classes/').replace('src/test/resources/', 'target/test-classes/')
Path destPath = destinationModuleDir.resolve(destRelPath)
- Files.createDirectories(destPath.getParent())
- Files.write(destPath, sb.toString().getBytes('UTF-8'))
+ cat.store(destPath)
}
static void copyResources(Path source, Path dest) {
@@ -63,3 +64,41 @@ static void copyResources(Path source, Path dest) {
}
}
}
+
+class ResourceConcatenator {
+ private final StringBuilder sb = new StringBuilder()
+ private final Properties props = new Properties()
+ private final List<Path> visitedPaths = new ArrayList<>()
+
+ public ResourceConcatenator append(Path path) {
+ if (Files.exists(path)) {
+ if (path.getFileName().toString().endsWith(".properties")) {
+ Properties newProps = new Properties()
+ path.withInputStream { is ->
+ newProps.load(is)
+ }
+ newProps.each { key, val ->
+ if (props.containsKey(key) && !props.get(key).equals(val)) {
+ throw new IllegalStateException("Conflicting property value "+ key +" = "+ val +": found in "+ path + " conflicting with some of " + visitedPaths);
+ }
+ }
+ props.putAll(newProps)
+ } else {
+ sb.append(path.getText('UTF-8') + '\n')
+ }
+ visitedPaths.add(path)
+ }
+ return this
+ }
+
+ public void store(Path path) {
+ Files.createDirectories(path.getParent())
+ if (path.getFileName().toString().endsWith(".properties")) {
+ path.withOutputStream { out ->
+ props.store(out, "")
+ }
+ } else {
+ Files.write(path, sb.toString().getBytes('UTF-8'))
+ }
+ }
+}
