diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/server/MavenWrapperSupport.kt b/plugins/maven/src/main/java/org/jetbrains/idea/maven/server/MavenWrapperSupport.kt index 36c658e75d6b9..2b76bf346da89 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/server/MavenWrapperSupport.kt +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/server/MavenWrapperSupport.kt @@ -5,6 +5,7 @@ import com.intellij.build.FilePosition import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.components.* import com.intellij.openapi.diagnostic.ControlFlowException +import com.intellij.openapi.externalSystem.util.environment.Environment import com.intellij.openapi.progress.ProgressIndicator import com.intellij.openapi.project.Project import com.intellij.openapi.util.io.FileUtil @@ -75,6 +76,14 @@ internal class MavenWrapperSupport { indicator?.apply { text = SyncBundle.message("maven.sync.wrapper.downloading.from", urlString) } try { HttpRequests.request(urlString) + .tuner{ + val username = Environment.getVariable("MVNW_USERNAME") + val password = Environment.getVariable("MVNW_PASSWORD") + if (!username.isNullOrBlank() && !password.isNullOrBlank()) { + indicator?.apply { text = SyncBundle.message("maven.sync.wrapper.downloading.auth") } + it.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString("$username:$password".toByteArray())) + } + } .forceHttps(false) .connectTimeout(30_000) .readTimeout(30_000) @@ -208,7 +217,14 @@ internal class MavenWrapperSupport { val stream = ByteArrayInputStream(wrapperProperties.contentsToByteArray(true)) properties.load(stream) - return properties.getProperty(DISTRIBUTION_URL_PROPERTY) + val configuredProperty = properties.getProperty(DISTRIBUTION_URL_PROPERTY) + val urlBase = Environment.getVariable("MVNW_REPOURL") + val configuredUrlBaseEnd = configuredProperty?.indexOf("/org/apache/maven") ?: -1 + if (!urlBase.isNullOrBlank() && configuredUrlBaseEnd >= 0) { + return (if (urlBase.endsWith('/')) urlBase.substring(0, urlBase.length - 1) else urlBase) + configuredProperty.substring(configuredUrlBaseEnd) + } + + return configuredProperty } catch (e: IOException) { MavenLog.LOG.warn("exception reading wrapper url", e) diff --git a/plugins/maven/src/main/resources/messages/MavenSyncBundle.properties b/plugins/maven/src/main/resources/messages/MavenSyncBundle.properties index bb9cc91bd140f..c3182814dd48b 100644 --- a/plugins/maven/src/main/resources/messages/MavenSyncBundle.properties +++ b/plugins/maven/src/main/resources/messages/MavenSyncBundle.properties @@ -62,6 +62,7 @@ cannot.download.zip.from=Cannot download ZIP distribution from {0}. Please check zip.is.not.correct=Zip archive at {0} is not correct Maven distribution. Please check distributionUrl maven.sync.wrapper.unpacking=Unpacking archive\u2026 maven.sync.wrapper.unpacked.into=Maven wrapper unpacked into {0} +maven.sync.wrapper.downloading.auth=Downloading Maven wrapper with Basic authentication maven.sync.wrapper.downloading.from=Downloading Maven wrapper from {0} maven.sync.wrapper.downloading.progress=Downloading Maven wrapper {0}/{1}% maven.sync.wrapper.downloading=Downloading Maven wrapper diff --git a/plugins/maven/src/test/java/org/jetbrains/idea/maven/server/MavenDistributionResolveTest.kt b/plugins/maven/src/test/java/org/jetbrains/idea/maven/server/MavenDistributionResolveTest.kt index 4dc516a220e44..ff81fad006e49 100644 --- a/plugins/maven/src/test/java/org/jetbrains/idea/maven/server/MavenDistributionResolveTest.kt +++ b/plugins/maven/src/test/java/org/jetbrains/idea/maven/server/MavenDistributionResolveTest.kt @@ -5,6 +5,8 @@ import com.intellij.build.SyncViewManager import com.intellij.build.events.BuildEvent import com.intellij.build.events.MessageEvent import com.intellij.maven.testFramework.MavenMultiVersionImportingTestCase +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.externalSystem.util.environment.Environment import com.intellij.testFramework.replaceService import com.intellij.util.ExceptionUtil import com.intellij.util.io.ZipUtil @@ -112,6 +114,40 @@ class MavenDistributionResolveTest : MavenMultiVersionImportingTestCase() { //assertContainsOnce { it.kind == MessageEvent.Kind.WARNING && it.description!= null && it.description!!.contains("is not correct maven home, reverting to embedded") } } + @Throws(IOException::class) + @Test fun testShouldUseSystemPropertyOverridesWhenDownloadingWrapper() = runBlocking { + runWithServer { url -> + + val envVariables = mapOf( + "MVNW_REPOURL" to url.substringBeforeLast("/"), + "MVNW_USERNAME" to "user_123", + "MVNW_PASSWORD" to "pass_abc" + ) + val environment = object : Environment { + override fun property(name: String): String? { + throw NotImplementedError() + } + + override fun variable(name: String): String? { + return envVariables[name] + } + } + + ApplicationManager.getApplication().replaceService(Environment::class.java, environment, testRootDisposable) + + createProjectPom("test" + + "project" + + "1") + createWrapperProperties("distributionUrl=https://something.com/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip") + MavenWorkspaceSettingsComponent.getInstance(project).settings.getGeneralSettings().mavenHomeType = MavenWrapper + importProjectAsync() + val connector = MavenServerManager.getInstance().getConnector(project, projectRoot.path) + assertTrue(connector.mavenDistribution.mavenHome.absolutePathString().contains("wrapper")) + assertNotContains { it.message.contains("something.com") } + assertContainsOnce { it.message == "Downloading Maven wrapper with Basic authentication\n" } + } + } + private inline fun runWithServer(test: (String) -> Unit) { val server = HttpServer.create()