Commit 0ad72d5b authored by Andy Wilkinson's avatar Andy Wilkinson

Remove reliance on mavenLocal from Gradle plugin’s tests

See gh-15471
parent 4edc3284
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
<gradle.executable>./gradlew</gradle.executable> <gradle.executable>./gradlew</gradle.executable>
<gradle.task>build</gradle.task> <gradle.task>build</gradle.task>
<skip.gradle.build>false</skip.gradle.build> <skip.gradle.build>false</skip.gradle.build>
<kotlin.version>1.2.20</kotlin.version>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
......
import io.spring.gradle.dependencymanagement.dsl.DependencyManagementExtension
// tag::configure-bom[] // tag::configure-bom[]
plugins { plugins {
java java
...@@ -12,16 +14,18 @@ dependencyManagement { ...@@ -12,16 +14,18 @@ dependencyManagement {
} }
// end::configure-bom[] // end::configure-bom[]
dependencyManagement { the<DependencyManagementExtension>().apply {
resolutionStrategy { resolutionStrategy {
eachDependency { eachDependency {
if (requested.group == "org.springframework.boot") { if (requested.group == "org.springframework.boot") {
useVersion("{version}") useVersion("TEST-SNAPSHOT")
} }
} }
} }
} }
repositories { repositories {
mavenLocal() maven {
url = uri("file:repository")
}
} }
...@@ -17,12 +17,12 @@ dependencyManagement { ...@@ -17,12 +17,12 @@ dependencyManagement {
resolutionStrategy { resolutionStrategy {
eachDependency { eachDependency {
if (it.requested.group == 'org.springframework.boot') { if (it.requested.group == 'org.springframework.boot') {
it.useVersion '{version}' it.useVersion 'TEST-SNAPSHOT'
} }
} }
} }
} }
repositories { repositories {
mavenLocal() maven { url 'file:repository' }
} }
...@@ -19,12 +19,14 @@ the<DependencyManagementExtension>().apply { ...@@ -19,12 +19,14 @@ the<DependencyManagementExtension>().apply {
resolutionStrategy { resolutionStrategy {
eachDependency { eachDependency {
if (requested.group == "org.springframework.boot") { if (requested.group == "org.springframework.boot") {
useVersion("{version}") useVersion("TEST-SNAPSHOT")
} }
} }
} }
} }
repositories { repositories {
mavenLocal() maven {
url = uri("file:repository")
}
} }
...@@ -8,7 +8,7 @@ dependencyManagement { ...@@ -8,7 +8,7 @@ dependencyManagement {
resolutionStrategy { resolutionStrategy {
eachDependency { eachDependency {
if (it.requested.group == 'org.springframework.boot') { if (it.requested.group == 'org.springframework.boot') {
it.useVersion '{version}' it.useVersion 'TEST-SNAPSHOT'
} }
} }
} }
...@@ -19,7 +19,7 @@ ext['slf4j.version'] = '1.7.20' ...@@ -19,7 +19,7 @@ ext['slf4j.version'] = '1.7.20'
// end::custom-version[] // end::custom-version[]
repositories { repositories {
mavenLocal() maven { url 'file:repository' }
} }
task slf4jVersion { task slf4jVersion {
......
...@@ -6,24 +6,26 @@ plugins { ...@@ -6,24 +6,26 @@ plugins {
apply(plugin = "io.spring.dependency-management") apply(plugin = "io.spring.dependency-management")
// tag::custom-version[]
extra["slf4j.version"] = "1.7.20"
// end::custom-version[]
repositories {
maven {
url = uri("file:repository")
}
}
the<DependencyManagementExtension>().apply { the<DependencyManagementExtension>().apply {
resolutionStrategy { resolutionStrategy {
eachDependency { eachDependency {
if (requested.group == "org.springframework.boot") { if (requested.group == "org.springframework.boot") {
useVersion("{version}") useVersion("TEST-SNAPSHOT")
} }
} }
} }
} }
// tag::custom-version[]
extra["slf4j.version"] = "1.7.20"
// end::custom-version[]
repositories {
mavenLocal()
}
task("slf4jVersion") { task("slf4jVersion") {
doLast { doLast {
println(project.the<DependencyManagementExtension>().managedVersions["org.slf4j:slf4j-api"]) println(project.the<DependencyManagementExtension>().managedVersions["org.slf4j:slf4j-api"])
......
...@@ -65,8 +65,8 @@ public class ManagingDependenciesDocumentationTests { ...@@ -65,8 +65,8 @@ public class ManagingDependenciesDocumentationTests {
Assume.assumeTrue(this.gradleBuild.getDsl() == Dsl.KOTLIN); Assume.assumeTrue(this.gradleBuild.getDsl() == Dsl.KOTLIN);
assertThat(this.gradleBuild.script( assertThat(this.gradleBuild.script(
"src/main/gradle/managing-dependencies/configure-bom-with-plugins") "src/main/gradle/managing-dependencies/configure-bom-with-plugins")
.build("dependencyManagement").getOutput()) .build("dependencyManagement").getOutput()).contains(
.contains("org.springframework.boot:spring-boot-starter "); "org.springframework.boot:spring-boot-starter TEST-SNAPSHOT");
} }
} }
...@@ -43,12 +43,10 @@ public class KotlinPluginActionIntegrationTests { ...@@ -43,12 +43,10 @@ public class KotlinPluginActionIntegrationTests {
} }
@Test @Test
public void kotlinVersionMatchesKotlinPluginVersion() { public void kotlinVersionPropertyIsSet() {
String output = this.gradleBuild.build("kotlinVersion", "dependencies", String output = this.gradleBuild.build("kotlinVersion", "dependencies",
"--configuration", "compileClasspath").getOutput(); "--configuration", "compileClasspath").getOutput();
assertThat(output).contains("Kotlin version: 1.2.20"); assertThat(output).containsPattern("Kotlin version: [0-9]\\.[0-9]\\.[0-9]+");
assertThat(output)
.containsPattern("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.2.20");
} }
@Test @Test
......
...@@ -24,33 +24,28 @@ import java.net.URL; ...@@ -24,33 +24,28 @@ import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.jar.JarFile;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import io.spring.gradle.dependencymanagement.DependencyManagementPlugin; import io.spring.gradle.dependencymanagement.DependencyManagementPlugin;
import io.spring.gradle.dependencymanagement.dsl.DependencyManagementExtension;
import org.apache.commons.compress.archivers.ArchiveEntry; import org.apache.commons.compress.archivers.ArchiveEntry;
import org.gradle.testkit.runner.BuildResult; import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.GradleRunner; import org.gradle.testkit.runner.GradleRunner;
import org.jetbrains.kotlin.cli.common.PropertiesKt; import org.jetbrains.kotlin.cli.common.PropertiesKt;
import org.jetbrains.kotlin.compilerRunner.KotlinCompilerRunner; import org.jetbrains.kotlin.compilerRunner.KotlinCompilerRunner;
import org.jetbrains.kotlin.gradle.model.KotlinProject;
import org.jetbrains.kotlin.gradle.plugin.KotlinGradleSubplugin; import org.jetbrains.kotlin.gradle.plugin.KotlinGradleSubplugin;
import org.jetbrains.kotlin.gradle.plugin.KotlinPlugin; import org.jetbrains.kotlin.gradle.plugin.KotlinPlugin;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import org.junit.rules.TestRule; import org.junit.rules.TestRule;
import org.junit.runner.Description; import org.junit.runner.Description;
import org.junit.runners.model.Statement; import org.junit.runners.model.Statement;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.springframework.asm.ClassVisitor; import org.springframework.asm.ClassVisitor;
import org.springframework.boot.loader.tools.LaunchScript; import org.springframework.boot.loader.tools.LaunchScript;
import org.springframework.util.FileCopyUtils; import org.springframework.util.FileCopyUtils;
import org.springframework.util.FileSystemUtils;
/** /**
* A {@link TestRule} for running a Gradle build using {@link GradleRunner}. * A {@link TestRule} for running a Gradle build using {@link GradleRunner}.
...@@ -146,6 +141,7 @@ public class GradleBuild implements TestRule { ...@@ -146,6 +141,7 @@ public class GradleBuild implements TestRule {
new File(pathOfJarContaining(PropertiesKt.class)), new File(pathOfJarContaining(PropertiesKt.class)),
new File(pathOfJarContaining(KotlinCompilerRunner.class)), new File(pathOfJarContaining(KotlinCompilerRunner.class)),
new File(pathOfJarContaining(KotlinPlugin.class)), new File(pathOfJarContaining(KotlinPlugin.class)),
new File(pathOfJarContaining(KotlinProject.class)),
new File(pathOfJarContaining(KotlinGradleSubplugin.class)), new File(pathOfJarContaining(KotlinGradleSubplugin.class)),
new File(pathOfJarContaining(ArchiveEntry.class))); new File(pathOfJarContaining(ArchiveEntry.class)));
} }
...@@ -185,6 +181,8 @@ public class GradleBuild implements TestRule { ...@@ -185,6 +181,8 @@ public class GradleBuild implements TestRule {
getDependencyManagementPluginVersion()); getDependencyManagementPluginVersion());
FileCopyUtils.copy(scriptContent, new FileWriter( FileCopyUtils.copy(scriptContent, new FileWriter(
new File(this.projectDir, "build" + this.dsl.getExtension()))); new File(this.projectDir, "build" + this.dsl.getExtension())));
FileSystemUtils.copyRecursively(new File("src/test/resources/repository"),
new File(this.projectDir, "repository"));
GradleRunner gradleRunner = GradleRunner.create().withProjectDir(this.projectDir) GradleRunner gradleRunner = GradleRunner.create().withProjectDir(this.projectDir)
.withPluginClasspath(pluginClasspath()); .withPluginClasspath(pluginClasspath());
if (this.dsl != Dsl.KOTLIN) { if (this.dsl != Dsl.KOTLIN) {
...@@ -222,44 +220,22 @@ public class GradleBuild implements TestRule { ...@@ -222,44 +220,22 @@ public class GradleBuild implements TestRule {
} }
private static String getBootVersion() { private static String getBootVersion() {
return evaluateExpression( return "TEST-SNAPSHOT";
"/*[local-name()='project']/*[local-name()='parent']/*[local-name()='version']"
+ "/text()");
} }
private static String getDependencyManagementPluginVersion() { private static String getDependencyManagementPluginVersion() {
try (FileReader pomReader = new FileReader(".flattened-pom.xml")) { try {
Document pom = DocumentBuilderFactory.newInstance().newDocumentBuilder() URL location = DependencyManagementExtension.class.getProtectionDomain()
.parse(new InputSource(pomReader)); .getCodeSource().getLocation();
NodeList dependencyElements = pom.getElementsByTagName("dependency"); try (JarFile jar = new JarFile(new File(location.toURI()))) {
for (int i = 0; i < dependencyElements.getLength(); i++) { return jar.getManifest().getMainAttributes()
Element dependency = (Element) dependencyElements.item(i); .getValue("Implementation-Version");
if (dependency.getElementsByTagName("artifactId").item(0).getTextContent()
.equals("dependency-management-plugin")) {
return dependency.getElementsByTagName("version").item(0)
.getTextContent();
} }
} }
throw new IllegalStateException(
"dependency management plugin version not found");
}
catch (Exception ex) { catch (Exception ex) {
throw new IllegalStateException( throw new IllegalStateException(
"Failed to find dependency management plugin version", ex); "Failed to find dependency management plugin version", ex);
} }
} }
private static String evaluateExpression(String expression) {
try (FileReader pomReader = new FileReader(".flattened-pom.xml")) {
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
XPathExpression expr = xpath.compile(expression);
String version = expr.evaluate(new InputSource(pomReader));
return version;
}
catch (Exception ex) {
throw new IllegalStateException("Failed to evaluate expression", ex);
}
}
} }
...@@ -3,10 +3,6 @@ plugins { ...@@ -3,10 +3,6 @@ plugins {
id 'org.springframework.boot' version '{version}' id 'org.springframework.boot' version '{version}'
} }
repositories {
mavenLocal()
}
dependencies { dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
} }
...@@ -17,7 +17,7 @@ if (project.hasProperty('applyDependencyManagementPlugin')) { ...@@ -17,7 +17,7 @@ if (project.hasProperty('applyDependencyManagementPlugin')) {
} }
repositories { repositories {
mavenLocal() maven { url 'file:repository' }
} }
task doesNotHaveDependencyManagement { task doesNotHaveDependencyManagement {
......
...@@ -6,7 +6,7 @@ plugins { ...@@ -6,7 +6,7 @@ plugins {
apply plugin: 'io.spring.dependency-management' apply plugin: 'io.spring.dependency-management'
repositories { repositories {
mavenLocal() maven { url 'file:repository' }
} }
dependencyManagement { dependencyManagement {
......
...@@ -18,7 +18,7 @@ sourceSets.all { ...@@ -18,7 +18,7 @@ sourceSets.all {
} }
repositories { repositories {
maven { url "https://repo.spring.io/libs-snapshot" } mavenCentral()
} }
dependencies { dependencies {
......
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>TEST-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<slf4j.version>1.7.25</slf4j.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>TEST-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment