Skip to content

Commit

Permalink
Remove reliance on mavenLocal from Gradle plugin’s tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed Dec 18, 2018
1 parent 4edc328 commit 0ad72d5
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
<gradle.executable>./gradlew</gradle.executable>
<gradle.task>build</gradle.task>
<skip.gradle.build>false</skip.gradle.build>
<kotlin.version>1.2.20</kotlin.version>
</properties>
<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import io.spring.gradle.dependencymanagement.dsl.DependencyManagementExtension

// tag::configure-bom[]
plugins {
java
Expand All @@ -12,16 +14,18 @@ dependencyManagement {
}
// end::configure-bom[]

dependencyManagement {
the<DependencyManagementExtension>().apply {
resolutionStrategy {
eachDependency {
if (requested.group == "org.springframework.boot") {
useVersion("{version}")
useVersion("TEST-SNAPSHOT")
}
}
}
}

repositories {
mavenLocal()
maven {
url = uri("file:repository")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ dependencyManagement {
resolutionStrategy {
eachDependency {
if (it.requested.group == 'org.springframework.boot') {
it.useVersion '{version}'
it.useVersion 'TEST-SNAPSHOT'
}
}
}
}

repositories {
mavenLocal()
maven { url 'file:repository' }
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ the<DependencyManagementExtension>().apply {
resolutionStrategy {
eachDependency {
if (requested.group == "org.springframework.boot") {
useVersion("{version}")
useVersion("TEST-SNAPSHOT")
}
}
}
}

repositories {
mavenLocal()
maven {
url = uri("file:repository")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dependencyManagement {
resolutionStrategy {
eachDependency {
if (it.requested.group == 'org.springframework.boot') {
it.useVersion '{version}'
it.useVersion 'TEST-SNAPSHOT'
}
}
}
Expand All @@ -19,7 +19,7 @@ ext['slf4j.version'] = '1.7.20'
// end::custom-version[]

repositories {
mavenLocal()
maven { url 'file:repository' }
}

task slf4jVersion {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,26 @@ plugins {

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 {
resolutionStrategy {
eachDependency {
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") {
doLast {
println(project.the<DependencyManagementExtension>().managedVersions["org.slf4j:slf4j-api"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public void dependencyManagementInIsolationWithPluginsBlock() {
Assume.assumeTrue(this.gradleBuild.getDsl() == Dsl.KOTLIN);
assertThat(this.gradleBuild.script(
"src/main/gradle/managing-dependencies/configure-bom-with-plugins")
.build("dependencyManagement").getOutput())
.contains("org.springframework.boot:spring-boot-starter ");
.build("dependencyManagement").getOutput()).contains(
"org.springframework.boot:spring-boot-starter TEST-SNAPSHOT");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,10 @@ public void noKotlinVersionPropertyWithoutKotlinPlugin() {
}

@Test
public void kotlinVersionMatchesKotlinPluginVersion() {
public void kotlinVersionPropertyIsSet() {
String output = this.gradleBuild.build("kotlinVersion", "dependencies",
"--configuration", "compileClasspath").getOutput();
assertThat(output).contains("Kotlin version: 1.2.20");
assertThat(output)
.containsPattern("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.2.20");
assertThat(output).containsPattern("Kotlin version: [0-9]\\.[0-9]\\.[0-9]+");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,28 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.jar.JarFile;
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.dsl.DependencyManagementExtension;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.GradleRunner;
import org.jetbrains.kotlin.cli.common.PropertiesKt;
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.KotlinPlugin;
import org.junit.rules.TemporaryFolder;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
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.boot.loader.tools.LaunchScript;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.FileSystemUtils;

/**
* A {@link TestRule} for running a Gradle build using {@link GradleRunner}.
Expand Down Expand Up @@ -146,6 +141,7 @@ private List<File> pluginClasspath() {
new File(pathOfJarContaining(PropertiesKt.class)),
new File(pathOfJarContaining(KotlinCompilerRunner.class)),
new File(pathOfJarContaining(KotlinPlugin.class)),
new File(pathOfJarContaining(KotlinProject.class)),
new File(pathOfJarContaining(KotlinGradleSubplugin.class)),
new File(pathOfJarContaining(ArchiveEntry.class)));
}
Expand Down Expand Up @@ -185,6 +181,8 @@ public GradleRunner prepareRunner(String... arguments) throws IOException {
getDependencyManagementPluginVersion());
FileCopyUtils.copy(scriptContent, new FileWriter(
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)
.withPluginClasspath(pluginClasspath());
if (this.dsl != Dsl.KOTLIN) {
Expand Down Expand Up @@ -222,44 +220,22 @@ public String getGradleVersion() {
}

private static String getBootVersion() {
return evaluateExpression(
"/*[local-name()='project']/*[local-name()='parent']/*[local-name()='version']"
+ "/text()");
return "TEST-SNAPSHOT";
}

private static String getDependencyManagementPluginVersion() {
try (FileReader pomReader = new FileReader(".flattened-pom.xml")) {
Document pom = DocumentBuilderFactory.newInstance().newDocumentBuilder()
.parse(new InputSource(pomReader));
NodeList dependencyElements = pom.getElementsByTagName("dependency");
for (int i = 0; i < dependencyElements.getLength(); i++) {
Element dependency = (Element) dependencyElements.item(i);
if (dependency.getElementsByTagName("artifactId").item(0).getTextContent()
.equals("dependency-management-plugin")) {
return dependency.getElementsByTagName("version").item(0)
.getTextContent();
}
try {
URL location = DependencyManagementExtension.class.getProtectionDomain()
.getCodeSource().getLocation();
try (JarFile jar = new JarFile(new File(location.toURI()))) {
return jar.getManifest().getMainAttributes()
.getValue("Implementation-Version");
}
throw new IllegalStateException(
"dependency management plugin version not found");
}
catch (Exception ex) {
throw new IllegalStateException(
"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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ plugins {
id 'org.springframework.boot' version '{version}'
}

repositories {
mavenLocal()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ if (project.hasProperty('applyDependencyManagementPlugin')) {
}

repositories {
mavenLocal()
maven { url 'file:repository' }
}

task doesNotHaveDependencyManagement {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ dependencyManagement {

repositories {
mavenCentral()
mavenLocal()
maven { url 'file:repository' }
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
apply plugin: 'io.spring.dependency-management'

repositories {
mavenLocal()
maven { url 'file:repository' }
}

dependencyManagement {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ sourceSets.all {
}

repositories {
maven { url "https://repo.spring.io/libs-snapshot" }
mavenCentral()
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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>

0 comments on commit 0ad72d5

Please sign in to comment.