Skip to content

Commit

Permalink
Basic changes to integrate the new platform model based on the decomp…
Browse files Browse the repository at this point in the history
…osed 'universe' BOM
  • Loading branch information
aloubyansky committed May 18, 2021
1 parent 2da1207 commit 12ab384
Show file tree
Hide file tree
Showing 46 changed files with 1,704 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,15 @@ public class BootstrapConfig {
@ConfigItem(defaultValue = "false")
boolean effectiveModelBuilder;

/**
* Whether to throw an error, warn or silently ignore misaligned platform BOM imports
*/
@ConfigItem(defaultValue = "error")
public MisalignedPlatformImports misalignedPlatformImports;

public enum MisalignedPlatformImports {
ERROR,
WARN,
IGNORE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.quarkus.builder.BuildResult;
import io.quarkus.builder.item.BuildItem;
import io.quarkus.deployment.builditem.AdditionalApplicationArchiveBuildItem;
import io.quarkus.deployment.builditem.AppModelProviderBuildItem;
import io.quarkus.deployment.builditem.ArchiveRootBuildItem;
import io.quarkus.deployment.builditem.GeneratedClassBuildItem;
import io.quarkus.deployment.builditem.GeneratedResourceBuildItem;
Expand All @@ -33,7 +34,6 @@
import io.quarkus.deployment.builditem.RawCommandLineArgumentsBuildItem;
import io.quarkus.deployment.builditem.ShutdownContextBuildItem;
import io.quarkus.deployment.pkg.builditem.BuildSystemTargetBuildItem;
import io.quarkus.deployment.pkg.builditem.CurateOutcomeBuildItem;
import io.quarkus.deployment.pkg.builditem.DeploymentResultBuildItem;
import io.quarkus.dev.spi.DevModeType;
import io.quarkus.runtime.LaunchMode;
Expand Down Expand Up @@ -117,7 +117,7 @@ public BuildResult run() throws Exception {
.addInitial(LiveReloadBuildItem.class)
.addInitial(AdditionalApplicationArchiveBuildItem.class)
.addInitial(BuildSystemTargetBuildItem.class)
.addInitial(CurateOutcomeBuildItem.class);
.addInitial(AppModelProviderBuildItem.class);
for (Class<? extends BuildItem> i : finalResults) {
chainBuilder.addFinal(i);
}
Expand Down Expand Up @@ -146,7 +146,7 @@ public BuildResult run() throws Exception {
devModeType == null ? Optional.empty() : Optional.of(devModeType), auxiliaryApplication))
.produce(new BuildSystemTargetBuildItem(targetDir, baseName, rebuild,
buildSystemProperties == null ? new Properties() : buildSystemProperties))
.produce(new CurateOutcomeBuildItem(effectiveModel));
.produce(new AppModelProviderBuildItem(effectiveModel));
for (PathsCollection i : additionalApplicationArchives) {
execBuilder.produce(new AdditionalApplicationArchiveBuildItem(i));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.quarkus.deployment.builditem;

import org.jboss.logging.Logger;

import io.quarkus.bootstrap.model.AppModel;
import io.quarkus.bootstrap.model.PlatformImports;
import io.quarkus.builder.item.SimpleBuildItem;
import io.quarkus.deployment.BootstrapConfig;

public final class AppModelProviderBuildItem extends SimpleBuildItem {

private static final Logger log = Logger.getLogger(AppModelProviderBuildItem.class);

private final AppModel appModel;

public AppModelProviderBuildItem(AppModel appModel) {
this.appModel = appModel;
}

public AppModel validateAndGet(BootstrapConfig config) {
final PlatformImports platforms = appModel.getPlatforms();
if (platforms != null && !BootstrapConfig.MisalignedPlatformImports.IGNORE.equals(config.misalignedPlatformImports)
&& !platforms.isAligned()) {
switch (config.misalignedPlatformImports) {
case ERROR:
throw new RuntimeException(platforms.getMisalignmentReport());
case WARN:
log.warn(platforms.getMisalignmentReport());
break;
default:
throw new RuntimeException("Unrecognized option for quarkus.bootstrap.misaligned-platform-imports: "
+ config.misalignedPlatformImports);
}
}
return appModel;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.quarkus.deployment.steps;

import io.quarkus.deployment.BootstrapConfig;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.AppModelProviderBuildItem;
import io.quarkus.deployment.pkg.builditem.CurateOutcomeBuildItem;

public class CurateOutcomeBuildStep {

BootstrapConfig config;

@BuildStep
CurateOutcomeBuildItem curateOutcome(AppModelProviderBuildItem appModelProvider) {
return new CurateOutcomeBuildItem(appModelProvider.validateAndGet(config));
}
}
2 changes: 1 addition & 1 deletion devtools/cli/src/main/java/io/quarkus/cli/Create.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static class TargetBuildTool {
boolean gradle = false;

@CommandLine.Option(names = {
"--grade-kotlin-dsl" }, order = 7, description = "Create a Gradle Kotlin DSL project.")
"--gradle-kotlin-dsl" }, order = 7, description = "Create a Gradle Kotlin DSL project.")
boolean gradleKotlinDsl = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ static QuarkusProject getQuarkusProject(BuildTool buildTool, Path projectRoot) {
}

private static QuarkusProject getNonMavenProject(Path projectRoot, BuildTool buildTool) {
return QuarkusProjectHelper.getProject(projectRoot, buildTool,
QuarkusCliVersion.version());
return QuarkusProjectHelper.getProject(projectRoot, buildTool, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ String getBuildGradlePath() {
return BUILD_GRADLE_PATH;
}

@Override
protected boolean importBom(ArtifactCoords coords) {
return importBomInModel(getModel(), coords);
}

@Override
protected boolean addDependency(ArtifactCoords coords, boolean managed) {
return addDependencyInModel(getModel(), coords, managed);
Expand All @@ -35,6 +40,12 @@ public BuildTool getBuildTool() {
return BuildTool.GRADLE;
}

static boolean importBomInModel(Model model, ArtifactCoords coords) {
return addDependencyInModel(model,
String.format(" implementation enforcedPlatform(%s)%n",
createDependencyCoordinatesString(coords, false, '\'')));
}

static boolean addDependencyInModel(Model model, ArtifactCoords coords, boolean managed) {
return addDependencyInModel(model,
String.format(" implementation %s%n", createDependencyCoordinatesString(coords, managed, '\'')));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ String getBuildGradlePath() {
return BUILD_GRADLE_PATH;
}

@Override
protected boolean importBom(ArtifactCoords coords) {
return importBomInModel(getModel(), coords);
}

@Override
protected boolean addDependency(ArtifactCoords coords, boolean managed) {
return addDependencyInModel(getModel(), coords, managed);
Expand All @@ -35,6 +40,12 @@ public BuildTool getBuildTool() {
return BuildTool.GRADLE_KOTLIN_DSL;
}

static boolean importBomInModel(Model model, ArtifactCoords coords) {
return addDependencyInModel(model,
String.format(" implementation enforcedPlatform(%s)%n",
createDependencyCoordinatesString(coords, false, '\'')));
}

static boolean addDependencyInModel(Model model, ArtifactCoords coords, boolean managed) {
return addDependencyInModel(model,
String.format(" implementation(%s)%n", createDependencyCoordinatesString(coords, managed, '"')));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
import static io.quarkus.bootstrap.resolver.model.impl.ArtifactCoordsImpl.TYPE_JAR;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
Expand Down Expand Up @@ -46,7 +43,8 @@
import org.gradle.tooling.provider.model.ParameterizedToolingModelBuilder;

import io.quarkus.bootstrap.BootstrapConstants;
import io.quarkus.bootstrap.model.AppArtifactKey;
import io.quarkus.bootstrap.model.PlatformImports;
import io.quarkus.bootstrap.resolver.AppModelResolverException;
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,7 +101,7 @@ public Object buildAll(String modelName, ModelParameter parameter, Project proje

final List<org.gradle.api.artifacts.Dependency> deploymentDeps = getEnforcedPlatforms(project);

final Map<String, String> platformProperties = resolvePlatformProperties(project, deploymentDeps);
final PlatformImports platformImports = resolvePlatformImports(project, deploymentDeps);

final Map<ArtifactCoords, Dependency> appDependencies = new LinkedHashMap<>();
final Set<ArtifactCoords> visitedDeps = new HashSet<>();
Expand All @@ -124,23 +122,20 @@ public Object buildAll(String modelName, ModelParameter parameter, Project proje
extensionDependencies,
deploymentDeps.stream().map(QuarkusModelBuilder::toEnforcedPlatformDependency)
.filter(Objects::nonNull).collect(Collectors.toList()),
platformProperties);
platformImports);
}

private Map<String, String> resolvePlatformProperties(Project project,
private PlatformImports resolvePlatformImports(Project project,
List<org.gradle.api.artifacts.Dependency> deploymentDeps) {
final Configuration boms = project.getConfigurations()
.detachedConfiguration(deploymentDeps.toArray(new org.gradle.api.artifacts.Dependency[0]));
final Map<String, String> platformProps = new HashMap<>();
final Set<AppArtifactKey> descriptorKeys = new HashSet<>(4);
final Set<AppArtifactKey> propertyKeys = new HashSet<>(2);
final PlatformImports platformImports = new PlatformImports();
boms.getResolutionStrategy().eachDependency(d -> {
final String group = d.getTarget().getGroup();
final String name = d.getTarget().getName();
if (name.endsWith(BootstrapConstants.PLATFORM_DESCRIPTOR_ARTIFACT_ID_SUFFIX)) {
descriptorKeys.add(new AppArtifactKey(group,
name.substring(0, name.length() - BootstrapConstants.PLATFORM_DESCRIPTOR_ARTIFACT_ID_SUFFIX.length()),
d.getTarget().getVersion()));
platformImports.addPlatformDescriptor(group, name, d.getTarget().getVersion(), "json",
d.getTarget().getVersion());
} else if (name.endsWith(BootstrapConstants.PLATFORM_PROPERTIES_ARTIFACT_ID_SUFFIX)) {
final DefaultDependencyArtifact dep = new DefaultDependencyArtifact();
dep.setExtension("properties");
Expand All @@ -154,44 +149,20 @@ private Map<String, String> resolvePlatformProperties(Project project,
for (ResolvedArtifact a : project.getConfigurations().detachedConfiguration(gradleDep)
.getResolvedConfiguration().getResolvedArtifacts()) {
if (a.getName().equals(name)) {
final Properties props = new Properties();
try (InputStream is = new FileInputStream(a.getFile())) {
props.load(is);
} catch (IOException e) {
throw new GradleException("Failed to read properties from " + a.getFile(), e);
}
for (Map.Entry<?, ?> prop : props.entrySet()) {
final String propName = String.valueOf(prop.getKey());
if (propName.startsWith(BootstrapConstants.PLATFORM_PROPERTY_PREFIX)) {
platformProps.put(propName, String.valueOf(prop.getValue()));
}
try {
platformImports.addPlatformProperties(group, name, null, "properties", d.getTarget().getVersion(),
a.getFile().toPath());
} catch (AppModelResolverException e) {
throw new GradleException("Failed to import platform properties " + a.getFile(), e);
}
break;
}
}
propertyKeys.add(new AppArtifactKey(group,
name.substring(0, name.length() - BootstrapConstants.PLATFORM_PROPERTIES_ARTIFACT_ID_SUFFIX.length()),
d.getTarget().getVersion()));
}

});
boms.getResolvedConfiguration();
if (!descriptorKeys.containsAll(propertyKeys)) {
final StringBuilder buf = new StringBuilder();
buf.append(
"The Quarkus platform properties applied to the project are missing the corresponding Quarkus platform BOM imports:");
final int l = buf.length();
for (AppArtifactKey key : propertyKeys) {
if (!descriptorKeys.contains(key)) {
if (l - buf.length() < 0) {
buf.append(',');
}
buf.append(' ').append(key);
}
}
throw new GradleException(buf.toString());
}
return platformProps;
return platformImports;
}

public Set<WorkspaceModule> getWorkspace(Project project, LaunchMode mode, ArtifactCoords mainModuleCoord) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class AppModel implements Serializable {
*/
private final Set<AppArtifactKey> localProjectArtifacts;

private final Map<String, String> platformProperties;
private final PlatformImports platformImports;

private final Map<String, CapabilityContract> capabilitiesContracts;

Expand All @@ -73,13 +73,17 @@ private AppModel(Builder builder) {
this.runnerParentFirstArtifacts = builder.runnerParentFirstArtifacts;
this.lesserPriorityArtifacts = builder.lesserPriorityArtifacts;
this.localProjectArtifacts = builder.localProjectArtifacts;
this.platformProperties = builder.platformProperties;
this.platformImports = builder.platformImports;
this.capabilitiesContracts = builder.capabilitiesContracts;
log.debugf("Created AppModel %s", this);
}

public Map<String, String> getPlatformProperties() {
return platformProperties;
return platformImports == null ? Collections.emptyMap() : platformImports.getPlatformProperties();
}

public PlatformImports getPlatforms() {
return platformImports;
}

public AppArtifact getAppArtifact() {
Expand Down Expand Up @@ -150,7 +154,7 @@ public static class Builder {
private final Set<AppArtifactKey> excludedArtifacts = new HashSet<>();
private final Set<AppArtifactKey> lesserPriorityArtifacts = new HashSet<>();
private final Set<AppArtifactKey> localProjectArtifacts = new HashSet<>();
private Map<String, String> platformProperties = Collections.emptyMap();
private PlatformImports platformImports;
private Map<String, CapabilityContract> capabilitiesContracts = Collections.emptyMap();

private Predicate<AppDependency> depPredicate;
Expand All @@ -160,12 +164,8 @@ public Builder setAppArtifact(AppArtifact appArtifact) {
return this;
}

public Builder addPlatformProperties(Map<String, String> platformProperties) {
if (this.platformProperties.isEmpty()) {
this.platformProperties = platformProperties;
} else {
this.platformProperties.putAll(platformProperties);
}
public Builder setPlatformImports(PlatformImports platformImports) {
this.platformImports = platformImports;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ public AppModel getAppModel(Path root) {
for (AppArtifactKey i : localProjectArtifacts) {
model.addLocalProjectArtifact(i);
}
model.addPlatformProperties(platformProperties);
final PlatformImports pi = new PlatformImports();
pi.setPlatformProperties(platformProperties);
model.setPlatformImports(pi);
return model.build();
}

Expand Down
Loading

0 comments on commit 12ab384

Please sign in to comment.