Repository: quarkusio/quarkus
Issue: JAX-RS exceptions in native mode CNFE javax.ws.rs.ext.Providers

Hi I see a lot of exceptions:
```
RESTEASY002120: ClassNotFoundException: Unable to load builtin provider org.jboss.resteasy.plugins.providers.JaxrsFormProvider from resource:META-INF/services/javax.ws.rs.ext.Providers
java.lang.ClassNotFoundException: org.jboss.resteasy.plugins.providers.JaxrsFormProvider...........
```

when I build https://github.com/quarkusio/quarkus-quickstarts/tree/master/using-opentracing to native executable. Is it problem with the example or bug?

Full log: https://pastebin.com/TN0yfCzN

Changed files:
extensions/smallrye-rest-client/deployment/src/main/java/io/quarkus/smallrye/restclient/deployment/SmallRyeRestClientProcessor.java
extensions/smallrye-rest-client/runtime/src/main/java/io/quarkus/smallrye/restclient/runtime/SmallRyeRestClientTemplate.java
extensions/smallrye-rest-client/runtime/src/main/java/io/quarkus/smallrye/restclient/runtime/RestClientBuilderImpl.java

diff --git a/extensions/smallrye-rest-client/deployment/src/main/java/io/quarkus/smallrye/restclient/deployment/SmallRyeRestClientProcessor.java b/extensions/smallrye-rest-client/deployment/src/main/java/io/quarkus/smallrye/restclient/deployment/SmallRyeRestClientProcessor.java
index ba85b88b632..7eee1cf12d9 100644
--- a/extensions/smallrye-rest-client/deployment/src/main/java/io/quarkus/smallrye/restclient/deployment/SmallRyeRestClientProcessor.java
+++ b/extensions/smallrye-rest-client/deployment/src/main/java/io/quarkus/smallrye/restclient/deployment/SmallRyeRestClientProcessor.java
@@ -62,13 +62,16 @@
 import io.quarkus.deployment.builditem.ExtensionSslNativeSupportBuildItem;
 import io.quarkus.deployment.builditem.FeatureBuildItem;
 import io.quarkus.deployment.builditem.SslNativeConfigBuildItem;
-import io.quarkus.deployment.builditem.substrate.*;
+import io.quarkus.deployment.builditem.substrate.ReflectiveClassBuildItem;
+import io.quarkus.deployment.builditem.substrate.ReflectiveHierarchyBuildItem;
+import io.quarkus.deployment.builditem.substrate.ServiceProviderBuildItem;
+import io.quarkus.deployment.builditem.substrate.SubstrateProxyDefinitionBuildItem;
+import io.quarkus.deployment.builditem.substrate.SubstrateResourceBuildItem;
 import io.quarkus.gizmo.MethodDescriptor;
 import io.quarkus.gizmo.ResultHandle;
 import io.quarkus.resteasy.common.deployment.JaxrsProvidersToRegisterBuildItem;
 import io.quarkus.smallrye.restclient.runtime.IncomingHeadersProvider;
 import io.quarkus.smallrye.restclient.runtime.RestClientBase;
-import io.quarkus.smallrye.restclient.runtime.RestClientBuilderImpl;
 import io.quarkus.smallrye.restclient.runtime.SmallRyeRestClientTemplate;
 import io.smallrye.restclient.DefaultResponseExceptionMapper;
 import io.smallrye.restclient.RestClientProxy;
@@ -125,6 +128,7 @@ void setup(BuildProducer<FeatureBuildItem> feature,
     }
 
     @BuildStep
+    @Record(ExecutionTime.STATIC_INIT)
     void processInterfaces(CombinedIndexBuildItem combinedIndexBuildItem,
             SslNativeConfigBuildItem sslNativeConfig,
             BuildProducer<SubstrateProxyDefinitionBuildItem> proxyDefinition,
@@ -132,7 +136,8 @@ void processInterfaces(CombinedIndexBuildItem combinedIndexBuildItem,
             BuildProducer<ReflectiveHierarchyBuildItem> reflectiveHierarchy,
             BuildProducer<BeanRegistrarBuildItem> beanRegistrars,
             BuildProducer<ExtensionSslNativeSupportBuildItem> extensionSslNativeSupport,
-            BuildProducer<ServiceProviderBuildItem> serviceProvider) {
+            BuildProducer<ServiceProviderBuildItem> serviceProvider,
+            SmallRyeRestClientTemplate smallRyeRestClientTemplate) {
 
         // According to the spec only rest client interfaces annotated with RegisterRestClient are registered as beans
         Map<DotName, ClassInfo> interfaces = new HashMap<>();
@@ -219,15 +224,18 @@ public void register(RegistrationContext registrationContext) {
 
         // Indicates that this extension would like the SSL support to be enabled
         extensionSslNativeSupport.produce(new ExtensionSslNativeSupportBuildItem(FeatureBuildItem.SMALLRYE_REST_CLIENT));
-        RestClientBuilderImpl.SSL_ENABLED = sslNativeConfig.isEnabled();
+
+        smallRyeRestClientTemplate.setSslEnabled(sslNativeConfig.isEnabled());
     }
 
     @BuildStep
+    @Record(ExecutionTime.STATIC_INIT)
     void registerProviders(BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
             JaxrsProvidersToRegisterBuildItem jaxrsProvidersToRegisterBuildItem,
-            CombinedIndexBuildItem combinedIndexBuildItem) {
-        RestClientBuilderImpl.REGISTER_BUILTIN_PROVIDERS = jaxrsProvidersToRegisterBuildItem.useBuiltIn();
-        RestClientBuilderImpl.PROVIDERS_TO_REGISTER = String.join(",", jaxrsProvidersToRegisterBuildItem.getProviders());
+            CombinedIndexBuildItem combinedIndexBuildItem,
+            SmallRyeRestClientTemplate smallRyeRestClientTemplate) {
+        smallRyeRestClientTemplate.initializeResteasyProviderFactory(jaxrsProvidersToRegisterBuildItem.useBuiltIn(),
+                jaxrsProvidersToRegisterBuildItem.getProviders());
 
         // register the providers for reflection
         for (String providerToRegister : jaxrsProvidersToRegisterBuildItem.getProviders()) {
diff --git a/extensions/smallrye-rest-client/runtime/src/main/java/io/quarkus/smallrye/restclient/runtime/RestClientBuilderImpl.java b/extensions/smallrye-rest-client/runtime/src/main/java/io/quarkus/smallrye/restclient/runtime/RestClientBuilderImpl.java
index 728a1daa815..8155def7b75 100644
--- a/extensions/smallrye-rest-client/runtime/src/main/java/io/quarkus/smallrye/restclient/runtime/RestClientBuilderImpl.java
+++ b/extensions/smallrye-rest-client/runtime/src/main/java/io/quarkus/smallrye/restclient/runtime/RestClientBuilderImpl.java
@@ -23,7 +23,14 @@
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
@@ -48,16 +55,20 @@
 import org.jboss.resteasy.client.jaxrs.ResteasyClient;
 import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
 import org.jboss.resteasy.client.jaxrs.engines.URLConnectionEngine;
-import org.jboss.resteasy.plugins.server.servlet.ResteasyContextParameters;
+import org.jboss.resteasy.client.jaxrs.internal.LocalResteasyProviderFactory;
+import org.jboss.resteasy.spi.ResteasyProviderFactory;
 import org.jboss.resteasy.spi.ResteasyUriBuilder;
 
-import io.smallrye.restclient.*;
+import io.smallrye.restclient.ConfigurationWrapper;
+import io.smallrye.restclient.DefaultMediaTypeFilter;
+import io.smallrye.restclient.DefaultResponseExceptionMapper;
+import io.smallrye.restclient.ExceptionMapping;
+import io.smallrye.restclient.MethodInjectionFilter;
+import io.smallrye.restclient.RestClientListeners;
+import io.smallrye.restclient.RestClientProxy;
 import io.smallrye.restclient.async.AsyncInvocationInterceptorHandler;
 import io.smallrye.restclient.header.ClientHeaderProviders;
 
-/**
- * Created by hbraun on 15.01.18.
- */
 public class RestClientBuilderImpl implements RestClientBuilder {
 
     private static final String RESTEASY_PROPERTY_PREFIX = "resteasy.";
@@ -68,17 +79,21 @@ public class RestClientBuilderImpl implements RestClientBuilder {
     public static final MethodInjectionFilter METHOD_INJECTION_FILTER = new MethodInjectionFilter();
     public static final ClientHeadersRequestFilter HEADERS_REQUEST_FILTER = new ClientHeadersRequestFilter();
 
-    public static boolean SSL_ENABLED = false;
-    public static boolean REGISTER_BUILTIN_PROVIDERS = true;
-    public static String PROVIDERS_TO_REGISTER = "";
+    static boolean SSL_ENABLED = false;
+    static ResteasyProviderFactory PROVIDER_FACTORY;
 
     RestClientBuilderImpl() {
         ClientBuilder availableBuilder = ClientBuilder.newBuilder();
 
         if (availableBuilder instanceof ResteasyClientBuilder) {
             builderDelegate = (ResteasyClientBuilder) availableBuilder;
-            builderDelegate.property(ResteasyContextParameters.RESTEASY_USE_BUILTIN_PROVIDERS, REGISTER_BUILTIN_PROVIDERS);
-            builderDelegate.property(ResteasyContextParameters.RESTEASY_PROVIDERS, PROVIDERS_TO_REGISTER);
+
+            ResteasyProviderFactory localProviderFactory = new LocalResteasyProviderFactory(PROVIDER_FACTORY);
+            if (ResteasyProviderFactory.peekInstance() != null) {
+                localProviderFactory.initializeClientProviders(ResteasyProviderFactory.getInstance());
+            }
+            builderDelegate.providerFactory(localProviderFactory);
+
             configurationWrapper = new ConfigurationWrapper(builderDelegate.getConfiguration());
             config = ConfigProvider.getConfig();
         } else {
diff --git a/extensions/smallrye-rest-client/runtime/src/main/java/io/quarkus/smallrye/restclient/runtime/SmallRyeRestClientTemplate.java b/extensions/smallrye-rest-client/runtime/src/main/java/io/quarkus/smallrye/restclient/runtime/SmallRyeRestClientTemplate.java
index 3c6478e42aa..0597b2fe58a 100644
--- a/extensions/smallrye-rest-client/runtime/src/main/java/io/quarkus/smallrye/restclient/runtime/SmallRyeRestClientTemplate.java
+++ b/extensions/smallrye-rest-client/runtime/src/main/java/io/quarkus/smallrye/restclient/runtime/SmallRyeRestClientTemplate.java
@@ -16,16 +16,56 @@
 
 package io.quarkus.smallrye.restclient.runtime;
 
+import java.util.Set;
+
+import javax.ws.rs.RuntimeType;
+
 import org.eclipse.microprofile.rest.client.spi.RestClientBuilderResolver;
+import org.jboss.resteasy.core.providerfactory.ClientHelper;
+import org.jboss.resteasy.core.providerfactory.NOOPServerHelper;
+import org.jboss.resteasy.core.providerfactory.ResteasyProviderFactoryImpl;
+import org.jboss.resteasy.plugins.providers.RegisterBuiltin;
+import org.jboss.resteasy.spi.ResteasyProviderFactory;
 
 import io.quarkus.runtime.annotations.Template;
 
-/**
- * @author <a href="http://kenfinnigan.me">Ken Finnigan</a>
- */
 @Template
 public class SmallRyeRestClientTemplate {
     public void setRestClientBuilderResolver() {
         RestClientBuilderResolver.setInstance(new BuilderResolver());
     }
+
+    public void setSslEnabled(boolean sslEnabled) {
+        RestClientBuilderImpl.SSL_ENABLED = sslEnabled;
+    }
+
+    public void initializeResteasyProviderFactory(boolean useBuiltIn, Set<String> providersToRegister) {
+        ResteasyProviderFactory clientProviderFactory = new ResteasyProviderFactoryImpl(null, true) {
+            @Override
+            public RuntimeType getRuntimeType() {
+                return RuntimeType.CLIENT;
+            }
+
+            @Override
+            protected void initializeUtils() {
+                clientHelper = new ClientHelper(this);
+                serverHelper = NOOPServerHelper.INSTANCE;
+            }
+        };
+
+        if (useBuiltIn) {
+            RegisterBuiltin.register(clientProviderFactory);
+        }
+
+        for (String providerToRegister : providersToRegister) {
+            try {
+                clientProviderFactory
+                        .registerProvider(Thread.currentThread().getContextClassLoader().loadClass(providerToRegister.trim()));
+            } catch (ClassNotFoundException e) {
+                throw new RuntimeException("Unable to find class for provider " + providerToRegister, e);
+            }
+        }
+
+        RestClientBuilderImpl.PROVIDER_FACTORY = clientProviderFactory;
+    }
 }