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

refactoring to avoid usage of deprecated classes #226

Merged
merged 1 commit into from
Mar 6, 2024
Merged
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
16 changes: 5 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<maven.version>3.9.0</maven.version>
<maven-project.version>2.2.1</maven-project.version>
<maven.version>3.9.6</maven.version>
<maven-plugin.version>3.11.0</maven-plugin.version>

<jackson-bom.version>2.15.3</jackson-bom.version>
<httpclient.version>4.5.14</httpclient.version>
Expand Down Expand Up @@ -71,7 +71,7 @@
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>${maven.version}</version>
<version>${maven-plugin.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand All @@ -86,12 +86,6 @@
<version>${maven.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>${maven-project.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
Expand Down Expand Up @@ -162,7 +156,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>${maven.version}</version>
<version>${maven-plugin.version}</version>
<configuration>
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
<goalPrefix>helm</goalPrefix>
Expand Down Expand Up @@ -285,7 +279,7 @@
</roles>
</developer>
<developer>
<id>dwi-di</id>
<id>d-rk</id>
<name>Dirk Wilden</name>
<email>[email protected]</email>
<roles>
Expand Down
68 changes: 42 additions & 26 deletions src/main/kotlin/com/deviceinsight/helm/ResolveHelmMojo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@
package com.deviceinsight.helm

import com.deviceinsight.helm.util.PlatformDetector
import org.apache.maven.artifact.Artifact
import org.apache.maven.artifact.repository.ArtifactRepository
import org.apache.maven.artifact.resolver.ArtifactResolutionRequest
import org.apache.maven.artifact.resolver.ArtifactResolutionResult
import org.apache.maven.plugins.annotations.Component
import org.apache.maven.plugins.annotations.LifecyclePhase
import org.apache.maven.plugins.annotations.Mojo
import org.apache.maven.plugins.annotations.Parameter
import org.apache.maven.repository.RepositorySystem
import org.eclipse.aether.RepositorySystem
import org.eclipse.aether.RepositorySystemSession
import org.eclipse.aether.artifact.Artifact
import org.eclipse.aether.artifact.DefaultArtifact
import org.eclipse.aether.repository.RemoteRepository
import org.eclipse.aether.resolution.ArtifactRequest
import java.io.File
import java.net.HttpURLConnection
import java.net.URI
import java.net.URL
Expand All @@ -51,52 +53,66 @@ open class ResolveHelmMojo : AbstractHelmMojo() {
@Parameter(property = "helmDownloadUrl", defaultValue = "https://get.helm.sh/")
private lateinit var helmDownloadUrl: URI

@Parameter(readonly = true, required = true, defaultValue = "\${localRepository}")
private lateinit var localRepository: ArtifactRepository
@Parameter(readonly = true, required = true, defaultValue = "\${repositorySystemSession}")
private lateinit var repositorySession: RepositorySystemSession

@Parameter(readonly = true, required = true, defaultValue = "\${project.remoteArtifactRepositories}")
private lateinit var remoteRepositories: List<ArtifactRepository>
@Parameter(readonly = true, required = true, defaultValue = "\${project.remoteProjectRepositories}")
private lateinit var remoteRepositories: List<RemoteRepository>

@Component
private lateinit var repositorySystem: RepositorySystem

protected lateinit var helm : String
protected lateinit var helm: String

protected fun resolveHelmBinary(): String {

val platformIdentifier = PlatformDetector.detectHelmReleasePlatformIdentifier()
val helmArtifact: Artifact =
repositorySystem.createArtifactWithClassifier(helmGroupId, helmArtifactId, helmVersion, "binary",
platformIdentifier)
val helmArtifact: Artifact = DefaultArtifact(helmGroupId, helmArtifactId, platformIdentifier, "binary", helmVersion)

val request = ArtifactResolutionRequest()
request.artifact = helmArtifact
request.isResolveTransitively = false
request.localRepository = localRepository
request.remoteRepositories = remoteRepositories
var resolvedArtifact = findArtifact(helmArtifact)

val resolutionResult: ArtifactResolutionResult = repositorySystem.resolve(request)

if (!resolutionResult.isSuccess) {
if (resolvedArtifact == null) {
log.info("Artifact not found in remote repositories")
downloadAndInstallHelm(helmArtifact, platformIdentifier)
resolvedArtifact = downloadAndInstallHelm(helmArtifact, platformIdentifier)
}

helmArtifact.file.setExecutable(true)
resolvedArtifact.file.setExecutable(true)

return resolvedArtifact.file.absolutePath
}

private fun findArtifact(helmArtifact: Artifact): Artifact? {

return helmArtifact.file.absolutePath
try {
val request = ArtifactRequest()
request.setArtifact(helmArtifact)
request.setRepositories(remoteRepositories)
val resolutionResult = repositorySystem.resolveArtifact(repositorySession, request)

return if (resolutionResult.isResolved) {
resolutionResult.artifact
} else {
null
}

} catch (e: Exception) {
log.debug("failed to resolve artifact: ${e.message}")
return null
}
}

private fun downloadAndInstallHelm(artifact: Artifact, platformIdentifier: String) {
private fun downloadAndInstallHelm(artifact: Artifact, platformIdentifier: String): Artifact {

val fileName = "helm-v$helmVersion-$platformIdentifier"

val targetFile = artifact.file.toPath()
val artifactPath = repositorySession.localRepositoryManager.getPathForLocalArtifact(artifact)
val targetFile = File(repositorySession.localRepository.basedir, artifactPath).toPath()
Files.createDirectories(targetFile.parent)

val url = helmDownloadUrl.resolve("./$fileName.zip").toURL()

downloadFileAndExtractBinary(url, targetFile)
return artifact.setFile(targetFile.toFile())
}

private fun downloadFileAndExtractBinary(url: URL, destination: Path) {
Expand Down
Loading