Created
January 15, 2021 22:35
-
-
Save wind57/003412be5ab10a3d34450d19a77a4f35 to your computer and use it in GitHub Desktop.
gradle common bom
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.nio.file.Files | |
import java.nio.file.Path | |
import java.nio.file.Paths | |
import java.util.function.Function | |
import java.util.function.Predicate | |
import java.util.function.Supplier | |
import java.util.stream.Collectors | |
import javax.xml.parsers.DocumentBuilder | |
import javax.xml.parsers.DocumentBuilderFactory | |
import org.w3c.dom.Document | |
import org.w3c.dom.Element | |
import org.w3c.dom.NodeList | |
plugins { | |
id 'java-platform' | |
id 'maven-publish' | |
} | |
group 'com.test' | |
version '1.1.0' | |
javaPlatform { | |
allowDependencies() | |
} | |
dependencies { | |
api platform('org.springframework.boot:spring-boot-dependencies:2.3.4.RELEASE') | |
constraints { | |
runtime "com.google.guava:guava:28.1-jre" | |
runtime "com.google.code.gson:gson:2.8.6" | |
} | |
} | |
publishing { | |
publications { | |
maven(org.gradle.api.publish.maven.MavenPublication) { | |
from components.javaPlatform | |
pom.withXml { | |
asNode().children().last() + { | |
resolveStrategy = Closure.DELEGATE_FIRST | |
build { | |
pluginManagement { | |
plugins { | |
project.configurations.each { conf -> | |
conf.dependencies.each { dep -> | |
String pomPath = pomPath(dep); | |
List<Plugin> plugins = pluginsFromManagement(pomPath); | |
for(Plugin p : plugins){ | |
plugin { | |
groupId "${p.groupId}" | |
artifactId "${p.artifactId}" | |
version "${p.version}" | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
repositories { | |
maven { | |
...... | |
} | |
} | |
} | |
/** | |
* get the Path for the POM file | |
*/ | |
static String pomPath(Object dep) { | |
String groupId = dep.group; | |
String artifactId = dep.name; | |
String version = dep.version; | |
List<String> tokens = new ArrayList<>(); | |
tokens.add(System.getProperty("user.home")); | |
tokens.add(".m2/repository"); | |
tokens.addAll( | |
Arrays.stream(groupId.split("\\.")).collect(Collectors.toList()) | |
); | |
tokens.add(artifactId); | |
tokens.add(version); | |
String filePath = tokens.stream().collect(Collectors.joining("/")) + "/"; | |
String fileName = | |
Files.list(Paths.get(filePath)) | |
.map(new Function<Path, String>() { | |
@Override | |
String apply(Path path) { | |
return path.toFile().getName(); | |
} | |
}) | |
.filter(new Predicate<String>() { | |
@Override | |
boolean test(String s) { | |
return s.endsWith(".pom"); | |
} | |
}) | |
.findFirst() | |
.orElseThrow(new Supplier<Exception>() { | |
@Override | |
Exception get() { | |
new RuntimeException("weird"); | |
} | |
}); | |
String pomPath = filePath + "/" + fileName; | |
println("==== Read pomFile from : " + pomPath); | |
return pomPath; | |
} | |
static List<Plugin> pluginsFromManagement(String pomPath){ | |
File fXmlFile = new File(pomPath); | |
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); | |
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); | |
Document doc = dBuilder.parse(fXmlFile); | |
NodeList plugins = doc.getElementsByTagName("plugins"); | |
Element pluginsList = (Element) plugins.item(0); | |
NodeList pluginList = pluginsList.getElementsByTagName("plugin"); | |
List<Plugin> result = new ArrayList<>(); | |
for (int i = 0; i < pluginList.getLength(); i++) { | |
Element movieElement = (Element) pluginList.item(i); | |
String groupId = movieElement.getElementsByTagName("groupId").item(0).getFirstChild().getTextContent(); | |
String artifactId = movieElement.getElementsByTagName("artifactId").item(0).getFirstChild().getTextContent(); | |
String version = movieElement.getElementsByTagName("version").item(0).getFirstChild().getTextContent(); | |
if(version.startsWith("\${")){ | |
String innerVersion = version.replaceFirst("\\\$", "").replaceFirst("\\{", "").replaceFirst("}", ""); | |
version = doc.getElementsByTagName(innerVersion).item(0).getTextContent(); | |
} | |
result.add(new Plugin(groupId, artifactId, version)); | |
} | |
return result; | |
} | |
/** | |
* represents a single plugin artifact from a pluginManagement entry | |
*/ | |
class Plugin { | |
String groupId; | |
String artifactId; | |
String version; | |
Plugin(String groupId, String artifactId, String version) { | |
this.groupId = groupId | |
this.artifactId = artifactId | |
this.version = version | |
} | |
public String toString(){ | |
return "groupdId = " + groupId + " artifactId = " + artifactId + " version = " + version + "\n"; | |
} | |
} | |
--- | |
This will generate a `pom.xml` in the form: | |
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | |
<!-- This module was also published with a richer model, Gradle metadata, --> | |
<!-- which should be used instead. Do not delete the following line which --> | |
<!-- is to indicate to Gradle or any Gradle module metadata file consumer --> | |
<!-- that they should prefer consuming it instead. --> | |
<!-- do_not_remove: published-with-gradle-metadata --> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.test</groupId> | |
<artifactId>bom</artifactId> | |
<version>1.1.0</version> | |
<packaging>pom</packaging> | |
<dependencyManagement> | |
<dependencies> | |
<dependency> | |
<groupId>com.google.guava</groupId> | |
<artifactId>guava</artifactId> | |
<version>28.1-jre</version> | |
</dependency> | |
<dependency> | |
<groupId>com.google.code.gson</groupId> | |
<artifactId>gson</artifactId> | |
<version>2.8.6</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-dependencies</artifactId> | |
<version>2.3.4.RELEASE</version> | |
<type>pom</type> | |
<scope>import</scope> | |
</dependency> | |
</dependencies> | |
</dependencyManagement> | |
<build> | |
<pluginManagement> | |
<plugins> | |
<plugin> | |
<groupId>org.codehaus.mojo</groupId> | |
<artifactId>build-helper-maven-plugin</artifactId> | |
<version>3.1.0</version> | |
</plugin> | |
<!-- A LOT of other plugins that come from Spring's pluginManagement --> | |
</plugins> | |
</pluginManagement> | |
</build> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What this does is create the
dependencyManagement
with whatever I specify plus it adds all the plugins that are defined inapi platform
underpluginManagement
, thus creating my common bom; that I will use as a parent in each dependency.