-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initialize dependency validation for gradle extensions
- Loading branch information
Showing
14 changed files
with
303 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
...tension-plugin/src/main/java/io/quarkus/extension/gradle/tasks/ValidateExtensionTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package io.quarkus.extension.gradle.tasks; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import org.gradle.api.DefaultTask; | ||
import org.gradle.api.GradleException; | ||
import org.gradle.api.artifacts.Configuration; | ||
import org.gradle.api.artifacts.ResolvedArtifact; | ||
import org.gradle.api.artifacts.ResolvedModuleVersion; | ||
import org.gradle.api.logging.Logger; | ||
import org.gradle.api.tasks.Internal; | ||
import org.gradle.api.tasks.TaskAction; | ||
|
||
import io.quarkus.bootstrap.model.AppArtifactKey; | ||
import io.quarkus.extension.gradle.QuarkusExtensionConfiguration; | ||
import io.quarkus.gradle.tooling.dependency.DependencyUtils; | ||
import io.quarkus.gradle.tooling.dependency.ExtensionDependency; | ||
|
||
public class ValidateExtensionTask extends DefaultTask { | ||
|
||
private QuarkusExtensionConfiguration extensionConfiguration; | ||
private Configuration runtimeModuleClasspath; | ||
private Configuration deploymentModuleClasspath; | ||
|
||
public ValidateExtensionTask() { | ||
setDescription("Validate extension dependencies"); | ||
setGroup("quarkus"); | ||
} | ||
|
||
public void setQuarkusExtensionConfiguration(QuarkusExtensionConfiguration extensionConfiguration) { | ||
this.extensionConfiguration = extensionConfiguration; | ||
} | ||
|
||
@Internal | ||
public Configuration getRuntimeModuleClasspath() { | ||
return this.runtimeModuleClasspath; | ||
} | ||
|
||
public void setRuntimeModuleClasspath(Configuration runtimeModuleClasspath) { | ||
this.runtimeModuleClasspath = runtimeModuleClasspath; | ||
} | ||
|
||
@Internal | ||
public Configuration getDeploymentModuleClasspath() { | ||
return this.deploymentModuleClasspath; | ||
} | ||
|
||
public void setDeploymentModuleClasspath(Configuration deploymentModuleClasspath) { | ||
this.deploymentModuleClasspath = deploymentModuleClasspath; | ||
} | ||
|
||
@TaskAction | ||
public void validateExtension() { | ||
Set<ResolvedArtifact> runtimeArtifacts = getRuntimeModuleClasspath().getResolvedConfiguration().getResolvedArtifacts(); | ||
|
||
List<AppArtifactKey> deploymentModuleKeys = collectRuntimeExtensionsDeploymentKeys(runtimeArtifacts); | ||
List<AppArtifactKey> invalidRuntimeArtifacts = findExtensionInConfiguration(runtimeArtifacts, deploymentModuleKeys); | ||
|
||
Set<ResolvedArtifact> deploymentArtifacts = getDeploymentModuleClasspath().getResolvedConfiguration() | ||
.getResolvedArtifacts(); | ||
List<AppArtifactKey> existingDeploymentModuleKeys = findExtensionInConfiguration(deploymentArtifacts, | ||
deploymentModuleKeys); | ||
deploymentModuleKeys.removeAll(existingDeploymentModuleKeys); | ||
|
||
boolean hasErrors = false; | ||
if (!invalidRuntimeArtifacts.isEmpty()) { | ||
hasErrors = true; | ||
} | ||
if (!deploymentModuleKeys.isEmpty()) { | ||
hasErrors = true; | ||
} | ||
|
||
if (hasErrors) { | ||
printValidationErrors(invalidRuntimeArtifacts, deploymentModuleKeys); | ||
} | ||
} | ||
|
||
private List<AppArtifactKey> collectRuntimeExtensionsDeploymentKeys(Set<ResolvedArtifact> runtimeArtifacts) { | ||
List<AppArtifactKey> runtimeExtensions = new ArrayList<>(); | ||
for (ResolvedArtifact resolvedArtifact : runtimeArtifacts) { | ||
ExtensionDependency extension = DependencyUtils.getExtensionInfoOrNull(getProject(), resolvedArtifact); | ||
if (extension != null) { | ||
runtimeExtensions.add(new AppArtifactKey(extension.getDeploymentModule().getGroupId(), | ||
extension.getDeploymentModule().getArtifactId())); | ||
} | ||
} | ||
return runtimeExtensions; | ||
} | ||
|
||
private List<AppArtifactKey> findExtensionInConfiguration(Set<ResolvedArtifact> deploymentArtifacts, | ||
List<AppArtifactKey> extensions) { | ||
List<AppArtifactKey> foundExtensions = new ArrayList<>(); | ||
|
||
for (ResolvedArtifact deploymentArtifact : deploymentArtifacts) { | ||
AppArtifactKey key = toAppArtifactKey(deploymentArtifact.getModuleVersion()); | ||
if (extensions.contains(key)) { | ||
foundExtensions.add(key); | ||
} | ||
} | ||
return foundExtensions; | ||
} | ||
|
||
private void printValidationErrors(List<AppArtifactKey> invalidRuntimeArtifacts, | ||
List<AppArtifactKey> missingDeploymentArtifacts) { | ||
Logger log = getLogger(); | ||
log.error("Quarkus Extension Dependency Verification Error"); | ||
|
||
if (!invalidRuntimeArtifacts.isEmpty()) { | ||
log.error("The following deployment artifact(s) appear on the runtime classpath: "); | ||
for (AppArtifactKey invalidRuntimeArtifact : invalidRuntimeArtifacts) { | ||
log.error("- " + invalidRuntimeArtifact); | ||
} | ||
} | ||
|
||
if (!missingDeploymentArtifacts.isEmpty()) { | ||
log.error("The following deployment artifact(s) were found to be missing in the deployment module: "); | ||
for (AppArtifactKey missingDeploymentArtifact : missingDeploymentArtifacts) { | ||
log.error("- " + missingDeploymentArtifact); | ||
} | ||
} | ||
|
||
throw new GradleException("Quarkus Extension Dependency Verification Error. See logs below"); | ||
} | ||
|
||
private static AppArtifactKey toAppArtifactKey(ResolvedModuleVersion artifactId) { | ||
return new AppArtifactKey(artifactId.getId().getGroup(), artifactId.getId().getName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.