Skip to content

Commit

Permalink
Improve robustness of our devtools Gradle code
Browse files Browse the repository at this point in the history
  • Loading branch information
mgorniew committed Sep 15, 2020
1 parent b9582fa commit 7f38fac
Show file tree
Hide file tree
Showing 19 changed files with 233 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,38 @@
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.maven.model.Dependency;
import org.gradle.tooling.BuildException;
import org.gradle.tooling.GradleConnector;
import org.gradle.tooling.ProjectConnection;
import org.gradle.tooling.model.eclipse.EclipseExternalDependency;
import org.gradle.tooling.model.eclipse.EclipseProject;

import io.quarkus.bootstrap.model.AppArtifactCoords;
import io.quarkus.bootstrap.resolver.QuarkusGradleModelFactory;
import io.quarkus.bootstrap.resolver.model.Dependency;
import io.quarkus.bootstrap.resolver.model.QuarkusModel;
import io.quarkus.runtime.LaunchMode;

public final class ConnectorDependencyResolver {

private List<Dependency> dependencies = null;
private List<AppArtifactCoords> dependencies = null;

List<Dependency> getDependencies(String buildContent, Path projectDirPath) {
List<AppArtifactCoords> getDependencies(String buildContent, Path projectDirPath) {
if (dependencies == null) {
EclipseProject eclipseProject = null;
QuarkusModel quarkusModel = null;
if (buildContent != null) {
try {
ProjectConnection connection = GradleConnector.newConnector()
.forProjectDirectory(projectDirPath.toFile())
.connect();
eclipseProject = connection.getModel(EclipseProject.class);
quarkusModel = QuarkusGradleModelFactory.create(projectDirPath.toFile(), LaunchMode.NORMAL.toString());
} catch (BuildException e) {
// ignore this error.
e.printStackTrace();
}
}
if (eclipseProject != null) {
dependencies = eclipseProject.getClasspath().stream().map(this::gradleModuleVersionToDependency)
.filter(Objects::nonNull)
.collect(Collectors.toList());
if (quarkusModel != null) {
dependencies = Stream
.concat(Stream.concat(quarkusModel.getAppDependencies().stream(),
quarkusModel.getExtensionDependencies().stream()),
quarkusModel.getEnforcedPlatformDependencies().stream())
.distinct().map(this::gradleModuleVersionToDependency).collect(Collectors.toList());
} else {
dependencies = Collections.emptyList();
}
Expand All @@ -43,15 +43,8 @@ List<Dependency> getDependencies(String buildContent, Path projectDirPath) {
return dependencies;
}

private Dependency gradleModuleVersionToDependency(EclipseExternalDependency eed) {
Dependency dependency = new Dependency();
if (eed == null || eed.getGradleModuleVersion() == null) {
// local dependencies are ignored
return null;
}
dependency.setGroupId(eed.getGradleModuleVersion().getGroup());
dependency.setArtifactId(eed.getGradleModuleVersion().getName());
dependency.setVersion(eed.getGradleModuleVersion().getVersion());
return dependency;
private AppArtifactCoords gradleModuleVersionToDependency(Dependency quarkusDependency) {
return new AppArtifactCoords(quarkusDependency.getGroupId(),
quarkusDependency.getName(), quarkusDependency.getType(), quarkusDependency.getVersion());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import java.nio.file.Path;
import java.util.List;

import org.apache.maven.model.Dependency;

import io.quarkus.bootstrap.model.AppArtifactCoords;
import io.quarkus.devtools.project.buildfile.AbstractGroovyGradleBuildFile;
import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor;

Expand All @@ -23,7 +22,7 @@ public GroovyBuildFileFromConnector(Path projectDirPath, QuarkusPlatformDescript
}

@Override
public List<Dependency> getDependencies() throws IOException {
public List<AppArtifactCoords> getDependencies() throws IOException {
return dependencyResolver.getDependencies(getBuildContent(), getProjectDirPath());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import java.nio.file.Path;
import java.util.List;

import org.apache.maven.model.Dependency;

import io.quarkus.bootstrap.model.AppArtifactCoords;
import io.quarkus.devtools.project.buildfile.AbstractKotlinGradleBuildFile;
import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor;

Expand All @@ -23,7 +22,7 @@ public KotlinBuildFileFromConnector(Path projectDirPath, QuarkusPlatformDescript
}

@Override
public List<Dependency> getDependencies() throws IOException {
public List<AppArtifactCoords> getDependencies() throws IOException {
return dependencyResolver.getDependencies(getBuildContent(), getProjectDirPath());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;

import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ExternalModuleDependency;
import org.gradle.api.artifacts.ModuleDependency;
import org.gradle.api.artifacts.ResolvedArtifact;
import org.gradle.api.artifacts.ResolvedConfiguration;
Expand Down Expand Up @@ -102,7 +105,9 @@ public Object buildAll(String modelName, ModelParameter parameter, Project proje

return new QuarkusModelImpl(new WorkspaceImpl(appArtifactCoords, getWorkspace(project.getRootProject(), mode)),
new LinkedList<>(appDependencies.values()),
extensionDependencies);
extensionDependencies,
deploymentDeps.stream().map(QuarkusModelBuilder::toEnforcedPlatformDependency)
.filter(Objects::nonNull).collect(Collectors.toList()));
}

public Set<WorkspaceModule> getWorkspace(Project project, LaunchMode mode) {
Expand Down Expand Up @@ -344,6 +349,18 @@ static Dependency toDependency(ResolvedArtifact a) {
return dependency;
}

static Dependency toEnforcedPlatformDependency(org.gradle.api.artifacts.Dependency dependency) {
if (dependency instanceof ExternalModuleDependency) {
ExternalModuleDependency emd = (ExternalModuleDependency) dependency;
Category category = emd.getAttributes().getAttribute(Category.CATEGORY_ATTRIBUTE);
if (category != null && Category.ENFORCED_PLATFORM.equals(category.getName())) {
return new DependencyImpl(emd.getName(), emd.getGroup(), emd.getVersion(),
"compile", "pom", null);
}
}
return null;
}

/**
* Creates an instance of DependencyImpl but does not associates it with a path
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,84 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.function.BiFunction;

import org.apache.maven.model.Dependency;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import io.quarkus.bootstrap.model.AppArtifactCoords;
import io.quarkus.devtools.project.buildfile.BuildFile;
import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor;

abstract class AbstractBuildFileTest {

protected static <T extends BuildFile> T initializeProject(String projectDir,
BiFunction<Path, QuarkusPlatformDescriptor, T> buildFileCreator)
throws IOException, URISyntaxException {
URL url = GroovyBuildFileTest.class.getClassLoader().getResource(projectDir);
URI uri = url.toURI();
Path gradleProjectPath = Paths.get(uri);

final File projectProps = new File(gradleProjectPath.toFile(), "gradle.properties");
if (!projectProps.exists()) {
throw new IllegalStateException("Failed to locate " + projectProps);
}
final Properties props = new Properties();
try (InputStream is = new FileInputStream(projectProps)) {
props.load(is);
}
final String quarkusVersion = getQuarkusVersion();
props.setProperty("quarkusPluginVersion", quarkusVersion);
try (OutputStream os = new FileOutputStream(projectProps)) {
props.store(os, "Quarkus Gradle TS");
}

final QuarkusPlatformDescriptor descriptor = Mockito.mock(QuarkusPlatformDescriptor.class);
return buildFileCreator.apply(gradleProjectPath, descriptor);
}

protected static String getQuarkusVersion() throws IOException {
final Path curDir = Paths.get("").toAbsolutePath().normalize();
final Path gradlePropsFile = curDir.resolve("gradle.properties");
Properties props = new Properties();
try (InputStream is = Files.newInputStream(gradlePropsFile)) {
props.load(is);
}
final String quarkusVersion = props.getProperty("version");
if (quarkusVersion == null) {
throw new IllegalStateException("Failed to locate Quarkus version in " + gradlePropsFile);
}
return quarkusVersion;
}

abstract String getProperty(String propertyName) throws IOException;

abstract List<Dependency> getDependencies() throws IOException;
abstract List<AppArtifactCoords> getDependencies() throws IOException;

@Test
void testGetDependencies() throws IOException {
List<Dependency> dependencies = getDependencies();
List<AppArtifactCoords> dependencies = getDependencies();
assertThat(dependencies).isNotEmpty();
List<String> depsString = new ArrayList<>();
for (Iterator<Dependency> depIter = dependencies.iterator(); depIter.hasNext();) {
Dependency dependency = depIter.next();
String managementKey = dependency.getManagementKey();
for (Iterator<AppArtifactCoords> depIter = dependencies.iterator(); depIter.hasNext();) {
AppArtifactCoords dependency = depIter.next();
String managementKey = dependency.getGroupId() + ":" + dependency.getArtifactId() + ":" + dependency.getType();
if (dependency.getVersion() != null && !dependency.getVersion().isEmpty()) {
managementKey += ':' + dependency.getVersion();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
package io.quarkus.gradle;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import org.apache.maven.model.Dependency;
import org.junit.jupiter.api.BeforeAll;
import org.mockito.Mockito;

import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor;
import io.quarkus.bootstrap.model.AppArtifactCoords;

class GroovyBuildFileTest extends AbstractBuildFileTest {

private static GroovyBuildFileFromConnector buildFile;

@BeforeAll
public static void beforeAll() throws URISyntaxException {
URL url = GroovyBuildFileTest.class.getClassLoader().getResource("gradle-project");
URI uri = url.toURI();
Path gradleProjectPath = Paths.get(uri);
final QuarkusPlatformDescriptor descriptor = Mockito.mock(QuarkusPlatformDescriptor.class);
buildFile = new GroovyBuildFileFromConnector(gradleProjectPath, descriptor);
public static void beforeAll() throws URISyntaxException, IOException {
buildFile = initializeProject("gradle-project", GroovyBuildFileFromConnector::new);
}

@Override
Expand All @@ -33,7 +23,7 @@ String getProperty(String propertyName) throws IOException {
}

@Override
List<Dependency> getDependencies() throws IOException {
List<AppArtifactCoords> getDependencies() throws IOException {
return buildFile.getDependencies();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
package io.quarkus.gradle;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import org.apache.maven.model.Dependency;
import org.junit.jupiter.api.BeforeAll;
import org.mockito.Mockito;

import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor;
import io.quarkus.bootstrap.model.AppArtifactCoords;

class KotlinBuildFileTest extends AbstractBuildFileTest {

private static KotlinBuildFileFromConnector buildFile;

@BeforeAll
public static void beforeAll() throws URISyntaxException {
URL url = KotlinBuildFileTest.class.getClassLoader().getResource("gradle-kts-project");
URI uri = url.toURI();
Path gradleProjectPath = Paths.get(uri);
final QuarkusPlatformDescriptor descriptor = Mockito.mock(QuarkusPlatformDescriptor.class);
buildFile = new KotlinBuildFileFromConnector(gradleProjectPath, descriptor);
public static void beforeAll() throws URISyntaxException, IOException {
buildFile = initializeProject("gradle-kts-project", KotlinBuildFileFromConnector::new);
}

@Override
Expand All @@ -33,7 +23,7 @@ String getProperty(String propertyName) throws IOException {
}

@Override
List<Dependency> getDependencies() throws IOException {
List<AppArtifactCoords> getDependencies() throws IOException {
return buildFile.getDependencies();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
plugins {
java
id("io.quarkus")
}

repositories {
mavenLocal()
if (System.getProperties().containsKey("maven.repo.local")) {
maven {
url = uri(System.getProperties().get("maven.repo.local") as String)
}
} else {
mavenLocal()
}
mavenCentral()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pluginManagement {
val quarkusPluginVersion: String by settings
repositories {
if (System.getProperties().containsKey("maven.repo.local")) {
maven {
url = uri(System.getProperties().get("maven.repo.local") as String)
}
} else {
mavenLocal()
}
mavenCentral()
gradlePluginPortal()
}
plugins {
id("io.quarkus") version quarkusPluginVersion
}
}
rootProject.name="gradle-kts-project"
11 changes: 8 additions & 3 deletions devtools/gradle/src/test/resources/gradle-project/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@


plugins {
id 'java'
id 'io.quarkus'
}

repositories {
mavenLocal()
if (System.properties.containsKey('maven.repo.local')) {
maven {
url System.properties.get('maven.repo.local')
}
} else {
mavenLocal()
}
mavenCentral()
}

Expand Down
Loading

0 comments on commit 7f38fac

Please sign in to comment.