Skip to content

Commit

Permalink
handle gradle included builds
Browse files Browse the repository at this point in the history
  • Loading branch information
glefloch committed Feb 16, 2021
1 parent c794b5a commit 4f22482
Show file tree
Hide file tree
Showing 15 changed files with 212 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.UnknownDomainObjectException;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ExternalModuleDependency;
import org.gradle.api.artifacts.ModuleDependency;
Expand All @@ -35,6 +36,7 @@
import org.gradle.api.artifacts.ResolvedDependency;
import org.gradle.api.artifacts.component.ProjectComponentIdentifier;
import org.gradle.api.attributes.Category;
import org.gradle.api.initialization.IncludedBuild;
import org.gradle.api.internal.artifacts.dependencies.DefaultDependencyArtifact;
import org.gradle.api.internal.artifacts.dependencies.DefaultExternalModuleDependency;
import org.gradle.api.plugins.Convention;
Expand All @@ -45,6 +47,7 @@

import io.quarkus.bootstrap.BootstrapConstants;
import io.quarkus.bootstrap.model.AppArtifactKey;
import io.quarkus.bootstrap.resolver.QuarkusGradleModelFactory;
import io.quarkus.bootstrap.resolver.model.ArtifactCoords;
import io.quarkus.bootstrap.resolver.model.Dependency;
import io.quarkus.bootstrap.resolver.model.ModelParameter;
Expand Down Expand Up @@ -103,17 +106,18 @@ public Object buildAll(String modelName, ModelParameter parameter, Project proje
final Map<ArtifactCoords, Dependency> appDependencies = new LinkedHashMap<>();
final Set<ArtifactCoords> visitedDeps = new HashSet<>();

final ResolvedConfiguration configuration = classpathConfig(project, mode).getResolvedConfiguration();
collectDependencies(configuration, mode, project, appDependencies);
collectFirstMetDeploymentDeps(configuration.getFirstLevelModuleDependencies(), appDependencies,
final ResolvedConfiguration resolvedConfiguration = classpathConfig(project, mode).getResolvedConfiguration();
collectDependencies(resolvedConfiguration, mode, project, appDependencies);
collectFirstMetDeploymentDeps(resolvedConfiguration.getFirstLevelModuleDependencies(), appDependencies,
deploymentDeps, visitedDeps);

final List<Dependency> extensionDependencies = collectExtensionDependencies(project, deploymentDeps);

ArtifactCoords appArtifactCoords = new ArtifactCoordsImpl(project.getGroup().toString(), project.getName(),
project.getVersion().toString());

return new QuarkusModelImpl(new WorkspaceImpl(appArtifactCoords, getWorkspace(project.getRootProject(), mode)),
return new QuarkusModelImpl(
new WorkspaceImpl(appArtifactCoords, getWorkspace(project.getRootProject(), mode)),
new LinkedList<>(appDependencies.values()),
extensionDependencies,
deploymentDeps.stream().map(QuarkusModelBuilder::toEnforcedPlatformDependency)
Expand Down Expand Up @@ -317,8 +321,10 @@ private List<Dependency> collectExtensionDependencies(Project project,

private void collectDependencies(ResolvedConfiguration configuration,
LaunchMode mode, Project project, Map<ArtifactCoords, Dependency> appDependencies) {

final Set<ResolvedArtifact> artifacts = configuration.getResolvedArtifacts();
Set<File> artifactFiles = null;

// if the number of artifacts is less than the number of files then probably
// the project includes direct file dependencies
if (artifacts.size() < configuration.getFiles().size()) {
Expand All @@ -331,9 +337,14 @@ private void collectDependencies(ResolvedConfiguration configuration,
final DependencyImpl dep = initDependency(a);
if (LaunchMode.DEVELOPMENT.equals(mode) &&
a.getId().getComponentIdentifier() instanceof ProjectComponentIdentifier) {
Project projectDep = project.getRootProject()
.findProject(((ProjectComponentIdentifier) a.getId().getComponentIdentifier()).getProjectPath());
addDevModePaths(dep, a, projectDep);
IncludedBuild includedBuild = includedBuild(project, a.getName());
if (includedBuild != null) {
addSubstitutedProject(dep, includedBuild.getProjectDir(), mode);
} else {
Project projectDep = project.getRootProject()
.findProject(((ProjectComponentIdentifier) a.getId().getComponentIdentifier()).getProjectPath());
addDevModePaths(dep, a, projectDep);
}
} else {
dep.addPath(a.getFile());
}
Expand Down Expand Up @@ -402,6 +413,24 @@ private void addDevModePaths(final DependencyImpl dep, ResolvedArtifact a, Proje
}
}

private void addSubstitutedProject(final DependencyImpl dep, File projectFile, LaunchMode mode) {
final QuarkusModel quarkusModel = QuarkusGradleModelFactory.create(projectFile, mode.name());
final io.quarkus.bootstrap.resolver.model.SourceSet moduleOutput = quarkusModel.getWorkspace().getMainModule()
.getSourceSet();
dep.addPath(moduleOutput.getResourceDirectory());
for (File sourceDirectory : moduleOutput.getSourceDirectories()) {
dep.addPath(sourceDirectory);
}
}

private IncludedBuild includedBuild(final Project project, final String projectName) {
try {
return project.getGradle().includedBuild(projectName);
} catch (UnknownDomainObjectException ignore) {
return null;
}
}

private SourceSetImpl convert(SourceSet sourceSet) {
if (sourceSet.getOutput().getResourcesDir().exists()) {
return new SourceSetImpl(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static QuarkusModel create(File projectDir, String mode, List<String> jvm
.forProjectDirectory(projectDir)
.connect()) {
connection.newBuild().forTasks(tasks).addJvmArguments(jvmArgs).run();

return connection.action(new QuarkusModelBuildAction(mode)).run();
}
}
Expand All @@ -28,7 +29,9 @@ public static QuarkusModel createForTasks(File projectDir, String... tasks) {
.forProjectDirectory(projectDir)
.connect()) {
final ModelBuilder<QuarkusModel> modelBuilder = connection.model(QuarkusModel.class);
modelBuilder.forTasks(tasks);
if (tasks.length != 0) {
modelBuilder.forTasks(tasks);
}
return modelBuilder.get();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ public Collection<WorkspaceModule> getAllModules() {
public WorkspaceModule getModule(ArtifactCoords key) {
return modules.get(key);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public BuildResult runGradleWrapper(File projectDir, String... args) throws IOEx
.directory(projectDir)
.command(command)
.redirectInput(ProcessBuilder.Redirect.INHERIT)
.redirectError(logOutput)
.redirectOutput(logOutput)
.redirectError(logOutput)
.start();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.quarkus.gradle.devmode;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class MultiModuleIncludedBuildTest extends QuarkusDevGradleTestBase {

@Override
protected String projectDirectoryName() {
return "multi-module-included-build";
}

@Override
protected String[] buildArguments() {
return new String[] { "clean", "--include-build", "../external-library", "quarkusDev" };
}

@Override
protected void testDevMode() throws Exception {
assertThat(getHttpResponse("/hello")).contains("foo bar");
}

@Override
protected File getProjectDir() {
File projectDir = super.getProjectDir();
final File appProjectProperties = new File(projectDir, "app/gradle.properties");
final File libProjectProperties = new File(projectDir, "external-library/gradle.properties");
final Path projectProperties = projectDir.toPath().resolve("gradle.properties");

try {
Files.copy(projectProperties, appProjectProperties.toPath());
Files.copy(projectProperties, libProjectProperties.toPath());
} catch (IOException e) {
throw new IllegalStateException("Unable to copy gradle.properties file", e);
}
this.projectDir = appProjectProperties.getParentFile();
return this.projectDir;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public abstract class QuarkusDevGradleTestBase extends QuarkusGradleWrapperTestB
private static final String PLUGIN_UNDER_TEST_METADATA_PROPERTIES = "plugin-under-test-metadata.properties";

private Future<?> quarkusDev;
private File projectDir;
protected File projectDir;

@Test
public void main() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
plugins {
id 'java'
id 'io.quarkus'
}

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

dependencies {
implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")
implementation 'io.quarkus:quarkus-resteasy'
implementation 'org.acme:external-library:1.0.0-SNAPSHOT'
}

group 'org.acme'
version '1.0.0-SNAPSHOT'

compileJava {
options.encoding = 'UTF-8'
options.compilerArgs << '-parameters'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pluginManagement {
repositories {
mavenLocal()
mavenCentral()
gradlePluginPortal()
}
plugins {
id 'io.quarkus' version "${quarkusPluginVersion}"
}
}

rootProject.name='app'
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.acme;

import org.acme.lib.LibService;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class ExampleResource {

@Inject
LibService libService;

@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "foo " + libService.bar();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Configuration file
# key = value
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
plugins {
id 'java'
id 'io.quarkus'
id "org.kordamp.gradle.jandex" version '0.6.0'
}

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

dependencies {
implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")
implementation 'io.quarkus:quarkus-core'
}

group 'org.acme'
version '1.0.0-SNAPSHOT'

compileJava {
options.encoding = 'UTF-8'
options.compilerArgs << '-parameters'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pluginManagement {
repositories {
mavenCentral()
gradlePluginPortal()
if (System.properties.containsKey('maven.repo.local')) {
maven {
url System.properties.get('maven.repo.local')
}
} else {
mavenLocal()
}
}
plugins {
id 'io.quarkus' version "${quarkusPluginVersion}"
}
}

rootProject.name='external-library'
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.acme.lib;

import javax.inject.Singleton;

@Singleton
public class LibService {
public String bar() {
return "bar";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#Gradle properties
#Tue Aug 25 06:41:36 GMT 2020
quarkusPlatformArtifactId=quarkus-bom
quarkusPlatformGroupId=io.quarkus

0 comments on commit 4f22482

Please sign in to comment.