├── .mvn └── wrapper │ ├── maven-wrapper.jar │ └── maven-wrapper.properties ├── src ├── main │ ├── resources │ │ └── application.properties │ └── java │ │ └── com │ │ └── bytesville │ │ └── customhttpclient │ │ ├── HttpClientDemo.java │ │ ├── OkHttpClientFactoryImpl.java │ │ ├── ApacheHttpClientFactoryImpl.java │ │ ├── KeepAliveConfig.java │ │ ├── ApiRestController.java │ │ ├── IdleConnectionMonitorThread.java │ │ ├── RestTemplateConfig.java │ │ └── RequestFactoryConfig.java └── test │ └── java │ └── com │ └── bytesville │ └── customhttpclient │ └── HttpClientDemoTests.java ├── .gitignore ├── .github └── dependabot.yml ├── readme.adoc ├── pom.xml ├── mvnw.cmd └── mvnw /.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zeagord/SpringCustomHttpClient/HEAD/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.3/apache-maven-3.5.3-bin.zip 2 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.tomcat.max-threads=400 2 | server.tomcat.min-spare-threads=50 3 | server.port: 8081 4 | management.endpoints.web.exposure.include=* 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ -------------------------------------------------------------------------------- /src/main/java/com/bytesville/customhttpclient/HttpClientDemo.java: -------------------------------------------------------------------------------- 1 | package com.bytesville.customhttpclient; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class HttpClientDemo { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(HttpClientDemo.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/test/java/com/bytesville/customhttpclient/HttpClientDemoTests.java: -------------------------------------------------------------------------------- 1 | package com.bytesville.customhttpclient; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class HttpClientDemoTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: maven 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | time: "21:00" 8 | open-pull-requests-limit: 10 9 | ignore: 10 | - dependency-name: org.springframework.boot:spring-boot-starter-parent 11 | versions: 12 | - 2.4.2 13 | - 2.4.3 14 | - 2.4.4 15 | - dependency-name: org.springframework.cloud:spring-cloud-commons 16 | versions: 17 | - 3.0.1 18 | - dependency-name: com.squareup.okhttp3:okhttp 19 | versions: 20 | - 4.9.0 21 | -------------------------------------------------------------------------------- /src/main/java/com/bytesville/customhttpclient/OkHttpClientFactoryImpl.java: -------------------------------------------------------------------------------- 1 | package com.bytesville.customhttpclient; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | import okhttp3.ConnectionPool; 5 | import okhttp3.ConnectionSpec; 6 | import okhttp3.OkHttpClient; 7 | import org.springframework.cloud.commons.httpclient.OkHttpClientFactory; 8 | 9 | public class OkHttpClientFactoryImpl implements OkHttpClientFactory { 10 | @Override public OkHttpClient.Builder createBuilder(boolean disableSslValidation) { 11 | OkHttpClient.Builder builder = new OkHttpClient.Builder(); 12 | ConnectionPool okHttpConnectionPool = new ConnectionPool(50, 30, TimeUnit.SECONDS); 13 | builder.connectionPool(okHttpConnectionPool); 14 | builder.connectTimeout(20, TimeUnit.SECONDS); 15 | builder.retryOnConnectionFailure(false); 16 | return builder; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/bytesville/customhttpclient/ApacheHttpClientFactoryImpl.java: -------------------------------------------------------------------------------- 1 | package com.bytesville.customhttpclient; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | import org.apache.http.client.config.RequestConfig; 5 | import org.apache.http.conn.ConnectionKeepAliveStrategy; 6 | import org.apache.http.impl.client.HttpClientBuilder; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.cloud.commons.httpclient.ApacheHttpClientFactory; 9 | import org.springframework.context.annotation.Configuration; 10 | 11 | public class ApacheHttpClientFactoryImpl implements ApacheHttpClientFactory { 12 | 13 | @Autowired 14 | private ConnectionKeepAliveStrategy connectionKeepAliveStrategy; 15 | 16 | @Override public HttpClientBuilder createBuilder() { 17 | RequestConfig requestConfig = RequestConfig 18 | .custom() 19 | .setConnectionRequestTimeout(20000) 20 | .setSocketTimeout(30000) 21 | .build(); 22 | 23 | return HttpClientBuilder 24 | .create() 25 | .setMaxConnTotal(400) 26 | .setMaxConnPerRoute(200) 27 | .setKeepAliveStrategy(connectionKeepAliveStrategy) 28 | .evictIdleConnections(30, TimeUnit.SECONDS) 29 | .setDefaultRequestConfig(requestConfig); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/bytesville/customhttpclient/KeepAliveConfig.java: -------------------------------------------------------------------------------- 1 | package com.bytesville.customhttpclient; 2 | 3 | import org.apache.http.HeaderElement; 4 | import org.apache.http.HeaderElementIterator; 5 | import org.apache.http.conn.ConnectionKeepAliveStrategy; 6 | import org.apache.http.message.BasicHeaderElementIterator; 7 | import org.apache.http.protocol.HTTP; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.Configuration; 10 | 11 | @Configuration 12 | public class KeepAliveConfig { 13 | @Bean ConnectionKeepAliveStrategy connectionKeepAliveStrategy(){ 14 | return (response, context) -> { 15 | HeaderElementIterator it = new BasicHeaderElementIterator( 16 | response.headerIterator(HTTP.CONN_KEEP_ALIVE)); 17 | while (it.hasNext()){ 18 | HeaderElement he = it.nextElement(); 19 | String param = he.getName(); 20 | String value = he.getValue(); 21 | if (value != null && param.equalsIgnoreCase("timeout")) { 22 | try { 23 | return Long.parseLong(value) * 1000; 24 | } catch(NumberFormatException exception) { 25 | exception.printStackTrace(); 26 | } 27 | } 28 | } 29 | // If there is no Keep-Alive header. Keep the connection for 30 seconds 30 | return 30*1000; 31 | }; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/bytesville/customhttpclient/ApiRestController.java: -------------------------------------------------------------------------------- 1 | package com.bytesville.customhttpclient; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.beans.factory.annotation.Qualifier; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | import org.springframework.web.client.RestTemplate; 8 | 9 | @RestController 10 | public class ApiRestController { 11 | 12 | @Autowired(required = false) @Qualifier("defaultRestTemplate") 13 | RestTemplate restTemplate; 14 | 15 | @Autowired(required = false) @Qualifier("apacheRestTemplate") 16 | RestTemplate apacheRestTemplate; 17 | 18 | @Autowired(required = false) @Qualifier("apacheSpringCommonsRestTemplate") 19 | RestTemplate apacheSpringCommonsRestTemplate; 20 | 21 | @Autowired(required = false) @Qualifier("OKSpringCommonsRestTemplate") 22 | RestTemplate okRestTemplate; 23 | 24 | @GetMapping("/default") 25 | public String getDefault() { 26 | return restTemplate.getForObject("http://httpbin.org/anything", String.class); 27 | } 28 | 29 | @GetMapping("/apache") 30 | public String getApache() { 31 | return apacheRestTemplate.getForObject("http://httpbin.org/anything", String.class); 32 | } 33 | 34 | @GetMapping("/apachespring") 35 | public String getApacheSpring() { 36 | return apacheSpringCommonsRestTemplate.getForObject("http://httpbin.org/anything", String.class); 37 | } 38 | @GetMapping("/ok") 39 | public String getOk() { 40 | return okRestTemplate.getForObject("http://httpbin.org/anything", String.class); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/bytesville/customhttpclient/IdleConnectionMonitorThread.java: -------------------------------------------------------------------------------- 1 | package com.bytesville.customhttpclient; 2 | 3 | import io.micrometer.core.instrument.Gauge; 4 | import io.micrometer.core.instrument.MeterRegistry; 5 | import java.util.concurrent.TimeUnit; 6 | import java.util.concurrent.atomic.AtomicInteger; 7 | import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.context.annotation.Configuration; 12 | 13 | public class IdleConnectionMonitorThread extends Thread { 14 | private static final Logger log = LoggerFactory.getLogger(IdleConnectionMonitorThread.class); 15 | private final PoolingHttpClientConnectionManager connMgr; 16 | private volatile boolean shutdown; 17 | private AtomicInteger availableConnections; 18 | private MeterRegistry registry; 19 | 20 | public IdleConnectionMonitorThread(PoolingHttpClientConnectionManager connMgr, MeterRegistry registry) { 21 | super(); 22 | this.connMgr = connMgr; 23 | this.registry = registry; 24 | this.availableConnections = this.registry.gauge("availableConnections", new AtomicInteger(0)); 25 | } 26 | @Override 27 | public void run() { 28 | try { 29 | while (!shutdown) { 30 | synchronized (this) { 31 | wait(1000); 32 | log.info(connMgr.getTotalStats().toString()); 33 | availableConnections.set(connMgr.getTotalStats().getAvailable()); 34 | connMgr.closeExpiredConnections(); 35 | connMgr.closeIdleConnections(30, TimeUnit.SECONDS); 36 | } 37 | } 38 | } catch (InterruptedException ex) { 39 | shutdown(); 40 | } 41 | } 42 | public void shutdown() { 43 | shutdown = true; 44 | synchronized (this) { 45 | notifyAll(); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/bytesville/customhttpclient/RestTemplateConfig.java: -------------------------------------------------------------------------------- 1 | package com.bytesville.customhttpclient; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.beans.factory.annotation.Qualifier; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | import org.springframework.context.annotation.Primary; 8 | import org.springframework.http.client.ClientHttpRequestFactory; 9 | import org.springframework.web.client.RestTemplate; 10 | 11 | /*** 12 | * RestTemplate to use the Apache HttpClient rather than JDK one. This one will set only Max Connection and Max per Route 13 | * For advanced Operation, refer AdvancedRestTemplate 14 | */ 15 | @Configuration 16 | public class RestTemplateConfig { 17 | 18 | @Autowired @Qualifier("apacheRestTemplate") 19 | ClientHttpRequestFactory customRequestFactory; 20 | 21 | @Autowired @Qualifier("apacheSpringCommonsRestTemplate") 22 | ClientHttpRequestFactory apacheHttpRequestFactory; 23 | 24 | @Autowired @Qualifier("OKSpringCommonsRestTemplate") 25 | ClientHttpRequestFactory okHttpRequestFactory; 26 | 27 | @Bean 28 | @Qualifier("defaultRestTemplate") 29 | @Primary 30 | public RestTemplate defaultRestTemplate(){ 31 | return new RestTemplate(); 32 | } 33 | 34 | @Bean 35 | @Qualifier("apacheRestTemplate") 36 | public RestTemplate createCustomRestTemplate(){ 37 | RestTemplate restTemplate = new RestTemplate(); 38 | restTemplate.setRequestFactory(customRequestFactory); 39 | return restTemplate; 40 | } 41 | 42 | @Bean 43 | @Qualifier("apacheSpringCommonsRestTemplate") 44 | public RestTemplate createApacheCustomRestTemplate() { 45 | RestTemplate restTemplate = new RestTemplate(); 46 | restTemplate.setRequestFactory(apacheHttpRequestFactory); 47 | return restTemplate; 48 | } 49 | 50 | @Bean 51 | @Qualifier("OKSpringCommonsRestTemplate") 52 | public RestTemplate createOKCustomRestTemplate() { 53 | RestTemplate restTemplate = new RestTemplate(); 54 | restTemplate.setRequestFactory(okHttpRequestFactory); 55 | return restTemplate; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /readme.adoc: -------------------------------------------------------------------------------- 1 | # Spring CustomHttpClient for RestTemplate 2 | 3 | Code base for the blog post link:https://www.bytesville.com/changing-httpclient-in-spring-resttemplate/[] 4 | 5 | ## Benchmark Results of Http Client Tuning 6 | 7 | ### JDK Client 8 | ---- 9 | $ wrk -d 60 -t 200 -c 300 http://localhost:8081/default 10 | Running 1m test @ http://localhost:8081/default 11 | 200 threads and 300 connections 12 | Thread Stats Avg Stdev Max +/- Stdev 13 | Latency 428.28ms 197.76ms 1.99s 90.31% 14 | Req/Sec 2.23 0.79 5.00 83.94% 15 | 28137 requests in 1.00m, 12.67MB read 16 | Socket errors: connect 0, read 0, write 0, timeout 41 17 | Requests/sec: 468.06 18 | Transfer/sec: 215.81KB 19 | ---- 20 | ### Apache Client 21 | ---- 22 | $ wrk -d 60 -t 200 -c 300 http://localhost:8081/apache 23 | Running 1m test @ http://localhost:8081/apache 24 | 200 threads and 300 connections 25 | Thread Stats Avg Stdev Max +/- Stdev 26 | Latency 419.24ms 193.38ms 2.00s 88.88% 27 | Req/Sec 2.32 0.79 5.00 85.59% 28 | 28635 requests in 1.00m, 12.89MB read 29 | Socket errors: connect 0, read 0, write 0, timeout 41 30 | Requests/sec: 476.37 31 | Transfer/sec: 219.64KB 32 | ---- 33 | ### Apache Client via Spring Cloud Commons 34 | ---- 35 | $ wrk -d 60 -t 200 -c 300 http://localhost:8081/apachespring 36 | Running 1m test @ http://localhost:8081/apachespring 37 | 200 threads and 300 connections 38 | Thread Stats Avg Stdev Max +/- Stdev 39 | Latency 423.83ms 215.93ms 2.00s 89.00% 40 | Req/Sec 2.35 0.83 5.00 83.71% 41 | 28460 requests in 1.00m, 12.81MB read 42 | Socket errors: connect 0, read 0, write 0, timeout 56 43 | Non-2xx or 3xx responses: 1 44 | Requests/sec: 473.47 45 | Transfer/sec: 218.30KB 46 | ---- 47 | ### OkHttpClient via Spring Cloud Commons 48 | ---- 49 | $ wrk -d 60 -t 200 -c 300 http://localhost:8081/ok 50 | Running 1m test @ http://localhost:8081/ok 51 | 200 threads and 300 connections 52 | Thread Stats Avg Stdev Max +/- Stdev 53 | Latency 377.16ms 175.46ms 1.99s 91.24% 54 | Req/Sec 2.64 0.73 5.00 88.59% 55 | 32005 requests in 1.00m, 14.41MB read 56 | Socket errors: connect 0, read 0, write 0, timeout 50 57 | Non-2xx or 3xx responses: 3 58 | Requests/sec: 531.75 59 | Transfer/sec: 245.16KB 60 | ---- 61 | -------------------------------------------------------------------------------- /src/main/java/com/bytesville/customhttpclient/RequestFactoryConfig.java: -------------------------------------------------------------------------------- 1 | package com.bytesville.customhttpclient; 2 | 3 | import io.micrometer.core.instrument.MeterRegistry; 4 | import okhttp3.OkHttpClient; 5 | import org.apache.http.client.HttpClient; 6 | import org.apache.http.client.config.RequestConfig; 7 | import org.apache.http.conn.ConnectionKeepAliveStrategy; 8 | import org.apache.http.impl.client.CloseableHttpClient; 9 | import org.apache.http.impl.client.HttpClients; 10 | import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; 11 | import org.springframework.beans.factory.annotation.Qualifier; 12 | import org.springframework.context.annotation.Bean; 13 | import org.springframework.context.annotation.Configuration; 14 | import org.springframework.http.client.ClientHttpRequestFactory; 15 | import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; 16 | import org.springframework.http.client.OkHttp3ClientHttpRequestFactory; 17 | 18 | @Configuration 19 | public class RequestFactoryConfig { 20 | 21 | private final ConnectionKeepAliveStrategy connectionKeepAliveStrategy; 22 | private final MeterRegistry registry; 23 | RequestFactoryConfig(ConnectionKeepAliveStrategy connectionKeepAliveStrategy, MeterRegistry registry){ 24 | this.connectionKeepAliveStrategy = connectionKeepAliveStrategy; 25 | this.registry = registry; 26 | } 27 | 28 | @Bean 29 | @Qualifier("apacheRestTemplate") 30 | public ClientHttpRequestFactory createRequestFactory() throws InterruptedException { 31 | PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); 32 | connectionManager.setMaxTotal(400); 33 | connectionManager.setDefaultMaxPerRoute(200); 34 | IdleConnectionMonitorThread 35 | connectionMonitor = new IdleConnectionMonitorThread(connectionManager, registry); 36 | connectionMonitor.start(); 37 | connectionMonitor.join(2000); 38 | RequestConfig requestConfig = RequestConfig 39 | .custom() 40 | .setConnectionRequestTimeout(5000) 41 | .setSocketTimeout(10000) 42 | .build(); 43 | 44 | CloseableHttpClient httpClient = HttpClients 45 | .custom() 46 | .setConnectionManager(connectionManager) 47 | .setKeepAliveStrategy(connectionKeepAliveStrategy) 48 | .setDefaultRequestConfig(requestConfig) 49 | .build(); 50 | return new HttpComponentsClientHttpRequestFactory(httpClient); 51 | } 52 | 53 | @Bean 54 | @Qualifier("apacheSpringCommonsRestTemplate") 55 | public ClientHttpRequestFactory createCommonsRequestFactory() { 56 | ApacheHttpClientFactoryImpl httpClientFactory = new ApacheHttpClientFactoryImpl(); 57 | HttpClient client = httpClientFactory.createBuilder().build(); 58 | return new HttpComponentsClientHttpRequestFactory(client); 59 | } 60 | @Bean 61 | @Qualifier("OKSpringCommonsRestTemplate") 62 | public ClientHttpRequestFactory createOKCommonsRequestFactory() { 63 | OkHttpClientFactoryImpl httpClientFactory= new OkHttpClientFactoryImpl(); 64 | OkHttpClient client = httpClientFactory.createBuilder(false).build(); 65 | return new OkHttp3ClientHttpRequestFactory(client); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.bytesville. 7 | customhttpclient 8 | 0.0.1 9 | jar 10 | 11 | customhttpclient 12 | Demo project for Apache Http Client with Rest Template 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 2.5.3 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-web 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-actuator 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-test 39 | test 40 | 41 | 42 | org.springframework.cloud 43 | spring-cloud-commons 44 | 3.0.3 45 | 46 | 47 | org.apache.httpcomponents 48 | httpclient 49 | 4.5.13 50 | 51 | 52 | com.squareup.okhttp3 53 | okhttp 54 | 4.9.3 55 | 56 | 57 | io.micrometer 58 | micrometer-registry-prometheus 59 | 60 | 61 | 62 | 63 | 64 | 65 | org.springframework.boot 66 | spring-boot-maven-plugin 67 | 68 | 69 | 70 | 71 | 72 | 73 | spring-snapshots 74 | Spring Snapshots 75 | https://repo.spring.io/snapshot 76 | 77 | true 78 | 79 | 80 | 81 | spring-milestones 82 | Spring Milestones 83 | https://repo.spring.io/milestone 84 | 85 | false 86 | 87 | 88 | 89 | 90 | 91 | 92 | spring-snapshots 93 | Spring Snapshots 94 | https://repo.spring.io/snapshot 95 | 96 | true 97 | 98 | 99 | 100 | spring-milestones 101 | Spring Milestones 102 | https://repo.spring.io/milestone 103 | 104 | false 105 | 106 | 107 | 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM http://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven2 Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' 39 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 40 | 41 | @REM set %HOME% to equivalent of $HOME 42 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 43 | 44 | @REM Execute a user defined script before this one 45 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 46 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 47 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" 48 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" 49 | :skipRcPre 50 | 51 | @setlocal 52 | 53 | set ERROR_CODE=0 54 | 55 | @REM To isolate internal variables from possible post scripts, we use another setlocal 56 | @setlocal 57 | 58 | @REM ==== START VALIDATION ==== 59 | if not "%JAVA_HOME%" == "" goto OkJHome 60 | 61 | echo. 62 | echo Error: JAVA_HOME not found in your environment. >&2 63 | echo Please set the JAVA_HOME variable in your environment to match the >&2 64 | echo location of your Java installation. >&2 65 | echo. 66 | goto error 67 | 68 | :OkJHome 69 | if exist "%JAVA_HOME%\bin\java.exe" goto init 70 | 71 | echo. 72 | echo Error: JAVA_HOME is set to an invalid directory. >&2 73 | echo JAVA_HOME = "%JAVA_HOME%" >&2 74 | echo Please set the JAVA_HOME variable in your environment to match the >&2 75 | echo location of your Java installation. >&2 76 | echo. 77 | goto error 78 | 79 | @REM ==== END VALIDATION ==== 80 | 81 | :init 82 | 83 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 84 | @REM Fallback to current working directory if not found. 85 | 86 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 87 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 88 | 89 | set EXEC_DIR=%CD% 90 | set WDIR=%EXEC_DIR% 91 | :findBaseDir 92 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 93 | cd .. 94 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 95 | set WDIR=%CD% 96 | goto findBaseDir 97 | 98 | :baseDirFound 99 | set MAVEN_PROJECTBASEDIR=%WDIR% 100 | cd "%EXEC_DIR%" 101 | goto endDetectBaseDir 102 | 103 | :baseDirNotFound 104 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 105 | cd "%EXEC_DIR%" 106 | 107 | :endDetectBaseDir 108 | 109 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 110 | 111 | @setlocal EnableExtensions EnableDelayedExpansion 112 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 113 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 114 | 115 | :endReadAdditionalConfig 116 | 117 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 118 | 119 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" 120 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 121 | 122 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 123 | if ERRORLEVEL 1 goto error 124 | goto end 125 | 126 | :error 127 | set ERROR_CODE=1 128 | 129 | :end 130 | @endlocal & set ERROR_CODE=%ERROR_CODE% 131 | 132 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost 133 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 134 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" 135 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" 136 | :skipRcPost 137 | 138 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 139 | if "%MAVEN_BATCH_PAUSE%" == "on" pause 140 | 141 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% 142 | 143 | exit /B %ERROR_CODE% 144 | -------------------------------------------------------------------------------- /mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven2 Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /etc/mavenrc ] ; then 40 | . /etc/mavenrc 41 | fi 42 | 43 | if [ -f "$HOME/.mavenrc" ] ; then 44 | . "$HOME/.mavenrc" 45 | fi 46 | 47 | fi 48 | 49 | # OS specific support. $var _must_ be set to either true or false. 50 | cygwin=false; 51 | darwin=false; 52 | mingw=false 53 | case "`uname`" in 54 | CYGWIN*) cygwin=true ;; 55 | MINGW*) mingw=true;; 56 | Darwin*) darwin=true 57 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home 58 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html 59 | if [ -z "$JAVA_HOME" ]; then 60 | if [ -x "/usr/libexec/java_home" ]; then 61 | export JAVA_HOME="`/usr/libexec/java_home`" 62 | else 63 | export JAVA_HOME="/Library/Java/Home" 64 | fi 65 | fi 66 | ;; 67 | esac 68 | 69 | if [ -z "$JAVA_HOME" ] ; then 70 | if [ -r /etc/gentoo-release ] ; then 71 | JAVA_HOME=`java-config --jre-home` 72 | fi 73 | fi 74 | 75 | if [ -z "$M2_HOME" ] ; then 76 | ## resolve links - $0 may be a link to maven's home 77 | PRG="$0" 78 | 79 | # need this for relative symlinks 80 | while [ -h "$PRG" ] ; do 81 | ls=`ls -ld "$PRG"` 82 | link=`expr "$ls" : '.*-> \(.*\)$'` 83 | if expr "$link" : '/.*' > /dev/null; then 84 | PRG="$link" 85 | else 86 | PRG="`dirname "$PRG"`/$link" 87 | fi 88 | done 89 | 90 | saveddir=`pwd` 91 | 92 | M2_HOME=`dirname "$PRG"`/.. 93 | 94 | # make it fully qualified 95 | M2_HOME=`cd "$M2_HOME" && pwd` 96 | 97 | cd "$saveddir" 98 | # echo Using m2 at $M2_HOME 99 | fi 100 | 101 | # For Cygwin, ensure paths are in UNIX format before anything is touched 102 | if $cygwin ; then 103 | [ -n "$M2_HOME" ] && 104 | M2_HOME=`cygpath --unix "$M2_HOME"` 105 | [ -n "$JAVA_HOME" ] && 106 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 107 | [ -n "$CLASSPATH" ] && 108 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 109 | fi 110 | 111 | # For Migwn, ensure paths are in UNIX format before anything is touched 112 | if $mingw ; then 113 | [ -n "$M2_HOME" ] && 114 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 115 | [ -n "$JAVA_HOME" ] && 116 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 117 | # TODO classpath? 118 | fi 119 | 120 | if [ -z "$JAVA_HOME" ]; then 121 | javaExecutable="`which javac`" 122 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 123 | # readlink(1) is not available as standard on Solaris 10. 124 | readLink=`which readlink` 125 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 126 | if $darwin ; then 127 | javaHome="`dirname \"$javaExecutable\"`" 128 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 129 | else 130 | javaExecutable="`readlink -f \"$javaExecutable\"`" 131 | fi 132 | javaHome="`dirname \"$javaExecutable\"`" 133 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 134 | JAVA_HOME="$javaHome" 135 | export JAVA_HOME 136 | fi 137 | fi 138 | fi 139 | 140 | if [ -z "$JAVACMD" ] ; then 141 | if [ -n "$JAVA_HOME" ] ; then 142 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 143 | # IBM's JDK on AIX uses strange locations for the executables 144 | JAVACMD="$JAVA_HOME/jre/sh/java" 145 | else 146 | JAVACMD="$JAVA_HOME/bin/java" 147 | fi 148 | else 149 | JAVACMD="`which java`" 150 | fi 151 | fi 152 | 153 | if [ ! -x "$JAVACMD" ] ; then 154 | echo "Error: JAVA_HOME is not defined correctly." >&2 155 | echo " We cannot execute $JAVACMD" >&2 156 | exit 1 157 | fi 158 | 159 | if [ -z "$JAVA_HOME" ] ; then 160 | echo "Warning: JAVA_HOME environment variable is not set." 161 | fi 162 | 163 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 164 | 165 | # traverses directory structure from process work directory to filesystem root 166 | # first directory with .mvn subdirectory is considered project base directory 167 | find_maven_basedir() { 168 | 169 | if [ -z "$1" ] 170 | then 171 | echo "Path not specified to find_maven_basedir" 172 | return 1 173 | fi 174 | 175 | basedir="$1" 176 | wdir="$1" 177 | while [ "$wdir" != '/' ] ; do 178 | if [ -d "$wdir"/.mvn ] ; then 179 | basedir=$wdir 180 | break 181 | fi 182 | # workaround for JBEAP-8937 (on Solaris 10/Sparc) 183 | if [ -d "${wdir}" ]; then 184 | wdir=`cd "$wdir/.."; pwd` 185 | fi 186 | # end of workaround 187 | done 188 | echo "${basedir}" 189 | } 190 | 191 | # concatenates all lines of a file 192 | concat_lines() { 193 | if [ -f "$1" ]; then 194 | echo "$(tr -s '\n' ' ' < "$1")" 195 | fi 196 | } 197 | 198 | BASE_DIR=`find_maven_basedir "$(pwd)"` 199 | if [ -z "$BASE_DIR" ]; then 200 | exit 1; 201 | fi 202 | 203 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} 204 | echo $MAVEN_PROJECTBASEDIR 205 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 206 | 207 | # For Cygwin, switch paths to Windows format before running java 208 | if $cygwin; then 209 | [ -n "$M2_HOME" ] && 210 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 211 | [ -n "$JAVA_HOME" ] && 212 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 213 | [ -n "$CLASSPATH" ] && 214 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 215 | [ -n "$MAVEN_PROJECTBASEDIR" ] && 216 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` 217 | fi 218 | 219 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 220 | 221 | exec "$JAVACMD" \ 222 | $MAVEN_OPTS \ 223 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 224 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 225 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 226 | --------------------------------------------------------------------------------