Skip to content

Commit

Permalink
IDEA-311825 Support MVNW_PASSWORD, MVNW_REPOURL, and MVNW_USERNAME fo…
Browse files Browse the repository at this point in the history
…r downloading Maven wrapper
  • Loading branch information
nrayburn-tech committed Dec 18, 2024
1 parent b68fd84 commit e32da48
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -112,6 +114,40 @@ class MavenDistributionResolveTest : MavenMultiVersionImportingTestCase() {
//assertContainsOnce<MessageEvent> { 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("<groupId>test</groupId>" +
"<artifactId>project</artifactId>" +
"<version>1</version>")
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<BuildEvent> { it.message.contains("something.com") }
assertContainsOnce<BuildEvent> { it.message == "Downloading Maven wrapper with Basic authentication\n" }
}
}


private inline fun runWithServer(test: (String) -> Unit) {
val server = HttpServer.create()
Expand Down

0 comments on commit e32da48

Please sign in to comment.