Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow extensions to inject a MavenArtifactResolver #39955

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.wildfly.common.function.Functions;

import io.quarkus.bootstrap.model.ApplicationModel;
import io.quarkus.bootstrap.resolver.maven.MavenArtifactResolver;
import io.quarkus.builder.BuildChainBuilder;
import io.quarkus.builder.BuildContext;
import io.quarkus.builder.BuildStepBuilder;
Expand Down Expand Up @@ -124,13 +125,15 @@ private static boolean isRecorder(AnnotatedElement element) {
*
* @param classLoader the class loader
* @param buildSystemProps the build system properties to use
* @param artifactResolver the Maven artifact resolver to use
* @param launchMode launch mode
* @return a consumer which adds the steps to the given chain builder
* @throws IOException if the class loader could not load a resource
* @throws ClassNotFoundException if a build step class is not found
*/
public static Consumer<BuildChainBuilder> loadStepsFrom(ClassLoader classLoader, Properties buildSystemProps,
ApplicationModel appModel, LaunchMode launchMode, DevModeType devModeType)
MavenArtifactResolver artifactResolver, ApplicationModel appModel, LaunchMode launchMode,
DevModeType devModeType)
throws IOException, ClassNotFoundException {

final BuildTimeConfigurationReader reader = new BuildTimeConfigurationReader(classLoader);
Expand Down Expand Up @@ -159,7 +162,7 @@ public String getId() {
Map<Class<?>, Object> proxies = new HashMap<>();
for (Class<?> clazz : ServiceUtil.classesNamedIn(classLoader, "META-INF/quarkus-build-steps.list")) {
try {
result = result.andThen(ExtensionLoader.loadStepsFromClass(clazz, readResult, proxies, bsf));
result = result.andThen(ExtensionLoader.loadStepsFromClass(clazz, readResult, proxies, bsf, artifactResolver));
} catch (Throwable e) {
throw new RuntimeException("Failed to load steps from " + clazz, e);
}
Expand Down Expand Up @@ -243,11 +246,13 @@ public String getId() {
* @param clazz the class to load from (must not be {@code null})
* @param readResult the build time configuration read result (must not be {@code null})
* @param runTimeProxies the map of run time proxy objects to populate for recorders (must not be {@code null})
* @param artifactResolver the Maven artifact resolver for injection
* @return a consumer which adds the steps to the given chain builder
*/
private static Consumer<BuildChainBuilder> loadStepsFromClass(Class<?> clazz,
BuildTimeConfigurationReader.ReadResult readResult,
Map<Class<?>, Object> runTimeProxies, BooleanSupplierFactoryBuildItem supplierFactory) {
Map<Class<?>, Object> runTimeProxies, BooleanSupplierFactoryBuildItem supplierFactory,
MavenArtifactResolver artifactResolver) {
final Constructor<?>[] constructors = clazz.getDeclaredConstructors();
// this is the chain configuration that will contain all steps on this class and be returned
Consumer<BuildChainBuilder> chainConfig = Functions.discardingConsumer();
Expand Down Expand Up @@ -277,7 +282,9 @@ private static Consumer<BuildChainBuilder> loadStepsFromClass(Class<?> clazz,
for (Parameter parameter : ctorParameters) {
Type parameterType = parameter.getParameterizedType();
final Class<?> parameterClass = parameter.getType();
if (rawTypeExtends(parameterType, SimpleBuildItem.class)) {
if (parameterClass == MavenArtifactResolver.class) {
ctorParamFns.add(bc -> artifactResolver);
} else if (rawTypeExtends(parameterType, SimpleBuildItem.class)) {
final Class<? extends SimpleBuildItem> buildItemClass = rawTypeOf(parameterType)
.asSubclass(SimpleBuildItem.class);
stepConfig = stepConfig.andThen(bsb -> bsb.consumes(buildItemClass));
Expand Down Expand Up @@ -356,7 +363,9 @@ private static Consumer<BuildChainBuilder> loadStepsFromClass(Class<?> clazz,
// next, determine the type
final Type fieldType = field.getGenericType();
final Class<?> fieldClass = field.getType();
if (rawTypeExtends(fieldType, SimpleBuildItem.class)) {
if (fieldType == MavenArtifactResolver.class) {
stepInstanceSetup = stepInstanceSetup.andThen((bc, o) -> ReflectUtil.setFieldVal(field, o, artifactResolver));
} else if (rawTypeExtends(fieldType, SimpleBuildItem.class)) {
final Class<? extends SimpleBuildItem> buildItemClass = rawTypeOf(fieldType).asSubclass(SimpleBuildItem.class);
stepConfig = stepConfig.andThen(bsb -> bsb.consumes(buildItemClass));
stepInstanceSetup = stepInstanceSetup
Expand Down Expand Up @@ -492,7 +501,9 @@ private static Consumer<BuildChainBuilder> loadStepsFromClass(Class<?> clazz,
final boolean overridable = parameter.isAnnotationPresent(Overridable.class);
final Type parameterType = parameter.getParameterizedType();
final Class<?> parameterClass = parameter.getType();
if (rawTypeExtends(parameterType, SimpleBuildItem.class)) {
if (parameterType == MavenArtifactResolver.class) {
methodParamFns.add((bc, bri) -> artifactResolver);
} else if (rawTypeExtends(parameterType, SimpleBuildItem.class)) {
final Class<? extends SimpleBuildItem> buildItemClass = parameterClass
.asSubclass(SimpleBuildItem.class);
methodStepConfig = methodStepConfig.andThen(bsb -> bsb.consumes(buildItemClass));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.quarkus.bootstrap.classloading.QuarkusClassLoader;
import io.quarkus.bootstrap.model.ApplicationModel;
import io.quarkus.bootstrap.resolver.maven.MavenArtifactResolver;
import io.quarkus.builder.BuildChain;
import io.quarkus.builder.BuildChainBuilder;
import io.quarkus.builder.BuildExecutionBuilder;
Expand Down Expand Up @@ -62,6 +63,7 @@ public class QuarkusAugmentor {
private final boolean auxiliaryApplication;
private final Optional<DevModeType> auxiliaryDevModeType;
private final boolean test;
private final MavenArtifactResolver mavenArtifactResolver;

QuarkusAugmentor(Builder builder) {
this.classLoader = builder.classLoader;
Expand All @@ -83,6 +85,7 @@ public class QuarkusAugmentor {
this.auxiliaryApplication = builder.auxiliaryApplication;
this.auxiliaryDevModeType = Optional.ofNullable(builder.auxiliaryDevModeType);
this.test = builder.test;
this.mavenArtifactResolver = builder.mavenArtifactResolver;
}

public BuildResult run() throws Exception {
Expand All @@ -106,7 +109,7 @@ public BuildResult run() throws Exception {
//in additional stuff from the deployment leaking in, this is unlikely but has a bit of a smell.
ExtensionLoader.loadStepsFrom(deploymentClassLoader,
buildSystemProperties == null ? new Properties() : buildSystemProperties,
effectiveModel, launchMode, devModeType)
mavenArtifactResolver, effectiveModel, launchMode, devModeType)
.accept(chainBuilder);

Thread.currentThread().setContextClassLoader(classLoader);
Expand Down Expand Up @@ -218,6 +221,7 @@ public static final class Builder {
DevModeType devModeType;
boolean test;
boolean auxiliaryApplication;
MavenArtifactResolver mavenArtifactResolver;

public Builder addBuildChainCustomizer(Consumer<BuildChainBuilder> customizer) {
this.buildChainCustomizers.add(customizer);
Expand Down Expand Up @@ -357,5 +361,10 @@ public Builder setDeploymentClassLoader(ClassLoader deploymentClassLoader) {
this.deploymentClassLoader = deploymentClassLoader;
return this;
}

public Builder setMavenArchiveResolver(final MavenArtifactResolver mavenArtifactResolver) {
this.mavenArtifactResolver = mavenArtifactResolver;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ private BuildResult runAugment(boolean firstRun, Set<String> changedResources,
.setClassLoader(classLoader)
.setTargetDir(quarkusBootstrap.getTargetDirectory())
.setDeploymentClassLoader(deploymentClassLoader)
.setMavenArchiveResolver(quarkusBootstrap.mavenArtifactResolver())
.setBuildSystemProperties(quarkusBootstrap.getBuildSystemProperties())
.setEffectiveModel(curatedApplication.getApplicationModel());
if (quarkusBootstrap.getBaseName() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ private MavenArtifactResolver artifactResolver(QuarkusBootstrapMojo mojo, Launch

private CuratedApplication doBootstrap(QuarkusBootstrapMojo mojo, LaunchMode mode)
throws MojoExecutionException {
final BootstrapAppModelResolver modelResolver = new BootstrapAppModelResolver(artifactResolver(mojo, mode))
MavenArtifactResolver resolver = artifactResolver(mojo, mode);
final BootstrapAppModelResolver modelResolver = new BootstrapAppModelResolver(resolver)
.setDevMode(mode == LaunchMode.DEVELOPMENT)
.setTest(mode == LaunchMode.TEST)
.setCollectReloadableDependencies(mode == LaunchMode.DEVELOPMENT || mode == LaunchMode.TEST);
Expand Down Expand Up @@ -246,7 +247,8 @@ private CuratedApplication doBootstrap(QuarkusBootstrapMojo mojo, LaunchMode mod
.setBaseName(mojo.finalName())
.setOriginalBaseName(mojo.mavenProject().getBuild().getFinalName())
.setTargetDirectory(mojo.buildDir().toPath())
.setForcedDependencies(forcedDependencies);
.setForcedDependencies(forcedDependencies)
.setMavenArtifactResolver(resolver);

try {
return builder.build().bootstrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.quarkus.maven.dependency.ResolvedDependency;
import io.quarkus.paths.PathCollection;
import io.quarkus.paths.PathList;
import io.smallrye.common.constraint.Assert;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems unused?


/**
* The entry point for starting/building a Quarkus application. This class sets up the base class loading
Expand Down Expand Up @@ -295,6 +296,10 @@ public boolean isTest() {
return test;
}

public MavenArtifactResolver mavenArtifactResolver() {
return mavenArtifactResolver;
}

public static class Builder {
public List<ClassLoaderEventListener> classLoadListeners = new ArrayList<>();
public boolean hostApplicationIsTestOnly;
Expand Down