Skip to content

Commit

Permalink
✨ add ability to pass maven coordinate in location
Browse files Browse the repository at this point in the history
Signed-off-by: Pranav Gaikwad <pgaikwad@redhat.com>
  • Loading branch information
pranavgaikwad committed Jun 11, 2024
1 parent bc4ff29 commit 13db4c9
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 2 deletions.
9 changes: 9 additions & 0 deletions demo-dep-output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2473,3 +2473,12 @@
- konveyor.io/dep-source=open-source
- konveyor.io/language=java
prefix: file:///root/.m2/repository/org/yaml/snakeyaml/1.30
- fileURI: io/javaoperatorsdk/operator/sample
provider: java
dependencies:
- name: io.javaoperatorsdk.operator.sample
version: 0.0.0
labels:
- konveyor.io/dep-source=open-source
- konveyor.io/language=java
prefix: file://java-project/src/main/io/javaoperatorsdk/operator/sample
3 changes: 2 additions & 1 deletion demo-output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- Backend=Golang
- Infra=Kubernetes
- Java
- Java Operator SDK
- Language=Golang
- License=Apache
violations:
Expand Down Expand Up @@ -907,7 +908,7 @@
errors:
error-rule-001: |-
unable to get query info: yaml: unmarshal errors:
line 9: cannot unmarshal !!map into string
line 10: cannot unmarshal !!map into string
unmatched:
- file-002
- lang-ref-002
Expand Down
2 changes: 1 addition & 1 deletion docs/providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Here's an example config for `java` provider that is currently in-tree and does
}
```

The `location` can be a path to the application's source code or to a binary JAR, WAR, or EAR file.
The `location` can be a path to the application's source code or to a binary JAR, WAR, or EAR file. Optionally, coordinates to a maven artifact can be provided as input in the format `maven://<group-id>.<artifact-id>:<version>:<classifier>:<path>`. The field `<path>` is optional, it specifies a local path where the artifact will be downloaded. If not specified, provider will use a temporary directory to download it.

The `java` provider also takes following options in `providerSpecificConfig`:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
WebArchive = ".war"
EnterpriseArchive = ".ear"
ClassFile = ".class"
MvnURIPrefix = "mvn://"
)

// provider specific config keys
Expand Down Expand Up @@ -220,6 +221,42 @@ func (p *javaProvider) Init(ctx context.Context, log logr.Logger, config provide
var returnErr error
// each service client should have their own context
ctx, cancelFunc := context.WithCancel(ctx)
// location can be a coordinate to a remote mvn artifact
if strings.HasPrefix(config.Location, MvnURIPrefix) {
mvnUri := strings.Replace(config.Location, MvnURIPrefix, "", 1)
// URI format is <group>:<artifact>:<version>:<classifier>:<path>
// <path> is optional & points to a local path where it will be downloaded
mvnUriParts := strings.Split(mvnUri, ":")
if len(mvnUriParts) < 4 || len(mvnUriParts) > 6 {
cancelFunc()
return nil, fmt.Errorf("invalid maven coordinates in location %s", config.Location)
}
outputDir := "."
if len(mvnUriParts) == 5 {
if stat, err := os.Stat(mvnUriParts[len(mvnUriParts)-1]); err != nil || !stat.IsDir() {
cancelFunc()
return nil, fmt.Errorf("output path does not exist or not a directory")
}
outputDir = mvnUriParts[len(mvnUriParts)-1]
}
mvnOptions := []string{
"dependency:copy",
fmt.Sprintf("-Dartifact=%s", strings.Join(mvnUriParts[:4], ":")),
fmt.Sprintf("-DoutputDirectory=%s", outputDir),
}
if mavenSettingsFile != "" {
mvnOptions = append(mvnOptions, "-s", mavenSettingsFile)
}
cmd := exec.CommandContext(ctx, "mvn", mvnOptions...)
cmd.Dir = outputDir
if err := cmd.Run(); err != nil {
cancelFunc()
return nil, fmt.Errorf("error downloading java artifact - %s", mvnUri)
}
config.Location = filepath.Join(outputDir,
fmt.Sprintf("%s.%s", strings.Join(mvnUriParts[1:3], "-"), strings.ToLower(mvnUriParts[3])))
}

extension := strings.ToLower(path.Ext(config.Location))
switch extension {
case JavaArchive, WebArchive, EnterpriseArchive:
Expand Down
10 changes: 10 additions & 0 deletions provider_container_settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@
"bundles": "/jdtls/java-analyzer-bundle/java-analyzer-bundle.core/target/java-analyzer-bundle.core-1.0.0-SNAPSHOT.jar"
},
"analysisMode": "source-only"
},
{
"location": "mvn://io.javaoperatorsdk:quarkus:1.6.2:jar",
"providerSpecificConfig": {
"lspServerName": "java",
"lspServerPath": "/jdtls/bin/jdtls",
"depOpenSourceLabelsFile": "/usr/local/etc/maven.default.index",
"bundles": "/jdtls/java-analyzer-bundle/java-analyzer-bundle.core/target/java-analyzer-bundle.core-1.0.0-SNAPSHOT.jar"
},
"analysisMode": "full"
}
]
},
Expand Down
10 changes: 10 additions & 0 deletions provider_local_external_images.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@
"bundles": "/jdtls/java-analyzer-bundle/java-analyzer-bundle.core/target/java-analyzer-bundle.core-1.0.0-SNAPSHOT.jar"
},
"analysisMode": "source-only"
},
{
"location": "mvn://io.javaoperatorsdk:quarkus:1.6.2:jar",
"providerSpecificConfig": {
"lspServerName": "java",
"lspServerPath": "/jdtls/bin/jdtls",
"depOpenSourceLabelsFile": "/usr/local/etc/maven.default.index",
"bundles": "/jdtls/java-analyzer-bundle/java-analyzer-bundle.core/target/java-analyzer-bundle.core-1.0.0-SNAPSHOT.jar"
},
"analysisMode": "full"
}
]
},
Expand Down
10 changes: 10 additions & 0 deletions provider_pod_local_settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@
"bundles": "/jdtls/java-analyzer-bundle/java-analyzer-bundle.core/target/java-analyzer-bundle.core-1.0.0-SNAPSHOT.jar"
},
"analysisMode": "source-only"
},
{
"location": "mvn://io.javaoperatorsdk:quarkus:1.6.2:jar",
"providerSpecificConfig": {
"lspServerName": "java",
"lspServerPath": "/jdtls/bin/jdtls",
"depOpenSourceLabelsFile": "/usr/local/etc/maven.default.index",
"bundles": "/jdtls/java-analyzer-bundle/java-analyzer-bundle.core/target/java-analyzer-bundle.core-1.0.0-SNAPSHOT.jar"
},
"analysisMode": "full"
}
]
},
Expand Down
9 changes: 9 additions & 0 deletions rule-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,12 @@
when:
java.referenced:
pattern: com.sun.net.httpserver.HttpExchange
- category: mandatory
description: |
This rule tests the application downloaded from maven artifact
tag:
- Java Operator SDK
ruleID: java-downloaded-maven-artifact
when:
java.referenced:
pattern: io.javaoperatorsdk.operator.Operator

0 comments on commit 13db4c9

Please sign in to comment.